Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
generalize keys_paginator and use it for Facebook
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco committed Feb 25, 2015
1 parent 11fe66d commit d7bf6f9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
14 changes: 8 additions & 6 deletions gratipay/elsewhere/_paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,18 @@ def f(self, response, parsed):
return f


def keys_paginator(**kw):
def keys_paginator(page_key, **kw):
# https://confluence.atlassian.com/display/BITBUCKET/Version+2#Version2-Pagingthroughobjectcollections
page_key = kw.get('page', 'values')
total_count_key = kw.get('total_count', 'size')
# https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2#paging
paging_key = kw.get('paging')
total_key = kw.get('total')
links_keys_map = tuple((k, kw.get(k, k)) for k in links_keys)
def f(self, response, parsed):
page = parsed[page_key]
links = {k: _strip_prefix(self.api_url, parsed[k2])
paging = parsed.get(paging_key, {}) if paging_key else parsed
links = {k: _strip_prefix(self.api_url, paging[k2])
for k, k2 in links_keys_map
if parsed.get(k2)}
total_count = parsed.get(total_count_key, -1) if links else len(page)
if paging.get(k2)}
total_count = paging.get(total_key, -1) if links else len(page)
return page, total_count, links
return f
2 changes: 1 addition & 1 deletion gratipay/elsewhere/bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Bitbucket(PlatformOAuth1):

# API attributes
api_format = 'json'
api_paginator = keys_paginator(prev='previous')
api_paginator = keys_paginator('values', prev='previous', total='size')
api_url = 'https://bitbucket.org/api'
api_user_info_path = '/2.0/users/{user_name}'
api_user_self_info_path = '/2.0/user'
Expand Down
2 changes: 2 additions & 0 deletions gratipay/elsewhere/facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from gratipay.elsewhere import PlatformOAuth2
from gratipay.elsewhere._extractors import key
from gratipay.elsewhere._paginators import keys_paginator


class Facebook(PlatformOAuth2):
Expand All @@ -18,6 +19,7 @@ class Facebook(PlatformOAuth2):

# API attributes
api_format = 'json'
api_paginator = keys_paginator('data', paging='paging', prev='previous')
api_url = 'https://graph.facebook.com'
api_user_info_path = '/{user_name}'
api_user_self_info_path = '/me'
Expand Down

0 comments on commit d7bf6f9

Please sign in to comment.