add api_proxy view
This commit is contained in:
parent
a7613f92ef
commit
51f6b0645f
1 changed files with 37 additions and 0 deletions
|
@ -1,9 +1,17 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# vi:si:et:sw=4:sts=4:ts=4
|
# vi:si:et:sw=4:sts=4:ts=4
|
||||||
|
import cookielib
|
||||||
|
import urllib2
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
from celery.utils import get_full_cls_name
|
from celery.utils import get_full_cls_name
|
||||||
from celery.backends import default_backend
|
from celery.backends import default_backend
|
||||||
|
|
||||||
|
from django.http import HttpResponse
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
from shortcuts import json_response
|
from shortcuts import json_response
|
||||||
|
import ox
|
||||||
|
|
||||||
|
|
||||||
def task_status(request, task_id):
|
def task_status(request, task_id):
|
||||||
|
@ -22,3 +30,32 @@ def task_status(request, task_id):
|
||||||
'traceback': traceback})
|
'traceback': traceback})
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
class SessionCookieJar(cookielib.LWPCookieJar):
|
||||||
|
def save(self):
|
||||||
|
return "#LWP-Cookies-2.0\n" + self.as_lwp_str()
|
||||||
|
|
||||||
|
def load(self, data, ignore_discard=True, ignore_expires=True):
|
||||||
|
f = StringIO(data)
|
||||||
|
self._really_load(f, 'memory', ignore_discard, ignore_expires)
|
||||||
|
|
||||||
|
def api_proxy(request):
|
||||||
|
url = settings.OXAPI_URL
|
||||||
|
cj = SessionCookieJar()
|
||||||
|
if 'cj' in request.session:
|
||||||
|
cj.load(request.session['cj'])
|
||||||
|
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
|
||||||
|
opener.addheaders = [
|
||||||
|
('User-Agent', request.META.get('HTTP_USER_AGENT'))
|
||||||
|
]
|
||||||
|
form = ox.MultiPartForm()
|
||||||
|
for key in request.POST:
|
||||||
|
form.add_field(key, request.POST[key])
|
||||||
|
r = urllib2.Request(url)
|
||||||
|
body = str(form)
|
||||||
|
r.add_header('Content-type', form.get_content_type())
|
||||||
|
r.add_header('Content-length', len(body))
|
||||||
|
r.add_data(body)
|
||||||
|
f = opener.open(r)
|
||||||
|
response = HttpResponse(f.read())
|
||||||
|
request.session['cj'] = cj.save()
|
||||||
|
return response
|
||||||
|
|
Loading…
Reference in a new issue