Lee McFadden avatar Lee McFadden committed 57acfdd

Added basic GAE support for bigdoorkit

Comments (0)

Files changed (2)

src/bigdoorkit/client.py

-import restkit
+try:
+    import restkit
+except ImportError, e:
+    import fetchkit as restkit
+
 import hashlib
 try:
     import json
 
 from uuid import uuid4
 from time import time as unix_time
-from urllib import urlencode
 from exc import PayloadError
 
 __all__ = ["Client"]

src/bigdoorkit/fetchkit.py

+from google.appengine.api import urlfetch
+from urllib import urlencode
+
+class Response(object):
+    """A simple compatibilty layer for RESTkit's responses on GAE.
+    """
+    def __init__(self, resp):
+        self.resp = resp
+
+    def body_string(self):
+        return self.resp.content
+
+class Resource(object):
+    """A simple compatibility layer for RESTkit on GAE.
+    """
+    def __init__(self, host):
+        self.host = host
+        self.form_headers = {'Content-Type':
+                             'application/x-www-form-urlencoded'}
+
+    def _build_url(self, url, params):
+        encoded_params = urlencode(params)
+        if encoded_params:
+            url = "%s?%s" % (url, encoded_params)
+        url = "%s%s" % (self.host, url)
+        return url
+
+    def get(self, url, **params):
+        url = self._build_url(url, params)
+        resp = urlfetch.fetch(url=url)
+        return Response(resp)
+
+    def post(self, url, payload=None, **params):
+        if payload is None:
+            payload = {}
+        encoded_payload = urlencode(payload)
+
+        url = self._build_url(url, params)
+        resp = urlfetch.fetch(url=url,
+                              payload=encoded_payload,
+                              method=urlfetch.POST,
+                              headers=self.form_headers)
+        return Response(resp)
+
+    def put(self, url, payload=None, **params):
+        if payload is None:
+            payload = {}
+        encoded_payload = urlencode(payload)
+
+        url = self._build_url(url, params)
+        resp = urlfetch.fetch(url=url,
+                              payload=encoded_payload,
+                              method=urlfetch.PUT,
+                              headers=self.form_headers)
+        return Response(resp)
+
+    def delete(self, url, **params):
+        url = self._build_url(url, params)
+        resp = urlfetch.fetch(url=url,
+                              method=urlfetch.DELETE)
+        return Response(resp)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.