aboutsummaryrefslogtreecommitdiffstats
path: root/github3/api.py
diff options
context:
space:
mode:
Diffstat (limited to 'github3/api.py')
-rw-r--r--github3/api.py70
1 files changed, 43 insertions, 27 deletions
diff --git a/github3/api.py b/github3/api.py
index ad02035..42bfa26 100644
--- a/github3/api.py
+++ b/github3/api.py
@@ -9,9 +9,14 @@ This module provies the core GitHub3 API interface.
import omnijson as json
+from .models import *
from .helpers import is_collection, to_python, to_api, get_scope
from .config import settings
+
+import requests
+
+
class GithubCore(object):
@staticmethod
@@ -30,49 +35,54 @@ class GithubCore(object):
raise ResponseError('The API Response was not valid.')
- def _to_map(self, obj, iterable):
- """Maps given dict iterable to a given Resource object."""
+ @staticmethod
+ def _generate_url(endpoint):
+ """Generates proper endpoint URL."""
- a = []
+ if is_collection(endpoint):
+ resource = map(str, endpoint)
+ resource = '/'.join(endpoint)
+ else:
+ resource = endpoint
- for it in iterable:
- a.append(obj.new_from_dict(it, rdd=self))
+ return (settings.base_url + resource)
- return a
+ def _get_http_resource(self, endpoint, params=None):
- def _get_resources(self, key, obj, limit=None, **kwargs):
- """GETs resources of given path, maps them to objects, and
- handles paging.
- """
- pass
+ url = self._generate_url(endpoint)
+ r = requests.get(url, params=params)
+ r.raise_for_status()
+ return r
- def _get_resource(self, http_resource, obj, **kwargs):
- """GETs API Resource of given path."""
- item = self._get_http_resource(http_resource, params=kwargs)
- item = self._resource_deserialize(item)
- return obj.new_from_dict(item, rdd=self)
+ def _get_resource(self, resource, obj, **kwargs):
+ r = self._get_http_resource(resource, params=kwargs)
+ item = self._resource_deserialize(r.content)
- def _post_resource(self, http_resource, **kwargs):
- """POSTs API Resource of given path."""
+ return obj.new_from_dict(item, gh=self)
- r = self._post_http_resource(http_resource, params=kwargs)
- return r
+ def _to_map(self, obj, iterable):
+ """Maps given dict iterable to a given Resource object."""
- def _delete_resource(self, http_resource):
- """DELETEs API Resource of given path."""
+ a = list()
- r = self._delete_http_resource(http_resource)
+ for it in iterable:
+ a.append(obj.new_from_dict(it, rdd=self))
- if r['status'] in ('200', '204'):
- return True
- else:
- return False
+ return a
+
+ def _get_url(self, resource):
+
+ if is_collection(resource):
+ resource = map(str, resource)
+ resource = '/'.join(resource)
+
+ return resource
@@ -83,6 +93,12 @@ class Github(GithubCore):
super(Github, self).__init__()
+ def get_user(self, username):
+ # return 'kennethreitz'
+ return self._get_resource(('users', username), User)
+ # return User()
+
+
class ResponseError(Exception):