Skip to content

Commit

Permalink
fixes for Github API v3
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe committed Jul 2, 2012
1 parent d46297b commit 109f7a2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
14 changes: 9 additions & 5 deletions apps/gists/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ def post(self):
if gist:
return self.redirect(self.reverse_url('view_gist', gist_id))
http = tornado.httpclient.AsyncHTTPClient()
url = "http://gist.github.com/api/v1/json/%s" % gist_id
url = "https://api.github.com/gists/%s" % gist_id
http.fetch(url, callback=lambda r:self.on_gist_found(gist_id, r))

def on_gist_found(self, gist_id, response):
gist_struct = anyjson.deserialize(response.body)
#pprint(gist_struct)
pprint(gist_struct)
try:
gist_info = gist_struct['gists'][0]
gist_info = gist_struct#['gists'][0]
except KeyError:
# TODO: redirect to a warning page
# gist is not found
Expand All @@ -44,8 +44,8 @@ def on_gist_found(self, gist_id, response):
gist.files = [unicode(x) for x in gist_info['files']]
gist.contents = []
gist.public = gist_info['public']
gist.owner = unicode(gist_info['owner'])
gist.repo = unicode(gist_info['repo'])
gist.owner = unicode(gist_info['user']['login'])
gist.repo = None #unicode(gist_info['repo'])
gist.user = self.get_current_user()
gist.save()

Expand Down Expand Up @@ -87,6 +87,7 @@ def find_gist(self, gist_id):
except (ValueError, AssertionError):
raise tornado.web.HTTPError(404, "Gist not found")


@route(r'/(\d+)/$', name="view_gist")
class GistHandler(GistBaseHandler):

Expand All @@ -111,6 +112,7 @@ def get(self, gist_id):
self.db.Vote.collection.one(_user_vote_search))
self.render("gist.html", **options)


@route(r'/(\d+)/edit/$', name="edit_gist")
class EditGistHandler(GistHandler):

Expand Down Expand Up @@ -180,6 +182,7 @@ def post(self):
html = markdown.markdown(text, safe_mode="escape")
self.write_json(dict(html=html))


@route(r'/tags.json$', name="autocomplete_tags")
class AutocompleteTags(BaseHandler):
def check_xsrf_cookie(self):
Expand All @@ -199,6 +202,7 @@ def get(self):
all_tags.sort(lambda x,y:cmp(x.lower(), y.lower()))
self.write_json(dict(tags=all_tags))


@route(r'/(\d+)/comments', name="comments")
class CommentsHandler(GistHandler):

Expand Down
23 changes: 4 additions & 19 deletions apps/main/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,51 +313,36 @@ def get_authenticated_user(self, redirect_uri, client_id, client_secret,
def _on_access_token(self, redirect_uri, client_id, client_secret,
callback, fields, response):
if response.error:
logging.warning('Facebook auth error: %s' % str(response))
logging.warning('GitHub auth error: %s' % response)
callback(None)
return

#print "self.request.arguments"
#print self.request.arguments
#print "RESPONSE.BODY:"
#print response.body

session = {
"access_token": cgi.parse_qs(response.body)["access_token"][-1],
"expires": cgi.parse_qs(response.body).get("expires")
}
#print "SESSION"
#print session
#print "\n"

self.github_request(
path="/user/show",
path="/user",
callback=self.async_callback(
self._on_get_user_info, callback, session, fields),
access_token=session["access_token"],
fields=",".join(fields)
)


def _on_get_user_info(self, callback, session, fields, user):
if user is None:
callback(None)
return

#pprint(user)
fieldmap = user['user']
print 'session.get("expires")', repr(session.get("expires"))

fieldmap = dict(user)
fieldmap.update({"access_token": session["access_token"],
"session_expires": session.get("expires")})
callback(fieldmap)


def github_request(self, path, callback, access_token=None,
post_args=None, **args):
"""
"""
url = "https://github.com/api/v2/json" + path
url = "https://api.github.com" + path
all_args = {}
if access_token:
all_args["access_token"] = access_token
Expand Down

0 comments on commit 109f7a2

Please sign in to comment.