Skip to content

Commit

Permalink
start on /url endpoint. for #31
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Sep 16, 2015
1 parent fdbea92 commit 181644c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
34 changes: 33 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

__author__ = 'Ryan Barrett <granary@ryanb.org>'

import json
import logging
import urllib
import urllib2

import appengine_config

Expand All @@ -18,6 +21,8 @@
import webapp2

import activitystreams
from granary import atom
from granary import microformats2
from granary import source

API_PARAMS = {
Expand All @@ -44,7 +49,7 @@ def template_vars(self):


class DemoHandler(webapp2.RequestHandler):
"""Handles requests from the interactive demo form on the front page."""
"""Handles silo requests from the interactive demo form on the front page."""
def get(self):
params = {name: val for name, val in self.request.params.items()
if name in API_PARAMS}
Expand All @@ -55,6 +60,32 @@ def get(self):
urllib.urlencode(params)))


class UrlHandler(activitystreams.Handler):
"""Handles AS/mf2 requests from the interactive demo form on the front page."""
def get(self):
expected_inputs = ('activitystreams', 'html', 'json-mf2')
input = util.get_required_param(self, 'input')
if input not in expected_inputs:
raise exc.HTTPBadRequest('Invalid input: %s, expected one of %r' %
(input, expected_inputs))

# fetch url
url = util.get_required_param(self, 'url')
logging.info('Fetching %s', url)
resp = urllib2.urlopen(url, timeout=appengine_config.HTTP_TIMEOUT)
body = resp.read()

# decode data
if input == 'activitystreams':
activities = json.loads(body)
# elif input == 'html':
# activities = json.loads(body)
elif input == 'json-mf2':
activities = microformats2.json_to_object(json.loads(body).get('items', []))

self.write_response(source.Source.make_activities_base_response(activities))


application = webapp2.WSGIApplication([
('/', FrontPageHandler),
('/demo', DemoHandler),
Expand All @@ -68,4 +99,5 @@ def get(self):
('/instagram/oauth_callback', instagram.CallbackHandler.to('/')),
('/twitter/start_auth', twitter.StartHandler.to('/twitter/oauth_callback')),
('/twitter/oauth_callback', twitter.CallbackHandler.to('/')),
('/url', UrlHandler),
] + handlers.HOST_META_ROUTES, debug=appengine_config.DEBUG)
2 changes: 1 addition & 1 deletion app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ handlers:
- url: /oauth_dropins/static
static_dir: local/lib/python2.7/site-packages/oauth_dropins/static

- url: /(demo|.well-known/.*)?
- url: /(demo|url|.well-known/.*)?
script: app.application
secure: always

Expand Down
52 changes: 52 additions & 0 deletions test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Unit tests for app.py.
"""

import json

import oauth_dropins.webutil.test
from oauth_dropins.webutil import testutil

import app


ACTIVITIES = [{
'object': {
'content': 'foo bar',
'published': '2012-03-04T18:20:37+00:00',
'url': 'https://perma/link',
}
}, {
'object': {
'content': 'baz baj',
},
}]

MF2_JSON = {'items': [{
'type': [u'h-entry'],
'properties': {
'content': [{
'value': 'foo bar',
'html': 'foo bar',
}],
'published': ['2012-03-04T18:20:37+00:00'],
'url': ['https://perma/link'],
},
}, {
'type': [u'h-entry'],
'properties': {
'content': [{
'value': 'baz baj',
'html': 'baz baj',
}],
},
}]}


class AppTest(testutil.HandlerTest):
def test_url_activitystreams_json_mf2(self):
self.expect_urlopen('http://my/posts.json', json.dumps(ACTIVITIES))
self.mox.ReplayAll()
resp = app.application.get_response(
'/url?url=http://my/posts.json&input=activitystreams&output=json-mf2')
self.assert_equals(200, resp.status_int)
self.assert_equals(MF2_JSON, json.loads(resp.body))

0 comments on commit 181644c

Please sign in to comment.