X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/f4921ca797953ddab6b35786864b2dd523190503..636fd34b05155b81691222ad80c4a3746d0c2fc1:/apps/apiclient/__init__.py?ds=sidebyside diff --git a/apps/apiclient/__init__.py b/apps/apiclient/__init__.py index 7913ac3c..56ecb96d 100644 --- a/apps/apiclient/__init__.py +++ b/apps/apiclient/__init__.py @@ -1,10 +1,9 @@ import urllib -from django.utils import simplejson +import json import oauth2 -from apiclient.models import OAuthConnection -from apiclient.settings import WL_CONSUMER_KEY, WL_CONSUMER_SECRET, WL_API_URL +from apiclient.settings import WL_CONSUMER_KEY, WL_CONSUMER_SECRET, WL_API_URL, BETA_API_URL if WL_CONSUMER_KEY and WL_CONSUMER_SECRET: @@ -21,26 +20,32 @@ class NotAuthorizedError(BaseException): pass -def api_call(user, path, data=None): - # what if not verified? - conn = OAuthConnection.get(user) +def api_call(user, path, data=None, beta=False): + from .models import OAuthConnection + api_url = BETA_API_URL if beta else WL_API_URL + conn = OAuthConnection.get(user=user, beta=beta) if not conn.access: raise NotAuthorizedError("No WL authorization for user %s." % user) token = oauth2.Token(conn.token, conn.token_secret) client = oauth2.Client(wl_consumer, token) if data is not None: + data = json.dumps(data) + data = urllib.urlencode({"data": data}) resp, content = client.request( - "%s%s.json" % (WL_API_URL, path), + "%s%s" % (api_url, path), method="POST", - body=urllib.urlencode(data)) + body=data) else: resp, content = client.request( - "%s%s.json" % (WL_API_URL, path)) + "%s%s" % (api_url, path)) status = resp['status'] + if status == '200': - return simplejson.loads(content) + return json.loads(content) elif status.startswith('2'): return + elif status == '401': + raise ApiError('User not authorized for publishing.') else: - raise ApiError("WL API call error") + raise ApiError("WL API call error %s, path: %s" % (status, path))