Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created June 26, 2012 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save revolunet/2996024 to your computer and use it in GitHub Desktop.
Save revolunet/2996024 to your computer and use it in GitHub Desktop.
proxify http with django
# -*- 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