Created
August 27, 2011 04:30
-
-
Save grodtron/1174979 to your computer and use it in GitHub Desktop.
a function that acts as an intermediary between ajax and the github gist api in my django application
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
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