Last active
September 29, 2015 10:17
-
-
Save dangerouse/1586071 to your computer and use it in GitHub Desktop.
Cloudsponge Proxy Reference - Python & Django
This file contains 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
# Cloudsponge proxy reference | |
# Python reference implementation using django | |
# | |
# contributed by Dannie Chu of foodoro.com | |
from django.http import HttpResponse, HttpResponseRedirect | |
# from fdr.logging.logger import Logger | |
from urllib2 import HTTPRedirectHandler | |
import urllib | |
import urllib2 | |
class NoRedirectHandler(HTTPRedirectHandler): | |
def http_error_302(self, req, fp, code, msg, headers): | |
infourl = urllib.addinfourl(fp, headers, req.get_full_url()) | |
infourl.status = code | |
infourl.code = code | |
return infourl | |
http_error_300 = http_error_301 = http_error_303 = http_error_307 = http_error_302 | |
def cloudspongeProxy(request): | |
httpResponse = HttpResponse() | |
data = {} | |
if request.method == 'GET' : | |
data = request.GET | |
elif request.method == 'POST' : | |
data = request.POST | |
if data : | |
data = urllib.urlencode(data) | |
try : | |
proxyRequest = urllib2.Request("https://api.cloudsponge.com/auth") | |
opener = urllib2.build_opener(NoRedirectHandler()) | |
response = opener.open(proxyRequest, data, timeout=30) | |
if response.getcode() == 302: | |
headers = dict(response.info()) | |
httpResponse = HttpResponseRedirect(headers['location']) | |
except Exception, ex : | |
# logger.exception("failed to proxy request: %s" % str(ex)) | |
return httpResponse |
I've updated the gist so that it doesn't depend on the 'status' header now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist previously worked perfectly for me. Recently, for some reason the response header from api.cloudsponge.com did not have a
status
field, so I had to change line 38 to:if headers.get('location'):
And then everything works as before.