+
+ auth_handler = urllib2.HTTPDigestAuthHandler();
+ auth_handler.add_password(
+ realm="localhost:8000",
+ uri="http://localhost:8000/api/",
+ user="test", passwd="test")
+
+
+ opener = urllib2.build_opener(auth_handler)
+ rq = urllib2.Request("http://localhost:8000/api/books.json")
+ rq.add_data(json.dumps({"text": document.text, "compressed": False}))
+ rq.add_header("Content-Type", "application/json")
+
+ try:
+ response = opener.open(rq)
+ result = {"success": True, "message": response.read()}
+ except urllib2.HTTPError, e:
+ logger.exception("Failed to send")
+ if e.code == 500:
+ return HttpResponse(e.read(), mimetype='text/plain')
+ result = {"success": False, "reason": e.read(), "errno": e.code}
+
+ return HttpResponse( json.dumps(result), mimetype='application/json')
\ No newline at end of file