aboutsummaryrefslogtreecommitdiffstats
path: root/pygithub3/requests
diff options
context:
space:
mode:
Diffstat (limited to 'pygithub3/requests')
-rw-r--r--pygithub3/requests/base.py91
-rw-r--r--pygithub3/requests/git_data/__init__.py0
-rw-r--r--pygithub3/requests/git_data/blobs.py18
-rw-r--r--pygithub3/requests/git_data/commits.py18
-rw-r--r--pygithub3/requests/git_data/references.py33
-rw-r--r--pygithub3/requests/git_data/tags.py15
-rw-r--r--pygithub3/requests/git_data/trees.py16
-rw-r--r--pygithub3/requests/pull_requests/__init__.py68
-rw-r--r--pygithub3/requests/pull_requests/comments.py46
9 files changed, 254 insertions, 51 deletions
diff --git a/pygithub3/requests/base.py b/pygithub3/requests/base.py
index 03b0f8a..c4fe5cc 100644
--- a/pygithub3/requests/base.py
+++ b/pygithub3/requests/base.py
@@ -2,20 +2,17 @@
# -*- encoding: utf-8 -*-
import re
-try:
- import simplejson as json
-except ImportError:
- import json
-from pygithub3.exceptions import (DoesNotExists, UriInvalid, ValidationError,
- InvalidBodySchema)
+from pygithub3.core.utils import import_module, json
+from pygithub3.exceptions import (RequestDoesNotExist, UriInvalid,
+ ValidationError, InvalidBodySchema)
from pygithub3.resources.base import Raw
-from pygithub3.core.utils import import_module
ABS_IMPORT_PREFIX = 'pygithub3.requests'
class Body(object):
+ """ Input's request handler """
def __init__(self, content, schema, required):
self.content = content
@@ -28,6 +25,7 @@ class Body(object):
return json.dumps(self.parse())
def parse(self):
+ """ Parse body with schema-required rules """
if not hasattr(self.content, 'items'):
raise ValidationError("'%s' needs a content dictionary"
% self.__class__.__name__)
@@ -44,21 +42,34 @@ class Body(object):
class Request(object):
- """ """
uri = ''
resource = Raw
body_schema = {}
def __init__(self, **kwargs):
- """ """
- self.body = kwargs.pop('body', None)
+ self.body = kwargs.pop('body', {})
self.args = kwargs
self.clean()
+ def __getattr__(self, name):
+ return self.args.get(name)
+
+ def __str__(self):
+ 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 clean(self):
self.uri = self.clean_uri() or self.uri
- self.body = Body(self.clean_body(), **self.clean_valid_body())
+ self.body = Body(self.clean_body(), **self._clean_valid_body())
def clean_body(self):
return self.body
@@ -66,7 +77,7 @@ class Request(object):
def clean_uri(self):
return None
- def clean_valid_body(self):
+ def _clean_valid_body(self):
schema = set(self.body_schema.get('schema', ()))
required = set(self.body_schema.get('required', ()))
if not required.issubset(schema):
@@ -76,21 +87,6 @@ class Request(object):
self.__class__.__name__, required, schema))
return dict(schema=schema, required=required)
- def __getattr__(self, name):
- return self.args.get(name)
-
- def __str__(self):
- 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 get_body(self):
return self.body.dumps()
@@ -110,28 +106,21 @@ class Factory(object):
return func(self, request_uri.lower(), **kwargs)
return wrapper
- def dispatch(func):
- def wrapper(self, request_uri, **kwargs):
- module_chunk, s, request_chunk = request_uri.rpartition('.')
- request_chunk = request_chunk.capitalize()
- try:
- # TODO: CamelCase and under_score support, now only Class Name
- module = import_module('%s.%s'
- % (ABS_IMPORT_PREFIX, module_chunk))
- request = getattr(module, request_chunk)
- except ImportError:
- raise DoesNotExists("'%s' module does not exists"
- % module_chunk)
- except AttributeError:
- raise DoesNotExists(
- "'%s' request doesn't exists into '%s' module"
- % (request_chunk, module_chunk))
- return func(self, request, **kwargs)
- return wrapper
-
@validate
- @dispatch
- def __call__(self, request='', **kwargs):
- request = request(**kwargs)
- assert isinstance(request, Request)
- return request
+ def __call__(self, request_uri, **kwargs):
+ module_chunk, s, request_chunk = request_uri.rpartition('.')
+ request_chunk = request_chunk.capitalize()
+ try:
+ # TODO: CamelCase and under_score support, now only Class Name
+ module = import_module('%s.%s' % (ABS_IMPORT_PREFIX, module_chunk))
+ request_class = getattr(module, request_chunk)
+ request = request_class(**kwargs)
+ assert isinstance(request, Request)
+ return request
+ except ImportError:
+ raise RequestDoesNotExist("'%s' module does not exist"
+ % module_chunk)
+ except AttributeError:
+ raise RequestDoesNotExist("'%s' request does not exist in "
+ "'%s' module" % (request_chunk,
+ module_chunk))
diff --git a/pygithub3/requests/git_data/__init__.py b/pygithub3/requests/git_data/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/pygithub3/requests/git_data/__init__.py
diff --git a/pygithub3/requests/git_data/blobs.py b/pygithub3/requests/git_data/blobs.py
new file mode 100644
index 0000000..a4bddd6
--- /dev/null
+++ b/pygithub3/requests/git_data/blobs.py
@@ -0,0 +1,18 @@
+# -*- encoding: utf-8 -*-
+
+from pygithub3.requests.base import Request
+from pygithub3.resources.git_data import Blob
+
+
+class Get(Request):
+ uri = 'repos/{user}/{repo}/git/blobs/{sha}'
+ resource = Blob
+
+
+class Create(Request):
+ uri = 'repos/{user}/{repo}/git/blobs'
+ resource = Blob
+ body_schema = {
+ 'schema': ('content', 'encoding'),
+ 'required': ('content', 'encoding'), # TODO: is enc really required?
+ }
diff --git a/pygithub3/requests/git_data/commits.py b/pygithub3/requests/git_data/commits.py
new file mode 100644
index 0000000..a26e07d
--- /dev/null
+++ b/pygithub3/requests/git_data/commits.py
@@ -0,0 +1,18 @@
+# -*- encoding: utf-8 -*-
+
+from pygithub3.requests.base import Request
+from pygithub3.resources.repos import Commit
+
+
+class Get(Request):
+ uri = 'repos/{user}/{repo}/git/commits/{sha}'
+ resource = Commit
+
+
+class Create(Request):
+ uri = 'repos/{user}/{repo}/git/commits'
+ resource = Commit
+ body_schema = {
+ 'schema': ('message', 'tree', 'parents', 'author', 'committer'),
+ 'required': ('message', 'tree', 'parents'),
+ }
diff --git a/pygithub3/requests/git_data/references.py b/pygithub3/requests/git_data/references.py
new file mode 100644
index 0000000..2aac6a4
--- /dev/null
+++ b/pygithub3/requests/git_data/references.py
@@ -0,0 +1,33 @@
+from pygithub3.requests.base import Request
+from pygithub3.resources.git_data import Reference
+
+
+class Get(Request):
+ uri = 'repos/{user}/{repo}/git/refs/{ref}'
+ resource = Reference
+
+
+class List(Request):
+ uri = 'repos/{user}/{repo}/git/refs/{namespace}'
+ resource = Reference
+
+
+class Create(Request):
+ uri = 'repos/{user}/{repo}/git/refs'
+ resource = Reference
+ body_schema = {
+ 'schema': ('ref', 'sha'),
+ 'required': ('ref', 'sha'),
+ }
+
+class Update(Request):
+ uri = 'repos/{user}/{repo}/git/refs/{ref}'
+ resource = Reference
+ body_schema = {
+ 'schema': ('sha', 'force'),
+ 'required': ('sha',),
+ }
+
+
+class Delete(Request):
+ uri = 'repos/{user}/{repo}/git/refs/{ref}'
diff --git a/pygithub3/requests/git_data/tags.py b/pygithub3/requests/git_data/tags.py
new file mode 100644
index 0000000..dbc8da4
--- /dev/null
+++ b/pygithub3/requests/git_data/tags.py
@@ -0,0 +1,15 @@
+from pygithub3.requests.base import Request
+from pygithub3.resources.git_data import Tag
+
+
+class Get(Request):
+ uri = 'repos/{user}/{repo}/git/tags/{sha}'
+ resource = Tag
+
+class Create(Request):
+ uri = 'repos/{user}/{repo}/git/tags'
+ resource = Tag
+ body_schema = {
+ 'schema': ('tag', 'message', 'object', 'type', 'tagger'),
+ 'required': ('type',),
+ }
diff --git a/pygithub3/requests/git_data/trees.py b/pygithub3/requests/git_data/trees.py
new file mode 100644
index 0000000..11d6409
--- /dev/null
+++ b/pygithub3/requests/git_data/trees.py
@@ -0,0 +1,16 @@
+from pygithub3.requests.base import Request
+from pygithub3.resources.git_data import Tree
+
+
+class Get(Request):
+ uri = 'repos/{user}/{repo}/git/trees/{sha}'
+ resource = Tree
+
+
+class Create(Request):
+ uri = 'repos/{user}/{repo}/git/trees'
+ resource = Tree
+ body_schema = {
+ 'schema': ('tree', 'base_tree'),
+ 'required': ('tree',),
+ }
diff --git a/pygithub3/requests/pull_requests/__init__.py b/pygithub3/requests/pull_requests/__init__.py
new file mode 100644
index 0000000..f25572d
--- /dev/null
+++ b/pygithub3/requests/pull_requests/__init__.py
@@ -0,0 +1,68 @@
+# -*- encoding: utf-8 -*-
+
+from pygithub3.requests.base import Request, ValidationError
+from pygithub3.resources.pull_requests import PullRequest, File
+from pygithub3.resources.repos import Commit
+
+
+class List(Request):
+ uri = 'repos/{user}/{repo}/pulls'
+ resource = PullRequest
+
+
+class Get(Request):
+ uri = 'repos/{user}/{repo}/pulls/{number}'
+ resource = PullRequest
+
+
+class Create(Request):
+ uri = 'repos/{user}/{repo}/pulls'
+ resource = PullRequest
+ body_schema = {
+ 'schema': ('title', 'body', 'base', 'head', 'issue'),
+ 'required': ('base', 'head'),
+ }
+
+ def clean_body(self):
+ if (not ('title' in self.body and 'body' in self.body) and
+ not 'issue' in self.body):
+ raise ValidationError('pull request creation requires either an '
+ 'issue number or a title and body')
+ return self.body
+
+class Update(Request):
+ uri = 'repos/{user}/{repo}/pulls/{number}'
+ resource = PullRequest
+ body_schema = {
+ 'schema': ('title', 'body', 'state'),
+ 'required': (),
+ }
+
+ def clean_body(self):
+ if ('state' in self.body and
+ self.body['state'] not in ['open', 'closed']):
+ raise ValidationError('If a state is specified, it must be one '
+ 'of "open" or "closed"')
+ return self.body
+
+
+class List_commits(Request):
+ uri = 'repos/{user}/{repo}/pulls/{number}/commits'
+ resource = Commit
+
+
+class List_files(Request):
+ uri = 'repos/{user}/{repo}/pulls/{number}/files'
+ resource = File
+
+
+class Is_merged(Request):
+ uri = 'repos/{user}/{repo}/pulls/{number}/merge'
+
+
+class Merge(Request):
+ uri = 'repos/{user}/{repo}/pulls/{number}/merge'
+ body_schema = {
+ 'schema': ('commit_message',),
+ 'required': (),
+ }
diff --git a/pygithub3/requests/pull_requests/comments.py b/pygithub3/requests/pull_requests/comments.py
new file mode 100644
index 0000000..1904f6c
--- /dev/null
+++ b/pygithub3/requests/pull_requests/comments.py
@@ -0,0 +1,46 @@
+# -*- encoding: utf-8 -*-
+
+from pygithub3.requests.base import Request, ValidationError
+from pygithub3.resources.pull_requests import Comment
+
+
+class List(Request):
+ uri = 'repos/{user}/{repo}/pulls/{number}/comments'
+ resource = Comment
+
+
+class Get(Request):
+ uri = 'repos/{user}/{repo}/pulls/comments/{number}'
+ resource = Comment
+
+
+class Create(Request):
+ uri = 'repos/{user}/{repo}/pulls/{number}/comments'
+ resource = Comment
+ body_schema = {
+ 'schema': ('body', 'commit_id', 'path', 'position', 'in_reply_to'),
+ 'required': ('body',),
+ }
+
+ def clean_body(self):
+ if (not ('commit_id' in self.body and
+ 'path' in self.body and
+ 'position' in self.body) and
+ not 'in_reply_to' in self.body):
+ raise ValidationError('supply either in_reply_to or commit_id, '
+ 'path, and position')
+ return self.body
+
+
+class Update(Request):
+ uri = 'repos/{user}/{repo}/pulls/comments/{number}'
+ resource = Comment
+ body_schema = {
+ 'schema': ('body',),
+ 'required': ('body',),
+ }
+
+
+class Delete(Request):
+ uri = 'repos/{user}/{repo}/pulls/comments/{number}'
+ resource = Comment