Commits

mrk  committed 4629ba1 Draft

apiexplorer, continued

  • Participants
  • Parent commits 9399780

Comments (0)

Files changed (1)

File apiexplorer.py

         self.response.status = 302
         self.response.location = next
 
+import time
+
 class ApiExplorer(FilmasterClientHandler):
     API_VERSION = "1.1"
     def get(self):
         uri = self.request.params.get('uri', '/profile/')
         method = self.request.params.get('method', 'get')
         data = self.request.params.get('data', '{}')
+        out = ''
+        error = ''
+
         try:
             if method in ('post', 'put'):
                 args = (json.loads(data), )
             else:
                 args = ()
+            if method == 'get' or self.request.method == 'POST':
+                out = getattr(self.filmaster_client, method)("/%s%s" % (self.API_VERSION, uri), *args)
+                out = json.dumps(out, sort_keys=True, indent=4)
+                out = self.create_links(out)
+        except urllib2.HTTPError, e:
+            error = "HTTPError %d" % e.code
+            out = e.read()
+        except Exception, e:
+            error = repr(e)
 
-            out = getattr(self.filmaster_client, method)("/%s%s" % (self.API_VERSION, uri), *args)
-            out = json.dumps(out, sort_keys=True, indent=4)
-            out = self.create_links(out)
-            error = ''
-        except Exception, e:
-            out = ''
-            error = str(e)
+        params = urlencode(dict(
+            uri=uri.encode('utf-8'),
+            method=method,
+            data=data,
+        ))
+        permalink = "/api-explorer/?" + params
 
         self._response(
                 response=out,
                 method=method,
                 error=error,
                 data=data,
+                permalink=permalink,
         )
 
     post = get
                 <option value="delete" %(delete_selected)s>DELETE</option>
             </select>
             http://api.filmaster.pl/%(api_version)s<input type="text" name="uri" value="%(uri)s" />
+            <a href="%(permalink)s">#</a>
             <input type="submit" />
             <section class="data">
             <label>request data:</lablel>