aboutsummaryrefslogtreecommitdiffstats
path: root/pygithub3/requests/__init__.py
diff options
context:
space:
mode:
authorDavid Medina <davidmedina9@gmail.com>2012-02-06 21:39:59 +0100
committerDavid Medina <davidmedina9@gmail.com>2012-02-07 19:22:59 +0100
commit86453d2fb40e21280aeeee7e5ee11229bcb5eaac (patch)
tree2490c76fbbdef9bc14e5c1c2b83b632b1401eb2e /pygithub3/requests/__init__.py
parentSomething confusing about PUT request (diff)
downloadpython-github3-86453d2fb40e21280aeeee7e5ee11229bcb5eaac.tar.xz
python-github3-86453d2fb40e21280aeeee7e5ee11229bcb5eaac.zip
Clean requests interface
Refactorize on build request to support specific requests cleaner *Also support to verbose on requests library
Diffstat (limited to 'pygithub3/requests/__init__.py')
-rw-r--r--pygithub3/requests/__init__.py106
1 files changed, 56 insertions, 50 deletions
diff --git a/pygithub3/requests/__init__.py b/pygithub3/requests/__init__.py
index 725ea12..9fdde27 100644
--- a/pygithub3/requests/__init__.py
+++ b/pygithub3/requests/__init__.py
@@ -8,60 +8,77 @@ try:
except ImportError:
import json
-ABS_IMPORT_PREFIX = 'pygithub3.requests'
+from pygithub3.exceptions import DoesNotExists, UriInvalid, ValidationError
+from pygithub3.resources.base import Raw
+ABS_IMPORT_PREFIX = 'pygithub3.requests'
-class RequestNotFound(Exception):
- pass
+class Body(object):
-class RequestUriInvalid(Exception):
- pass
+ def __init__(self, content, schema):
+ self.content = content
+ self.schema = schema
+ def dumps(self):
+ if not self.content:
+ return None
+ return json.dumps(self.parse())
-class RequestValidationError(Exception):
- pass
+ def parse(self):
+ if not self.schema:
+ return self.content
+ if not hasattr(self.content, 'items'):
+ raise ValidationError("'%s' needs a content dictionary"
+ % self.__class__.__name__)
+ return {key: self.content[key] for key in self.schema
+ if key in self.content}
class Request(object):
""" """
+ uri = ''
+ resource = Raw
+ body_schema = ()
+
def __init__(self, args):
""" """
+ self.body = args.pop('body', None)
self.args = args
- self.validate()
- self.uri = self.set_uri()
+ self.clean()
- def validate(self):
- raise NotImplementedError
+ def clean(self):
+ self.uri = self.clean_uri() or self.uri
+ self.body = Body(self.clean_body(), self.body_schema)
- def set_uri(self):
- raise NotImplementedError
+ def clean_body(self):
+ return self.body
- def get_data(self):
- raise NotImplementedError
+ def clean_uri(self):
+ return None
- def get_uri(self):
- return str(self.uri).strip('/')
-
- def get_resource(self):
- return getattr(self, 'resource', '')
+ @property
+ def resource(self):
+ return self.resource
def __getattr__(self, name):
return self.args.get(name)
def __str__(self):
- return self.get_uri()
+ return self.populate_uri()
+
+ def populate_uri(self):
+ try:
+ populated_uri = self.uri.format(**self.args)
+ except KeyError:
+ raise ValidationError(
+ "'%s' request wasn't be able to populate the uri '%s' with "
+ "'%s' args" % (self.__class__.__name__, self.uri, self.args))
+ return str(populated_uri).strip('/')
- def _parse_simple_dict(self, to_parse):
- if not hasattr(to_parse, 'items'):
- raise RequestValidationError("'%s' needs a data dictionary"
- % self.__class__.__name__)
- update_params = {
- valid_key: to_parse[valid_key]
- for valid_key in self.valid
- if valid_key in to_parse}
- return update_params
+ def get_body(self):
+ return self.body.dumps()
class Factory(object):
@@ -69,29 +86,19 @@ class Factory(object):
import_pattern = re.compile(r'^(\w+\.)+\w+$')
- def __init__(self):
- """ """
- self.args = {}
-
- def config_with(self, **kwargs):
- self.args = kwargs
-
- def clear_config(self):
- self.args = {}
-
def __validate(func):
""" """
- def wrapper(self, request_uri):
+ def wrapper(self, request_uri, **kwargs):
if not Factory.import_pattern.match(request_uri):
- raise RequestUriInvalid("'%s' isn't valid form" % request_uri)
- return func(self, request_uri.lower())
+ raise UriInvalid("'%s' isn't valid form" % request_uri)
+ return func(self, request_uri.lower(), **kwargs)
return wrapper
def __dispatch(func):
""" """
- def wrapper(self, request_uri):
+ def wrapper(self, request_uri, **kwargs):
module_chunk, s, request_chunk = request_uri.rpartition('.')
try:
# TODO: CamelCase and under_score support, now only Class Name
@@ -99,19 +106,18 @@ class Factory(object):
% (ABS_IMPORT_PREFIX, module_chunk))
request = getattr(module, request_chunk.capitalize())
except ImportError:
- raise RequestNotFound("'%s' module does not exists"
+ raise DoesNotExists("'%s' module does not exists"
% module_chunk)
except AttributeError:
- raise RequestNotFound(
+ raise DoesNotExists(
"'%s' request doesn't exists into '%s' module"
% (request_chunk.capitalize(), module_chunk))
- return func(self, request)
+ return func(self, request, **kwargs)
return wrapper
@__validate
@__dispatch
- def __call__(self, request=''):
- request = request(self.args)
- self.clear_config()
+ def __call__(self, request='', **kwargs):
+ request = request(kwargs)
assert isinstance(request, Request)
return request