aboutsummaryrefslogtreecommitdiffstats
path: root/github3/core
diff options
context:
space:
mode:
Diffstat (limited to 'github3/core')
-rw-r--r--github3/core/__init__.py0
-rw-r--r--github3/core/client.py96
-rw-r--r--github3/core/resources/__init__.py81
-rw-r--r--github3/core/resources/user/__init__.py2
-rw-r--r--github3/core/resources/user/emails.py8
-rw-r--r--github3/core/resources/user/followers.py0
-rw-r--r--github3/core/resources/user/keys.py0
-rw-r--r--github3/core/resources/user/user.py23
8 files changed, 210 insertions, 0 deletions
diff --git a/github3/core/__init__.py b/github3/core/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/github3/core/__init__.py
diff --git a/github3/core/client.py b/github3/core/client.py
new file mode 100644
index 0000000..99a88f0
--- /dev/null
+++ b/github3/core/client.py
@@ -0,0 +1,96 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+import requests
+from errors import GithubError
+
+
+VALID_REQUEST_ARGS = set((
+ 'params', 'data', 'headers', 'cookies', 'files', 'auth', 'timeout',
+ 'allow_redirects', 'proxies', 'return_response', 'config',
+ 'prefetch', 'verify'))
+
+
+class Client(object):
+ """ Client to send configurated requests"""
+
+ def __init__(self, **kwargs):
+ """
+ It can be configurated
+
+ :login, :password, :user, :repo, :token, :per_page, :base_url
+ """
+
+ self.requester = requests.session()
+ self.config = {
+ 'per_page': 100,
+ 'base_url': 'https://api.github.com/'
+ }
+ self.config.update(kwargs)
+ self.set_credentials(self.config.get('login'),
+ self.config.get('password'))
+ self.set_token(self.config.get('token'))
+ self.__set_params(self.config)
+
+ @property
+ def user(self):
+ return self.config.get('user')
+
+ @user.setter
+ def set_user(self, user):
+ self.config['user'] = user
+
+ @property
+ def repo(self):
+ return self.config.get('repo')
+
+ @repo.setter
+ def set_repo(self, repo):
+ self.config['repo'] = repo
+
+ def set_credentials(self, login, password):
+ if login and password:
+ self.requester.auth = (login, password)
+
+ def set_token(self, token):
+ if token:
+ self.requester.params['access_token'] = token
+
+ def __set_params(self, config):
+ self.requester.params['per_page'] = config.get('per_page')
+
+ def __parse_kwargs(func):
+ """ Decorator to put extra args into requests.params """
+
+ def wrapper(self, verb, resource, **kwargs):
+ diffs = kwargs.viewkeys() - VALID_REQUEST_ARGS
+ new_params = kwargs.get('params') or {}
+ new_params.update({key:kwargs[key] for key in diffs})
+ kwargs['params'] = new_params
+ return func(self, verb, resource, **kwargs)
+ return wrapper
+
+ @__parse_kwargs
+ def request(self, verb, resource, **kwargs):
+ resource = "%s%s" % (self.config['base_url'], resource)
+ response = self.requester.request(verb, resource, **kwargs)
+ GithubError(response).process()
+ return response
+
+ def get(self, resource, **kwargs):
+ return self.request('get', resource, **kwargs)
+
+ def post(self, resource, **kwargs):
+ return self.request('post', resource, **kwargs)
+
+ def patch(self, resource, **kwargs):
+ return self.request('patch', resource, **kwargs)
+
+ def put(self, resource, **kwargs):
+ return self.request('put', resource, **kwargs)
+
+ def delete(self, resource, **kwargs):
+ return self.request('delete', resource, **kwargs)
+
+ def head(self, resource, **kwargs):
+ return self.request('head', resource, **kwargs)
diff --git a/github3/core/resources/__init__.py b/github3/core/resources/__init__.py
new file mode 100644
index 0000000..c5dee18
--- /dev/null
+++ b/github3/core/resources/__init__.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+import re
+
+class UriNotFound(Exception):
+ pass
+
+
+class UriInvalid(Exception):
+ pass
+
+
+class Resource(object):
+ """ """
+
+ def __init__(self, args):
+ """ """
+ self.args = args
+ self.validate()
+ self.uri = self.set_uri()
+
+ def validate(self, args):
+ raise NotImplementedError
+
+ def set_uri(self):
+ raise NotImplementedError
+
+ def get_uri(self):
+ return str(self.uri).strip('/')
+
+ def get_model(self):
+ return getattr(self, 'model', '')
+
+ def __getattr__(self, name):
+ return self.args.get(name)
+
+ def __str__(self):
+ return self.get_uri()
+
+
+class Factory(object):
+ """ """
+ import_pattern = re.compile(r'^(\w+\.)+\w+$')
+
+ def __init__(self, **kwargs):
+ self.args = kwargs
+
+ def __validate(func):
+ """ """
+
+ def wrapper(self, resource_path):
+ if not Factory.import_pattern.match(resource_path):
+ raise UriInvalid("'%s' isn't valid form" % resource_path)
+ return func(self, resource_path.lower())
+ return wrapper
+
+ def __dispatch(func):
+ """ """
+
+ from importlib import import_module
+ def wrapper(self, resource_path):
+ module_chunk, s, uri_chunk = resource_path.rpartition('.')
+ try:
+ # TODO: CamelCase and under_score support, now only Class Name
+ module = import_module('core.resources.%s' % module_chunk)
+ uri = getattr(module, uri_chunk.capitalize())
+ except ImportError:
+ raise UriNotFound("'%s' module does not exists" % module_chunk)
+ except AttributeError:
+ raise UriNotFound("'%s' uri doesn't exists into '%s' module"
+ % (uri_chunk.capitalize(), module_chunk))
+ return func(self, uri)
+ return wrapper
+
+ @__validate
+ @__dispatch
+ def __call__(self, resource_class=''):
+ resource = resource_class(self.args)
+ assert isinstance(resource, Resource)
+ return resource
diff --git a/github3/core/resources/user/__init__.py b/github3/core/resources/user/__init__.py
new file mode 100644
index 0000000..8248571
--- /dev/null
+++ b/github3/core/resources/user/__init__.py
@@ -0,0 +1,2 @@
+from core.resources import Resource
+from user import *
diff --git a/github3/core/resources/user/emails.py b/github3/core/resources/user/emails.py
new file mode 100644
index 0000000..83644db
--- /dev/null
+++ b/github3/core/resources/user/emails.py
@@ -0,0 +1,8 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+from core.uris import Manager
+from models.base import Model
+
+class List(Manager):
+ pass
diff --git a/github3/core/resources/user/followers.py b/github3/core/resources/user/followers.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/github3/core/resources/user/followers.py
diff --git a/github3/core/resources/user/keys.py b/github3/core/resources/user/keys.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/github3/core/resources/user/keys.py
diff --git a/github3/core/resources/user/user.py b/github3/core/resources/user/user.py
new file mode 100644
index 0000000..4308f01
--- /dev/null
+++ b/github3/core/resources/user/user.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+from . import Resource
+from models.user import User
+
+__all__ = ('Get', 'Update')
+
+class Get(Resource):
+
+ model = User
+
+ def validate(self):
+ pass
+
+ def set_uri(self):
+ if self.user:
+ return 'users/%s' % self.user
+ else:
+ return 'user'
+
+class Update(Resource):
+ pass