diff options
Diffstat (limited to 'github3/handlers')
| -rw-r--r-- | github3/handlers/__init__.py | 0 | ||||
| -rw-r--r-- | github3/handlers/base.py | 85 | ||||
| -rw-r--r-- | github3/handlers/gists.py | 28 | ||||
| -rw-r--r-- | github3/handlers/user.py | 205 |
4 files changed, 318 insertions, 0 deletions
diff --git a/github3/handlers/__init__.py b/github3/handlers/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/github3/handlers/__init__.py diff --git a/github3/handlers/base.py b/github3/handlers/base.py new file mode 100644 index 0000000..50e2df8 --- /dev/null +++ b/github3/handlers/base.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +# +# author: David Medina + +from github3.core import Paginate +from github3.converters import Modelizer + +class Handler(object): + """ Handler base. Requests to API and modelize responses """ + + def __init__(self, gh): + self._gh = gh + super(Handler, self).__init__() + + def _prefix_resource(self, resource): + prefix = getattr(self, 'prefix', '') + return '/'.join((prefix, resource)).rstrip('/') + + def _get_converter(self, **kwargs): + converter = kwargs.get( + 'converter', # 1. in kwargs + getattr(self, 'converter', # 2. in handler + Modelizer())) # 3. Default + + return converter + + def _put(self, resource, **kwargs): + """ Put proxy request""" + + return self._bool(resource, method='put', **kwargs) + + def _delete(self, resource, **kwargs): + """ Delete proxy request""" + + return self._bool(resource, method='delete', **kwargs) + + def _bool(self, resource, **kwargs): + """ Handler request to boolean response """ + + from github3.exceptions import NotFound + resource = self._prefix_resource(resource) + try: + callback = getattr(self._gh, kwargs.get('method',''), self._gh.head) + response = callback(resource, **kwargs) + except NotFound: + return False + assert response.status_code == 204 + return True + + #TODO: if limit is multiple of per_page... it do another request for nothing + def _get_resources(self, resource, model=None, limit=None, **kwargs): + """ Hander request to multiple resources """ + + resource = self._prefix_resource(resource) + page_resources = Paginate(resource, self._gh.get, **kwargs) + counter = 1 + for page in page_resources: + for raw_resource in page: + if limit and counter > limit: break + counter += 1 + converter = self._get_converter(**kwargs) + converter.inject(model) + yield converter.loads(raw_resource) + else: + continue + break + + def _get_resource(self, resource, model=None, **kwargs): + """ Handler request to single resource """ + + resource = self._prefix_resource(resource) + raw_resource = self._gh.get(resource) + converter = self._get_converter(**kwargs) + converter.inject(model) + return converter.loads(raw_resource) + + def _post_resource(self, resource, data, model=None, **kwargs): + """ Handler request to create a resource """ + + resource = self._prefix_resource(resource) + raw_resource = self._gh.post(resource, data=data) + converter = self._get_converter(**kwargs) + converter.inject(model) + return converter.loads(raw_resource) diff --git a/github3/handlers/gists.py b/github3/handlers/gists.py new file mode 100644 index 0000000..15f215c --- /dev/null +++ b/github3/handlers/gists.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +# +# author: Antti Kaihola + +from .base import Handler +from .. import models + + +class Gist(Handler): + """ Gist handler """ + + prefix = 'gists' + + def __repr__(self): + return '<Gist handler>' + + def get(self, gist_id): + """ Return gist """ + + return self._get_resource(gist_id, model=models.Gist) + + def create_gist(self, description, public=True, files={}): + """ Create a gist """ + data = {'description': description, + 'public': public, + 'files': files} + return self._post_resource('', data=data, model=models.Gist) diff --git a/github3/handlers/user.py b/github3/handlers/user.py new file mode 100644 index 0000000..fb893b4 --- /dev/null +++ b/github3/handlers/user.py @@ -0,0 +1,205 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +# +# author: David Medina + +from .base import Handler +import github3.models as models +from github3.converters import Rawlizer + +class User(Handler): + """ User handler with public access """ + + prefix = 'users' + + def __repr__(self): + return '<User handler> %s>' % getattr(self, 'username', 'without user') + + def set_username(self, user): + """ + Set username to query public handler + + :param `user`: User model or username string + """ + + parse_user = str(getattr(user, 'login', user)) + self.username = parse_user + self.prefix = '/'.join((self.prefix, parse_user)) + + def get(self): + """ Return user """ + + return self._get_resource('', model=models.User) + + def get_followers(self): + """ Return user's followers """ + + return self._get_resources('followers', model=models.User) + + def get_following(self): + """ Return users that follow """ + + return self._get_resources('following', model=models.User) + + def get_repos(self): + """ Return user's public repositories """ + + return self._get_resources('repos', model=models.Repo) + + def get_watched(self): + """ Return repositories that user whatch """ + + return self._get_resources('watched', model=models.Repo) + + def get_orgs(self): + """ Return user's public organizations """ + + return self._get_resources('orgs', model=models.Org) + + def get_gists(self): + """ Return user's gists """ + + return self._get_resources('gists', model=models.Gist) + +class AuthUser(User): + """ User handler with public and private access """ + + prefix = 'user' + + def __repr__(self): + return '<AuthUser handler> %s>' % self._gh.session.auth[0] + + def get(self): + return self._get_resource('', model=models.AuthUser) + + def get_emails(self): + """ Return list of emails """ + + # Ignore converter, it must be Rawlizer + emails = self._get_resource('emails', converter=Rawlizer()) + return emails + + def create_emails(self, *args): + """ + Add emails + + :param args: Collection of emails + create_emails(*('test1@example.com', 'test2@example.cm')) + """ + parsed_emails = map(str, args) + all_mails = self._post_resource( + 'emails', data=parsed_emails, converter=Rawlizer()) + return all_mails + + def delete_emails(self, *args): + """ + Delete emails + + :param args: Collection of emails + create_emails(*('test1@example.com', 'test2@example.cm')) + """ + parsed_emails = map(str, args) + return self._delete('emails', data=parsed_emails) + + def is_following(self, user): + """ + Return true if you are following the user + + :param `user`: User model or username string + """ + + parse_user = str(getattr(user, 'login', user)) + return self._bool('following/%s' % parse_user) + + def follow(self, user): + """ + Follow user + + :param `user`: User model or username string + """ + + parse_user = str(getattr(user, 'login', user)) + return self._put('following/%s' % parse_user) + + def unfollow(self, user): + """ + Unfollow user + + :param `user`: User model or username string + """ + + parse_user = str(getattr(user, 'login', user)) + return self._delete('following/%s' % parse_user) + + def get_keys(self): + """ Get public keys """ + + return self._get_resources('keys', model=models.Key) + + def get_key(self, key_id): + """ Get public key by id """ + + return self._get_resource('keys/%s' % key_id, model=models.Key) + + def create_key(self, **kwargs): + """ + Create public key + + :param title + :param key: Key string + """ + + #TODO: render key.pub file + key = { + 'title': kwargs.get('title',''), + 'key': kwargs.get('key','') + } + return self._post_resource('keys', data=key, model=models.Key) + + def delete_key(self, key_id): + """ Delete public key """ + + return self._delete('keys/%s' % key_id) + + def get_repos(self, filter='all'): + """ + Return user's public repositories + + param: filter: 'all', 'public', 'private' or 'member' + """ + + return self._get_resources('repos', model=models.Repo, + type=str(filter)) + + def is_watching_repo(self, owner, repo): + """ + Return true if you are watching the user repository + + :param owner: username + :param repo: repository name + is_watching_repo('copitux', 'python-github3') + """ + + owner = getattr(owner, 'login', owner) + repo = getattr(repo, 'name', repo) + return self._bool('watched/%s/%s' % (owner, repo)) + + def watch_repo(self, owner, repo): + """ + Watch the repository + + :param owner: username + :param repo: repository name + """ + + return self._put('watched/%s/%s' % (owner, repo)) + + def unwatch_repo(self, owner, repo): + """ + Unwatch the repository + + :param owner: username + :param repo: repository name + """ + + return self._delete('watched/%s/%s' % (owner, repo)) |
