Created
June 26, 2012 14:17
-
-
Save revolunet/2996024 to your computer and use it in GitHub Desktop.
proxify http with django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: UTF-8 -*- | |
# | |
# sample django HTTP proxy | |
# | |
import urlparse | |
import requests | |
import logger | |
API_URL = 'http://www.wsbackend.com/api' | |
# restrict access to loggedin users | |
#@login_required | |
def api(request, path='/'): | |
# get original request parameters | |
params = {} | |
method = 'get' | |
if request.method == 'POST': | |
params = request.POST.copy() | |
elif request.method == 'GET': | |
params = request.GET.copy() | |
# add internal user id for backend | |
params['userId'] = request.session.get('userId') | |
# build url | |
full_url = urlparse.urljoin(API_URL, path) | |
# log info | |
logger.info('user %s calls %s' % (params['userId'], full_url)) | |
# call the remote API with the same http method | |
api_response = getattr(requests, request.method.lower())(full_url, params=params) | |
# return remote response to final user | |
return api_response.content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment