Skip to content

Instantly share code, notes, and snippets.

@grodtron
Created August 27, 2011 04:39
Show Gist options
  • Save grodtron/1174989 to your computer and use it in GitHub Desktop.
Save grodtron/1174989 to your computer and use it in GitHub Desktop.
A function that acts as an intermediary between ajax and github in my django application
def createGist(request):
if(request.method == "POST"):
data = {}
data["description"] = request.POST["description"]
data["public"] = True
filename = request.POST["filename"]
data["files"] = {filename:{}}
data["files"][filename]["content"] = request.POST["content"]
url = "https://api.github.com/gists"
auth_string = "%s:%s" % (request.POST["username"], request.POST["password"])
auth_string = base64.encodestring(auth_string)[:-1]
req = urllib2.Request(url, simplejson.dumps(data))
req.add_header("Authorization", "Basic %s" % auth_string)
try:
result = urllib2.urlopen(req)
except urllib2.HTTPError, e:
return HttpResponse('<p>error: ' + str(e) + '</p>', mimetype='text/html', status=e.code)
except urllib2.URLError, e:
return HttpResponse('<p>error: ' + str(e) + '</p>', mimetype='text/html', status=500)
result = simplejson.load(result)
embedhtml = "<script src=\""
embedhtml += result["html_url"]
embedhtml += ".js?file=" + filename + "\"></script>"
return HttpResponse(embedhtml, mimetype="text/html")
else:
return HttpResponse('{"error":"no data sent"}', mimetype="application/json")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment