aboutsummaryrefslogtreecommitdiffstats
path: root/pygithub3
diff options
context:
space:
mode:
Diffstat (limited to 'pygithub3')
-rw-r--r--pygithub3/github.py16
-rw-r--r--pygithub3/requests/base.py2
-rw-r--r--pygithub3/requests/gists/__init__.py77
-rw-r--r--pygithub3/requests/gists/comments.py40
-rw-r--r--pygithub3/resources/gists.py45
-rw-r--r--pygithub3/services/gists/__init__.py154
-rw-r--r--pygithub3/services/gists/comments.py71
-rw-r--r--pygithub3/services/repos/__init__.py6
-rw-r--r--pygithub3/tests/services/test_gists.py130
-rw-r--r--pygithub3/tests/services/test_repos.py4
10 files changed, 536 insertions, 9 deletions
diff --git a/pygithub3/github.py b/pygithub3/github.py
index 23b5b0b..0b302a1 100644
--- a/pygithub3/github.py
+++ b/pygithub3/github.py
@@ -2,6 +2,7 @@
# -*- encoding: utf-8 -*-
+#TODO: Move the imports out. setup related
class Github(object):
"""
You can preconfigure all services globally with a ``config`` dict. See
@@ -14,9 +15,11 @@ class Github(object):
def __init__(self, **config):
from pygithub3.services.users import User
- from pygithub3.services.repos import Repos
+ from pygithub3.services.repos import Repo
+ from pygithub3.services.gists import Gist
self._users = User(**config)
- self._repos = Repos(**config)
+ self._repos = Repo(**config)
+ self._gists = Gist(**config)
@property
def remaining_requests(self):
@@ -27,7 +30,7 @@ class Github(object):
@property
def users(self):
"""
- :ref:`User service <User service>`
+ :ref:`Users service <Users service>`
"""
return self._users
@@ -37,3 +40,10 @@ class Github(object):
:ref:`Repos service <Repos service>`
"""
return self._repos
+
+ @property
+ def gists(self):
+ """
+ :ref:`Gists service <Gists service>`
+ """
+ return self._gists
diff --git a/pygithub3/requests/base.py b/pygithub3/requests/base.py
index 65bc558..03b0f8a 100644
--- a/pygithub3/requests/base.py
+++ b/pygithub3/requests/base.py
@@ -37,7 +37,7 @@ class Body(object):
if attr_required not in parsed:
raise ValidationError("'%s' attribute is required" %
attr_required)
- if not parsed[attr_required]:
+ if parsed[attr_required] is None:
raise ValidationError("'%s' attribute can't be empty" %
attr_required)
return parsed
diff --git a/pygithub3/requests/gists/__init__.py b/pygithub3/requests/gists/__init__.py
new file mode 100644
index 0000000..4dbc6e6
--- /dev/null
+++ b/pygithub3/requests/gists/__init__.py
@@ -0,0 +1,77 @@
+# -*- encoding: utf-8 -*-
+
+from pygithub3.requests.base import Request, ValidationError
+from pygithub3.resources.gists import Gist
+
+class List(Request):
+
+ uri = 'users/{user}/gists'
+ resource = Gist
+
+ def clean_uri(self):
+ if not self.user:
+ return 'gists'
+
+
+class Public(Request):
+
+ uri = 'gists/public'
+ resource = Gist
+
+
+class Starred(Request):
+
+ uri = 'gists/starred'
+ resource = Gist
+
+
+class Get(Request):
+
+ uri = 'gists/{id}'
+ resource = Gist
+
+
+class Create(Request):
+
+ uri = 'gists'
+ resource = Gist
+ body_schema = {
+ 'schema': ('description', 'public', 'files'),
+ 'required': ('public', 'files')
+ }
+
+
+class Update(Request):
+
+ uri = 'gists/{id}'
+ resource = Gist
+ body_schema = {
+ 'schema': ('description', 'public', 'files'),
+ 'required': (),
+ }
+
+
+class Star(Request):
+
+ uri = 'gists/{id}/star'
+
+
+class Unstar(Request):
+
+ uri = 'gists/{id}/star'
+
+
+class Is_starred(Request):
+
+ uri = 'gists/{id}/star'
+
+
+class Fork(Request):
+
+ uri = 'gists/{id}/fork'
+ resource = Gist
+
+
+class Delete(Request):
+
+ uri = 'gists/{id}'
diff --git a/pygithub3/requests/gists/comments.py b/pygithub3/requests/gists/comments.py
new file mode 100644
index 0000000..8e5af15
--- /dev/null
+++ b/pygithub3/requests/gists/comments.py
@@ -0,0 +1,40 @@
+# -*- encoding: utf-8 -*-
+
+from pygithub3.requests.base import Request
+from pygithub3.resources.gists import Comment
+
+class List(Request):
+
+ uri = 'gists/{gist_id}/comments'
+ resource = Comment
+
+
+class Get(Request):
+
+ uri = 'gists/comments/{id}'
+ resource = Comment
+
+
+class Create(Request):
+
+ uri = 'gists/{gist_id}/comments'
+ resource = Comment
+ body_schema = {
+ 'schema': ('body', ),
+ 'required': ('body', )
+ }
+
+
+class Update(Request):
+
+ uri = 'gists/comments/{id}'
+ resource = Comment
+ body_schema = {
+ 'schema': ('body', ),
+ 'required': ('body', )
+ }
+
+
+class Delete(Request):
+
+ uri = 'gists/comments/{id}'
diff --git a/pygithub3/resources/gists.py b/pygithub3/resources/gists.py
new file mode 100644
index 0000000..7e9550a
--- /dev/null
+++ b/pygithub3/resources/gists.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+from .base import Resource
+from .users import User
+
+
+class File(Resource):
+
+ def __str__(self):
+ return '<GistFile (%s)>' % getattr(self, 'filename', '')
+
+
+class Fork(Resource):
+
+ _dates = ('created_at', )
+ _maps = {'user': User}
+ def __str__(self):
+ return '<GistFork>'
+
+
+class History(Resource):
+
+ _dates = ('committed_at', )
+ _maps = {'user': User}
+
+ def __str__(self):
+ return '<GistHistory (%s)>' % getattr(self, 'version', '')
+
+class Gist(Resource):
+
+ _dates = ('created_at', )
+ _maps = {'user': User}
+ _collection_maps = {'files': File, 'forks': Fork, 'history': History}
+
+ def __str__(self):
+ return '<Gist (%s)>' % getattr(self, 'description', '')
+
+class Comment(Resource):
+
+ _dates = ('created_at', )
+ _maps = {'user': User}
+
+ def __str__(self):
+ return '<GistComment (%s)>' % getattr(self, 'user', '')
diff --git a/pygithub3/services/gists/__init__.py b/pygithub3/services/gists/__init__.py
new file mode 100644
index 0000000..aadb136
--- /dev/null
+++ b/pygithub3/services/gists/__init__.py
@@ -0,0 +1,154 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+from pygithub3.services.base import Service
+from comments import Comments
+
+
+class Gist(Service):
+ """ Consume `Gists API <http://developer.github.com/v3/gists>`_ """
+
+ def __init__(self, **config):
+ self.comments = Comments(**config)
+ super(Gist, self).__init__(**config)
+
+ def list(self, user=None):
+ """ Get user's gists
+
+ :param str user: Username
+ :returns: A :doc:`result`
+
+ If you call it without user and you are authenticated, get the
+ authenticated user's gists. but if you aren't authenticated get the
+ public gists
+
+ ::
+
+ gist_service.list('copitux')
+ gist_service.list()
+ """
+ request = self.request_builder('gists.list', user=user)
+ return self._get_result(request)
+
+ def public(self):
+ """ Get public gists
+
+ :returns: A :doc:`result`
+
+ .. note ::
+ Be careful iterating the result
+ """
+ request = self.request_builder('gists.public')
+ return self._get_result(request)
+
+ def starred(self):
+ """ Get authenticated user's starred gists
+
+ :returns: A :doc:`result`
+
+ .. warning::
+ You must be authenticated
+ """
+ request = self.request_builder('gists.starred')
+ return self._get_result(request)
+
+ def get(self, id):
+ """ Get a single gist
+
+ :param int id: Gist id
+ """
+ request = self.request_builder('gists.get', id=id)
+ return self._get(request)
+
+ def create(self, data):
+ """ Create a gist
+
+ :param dict data: Input. See `github gists doc`_
+
+ ::
+
+ gist_service.create(dict(description='some gist', public=True,
+ files={'xample.py': {'content': 'import code'}}))
+ """
+ request = self.request_builder('gists.create', body=data)
+ return self._post(request)
+
+ def update(self, id, data):
+ """ Update a single gist
+
+ :param int id: Gist id
+ :param dict data: Input. See `github gists doc`_
+
+ .. warning ::
+ You must be authenticated
+
+ ::
+
+ gist_service.update(dict(description='edited',
+ files={'xample.py': {
+ 'filename': 'new_xample.py',
+ 'content': 'import new_code'}}))
+ """
+ request = self.request_builder('gists.update', id=id, body=data)
+ return self._patch(request)
+
+ def star(self, id):
+ """ Star a gist
+
+ :param int id: Gist id
+
+ .. warning ::
+ You must be authenticated
+
+ """
+ request = self.request_builder('gists.star', id=id)
+ self._put(request)
+
+ def unstar(self, id):
+ """ Unstar a gist
+
+ :param int id: Gist id
+
+ .. warning ::
+ You must be authenticated
+
+ """
+ request = self.request_builder('gists.unstar', id=id)
+ return self._delete(request)
+
+ def is_starred(self, id):
+ """ Check if a gist is starred
+
+ :param int id: Gist id
+
+ .. warning ::
+ You must be authenticated
+
+ """
+ request = self.request_builder('gists.is_starred', id=id)
+ return self._bool(request)
+
+ def fork(self, id):
+ """ Fork a gist
+
+ :param int id: Gist id
+
+ .. warning ::
+ You must be authenticated
+
+ """
+
+ request = self.request_builder('gists.fork', id=id)
+ return self._post(request)
+
+ def delete(self, id):
+ """ Delete a gist
+
+ :param int id: Gist id
+
+ .. warning ::
+ You must be authenticated
+
+ """
+ request = self.request_builder('gists.delete', id=id)
+ return self._delete(request)
diff --git a/pygithub3/services/gists/comments.py b/pygithub3/services/gists/comments.py
new file mode 100644
index 0000000..7f4955f
--- /dev/null
+++ b/pygithub3/services/gists/comments.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+from pygithub3.services.base import Service, MimeTypeMixin
+
+
+class Comments(Service, MimeTypeMixin):
+ """ Consume `Comments API
+ <http://developer.github.com/v3/gists/comments>`_
+
+ .. note::
+ This service support :ref:`mimetypes-section` configuration
+ """
+
+ def list(self, gist_id):
+ """ Get gist's comments
+
+ :param int gist_id: Gist id
+ :returns: A :doc:`result`
+ """
+ request = self.request_builder('gists.comments.list', gist_id=gist_id)
+ return self._get_result(request, **self._get_mimetype_as_header())
+
+ def get(self, id):
+ """ Get a single comment
+
+ :param int id: Comment id
+ """
+ request = self.request_builder('gists.comments.get', id=id)
+ return self._get(request, **self._get_mimetype_as_header())
+
+ def create(self, gist_id, message):
+ """ Create a comment
+
+ :param int gist_id: Gist id
+ :param str message: Comment's message
+
+ .. warning::
+ You must be authenticated
+
+ ::
+
+ comment_service.create(1, 'comment')
+ """
+ request = self.request_builder('gists.comments.create',
+ gist_id=gist_id, body={'body': message})
+ return self._post(request, **self._get_mimetype_as_header())
+
+ def update(self, id, message):
+ """ Update a comment
+
+ :param int id: Comment id
+ :param str message: Comment's message
+
+ .. warning::
+ You must be authenticated
+ """
+ request = self.request_builder('gists.comments.update', id=id,
+ body={'body': message})
+ return self._patch(request, **self._get_mimetype_as_header())
+
+ def delete(self, id):
+ """ Delete a comment
+
+ :param int id: Comment id
+
+ .. warning::
+ You must be authenticated
+ """
+ request = self.request_builder('gists.comments.delete', id=id)
+ self._delete(request)
diff --git a/pygithub3/services/repos/__init__.py b/pygithub3/services/repos/__init__.py
index 3ef1fb4..628e9d6 100644
--- a/pygithub3/services/repos/__init__.py
+++ b/pygithub3/services/repos/__init__.py
@@ -11,7 +11,7 @@ from .watchers import Watchers
from .hooks import Hooks
-class Repos(Service):
+class Repo(Service):
""" Consume `Repos API <http://developer.github.com/v3/repos>`_ """
def __init__(self, **config):
@@ -22,7 +22,7 @@ class Repos(Service):
self.keys = Keys(**config)
self.watchers = Watchers(**config)
self.hooks = Hooks(**config)
- super(Repos, self).__init__(**config)
+ super(Repo, self).__init__(**config)
def list(self, user=None, type='all'):
""" Get user's repositories
@@ -133,7 +133,7 @@ class Repos(Service):
return self.__list_contributors(user, repo)
def list_contributors_with_anonymous(self, user=None, repo=None):
- """ Like :attr:`~pygithub3.services.repos.Repos.list_contributors` plus
+ """ Like :attr:`~pygithub3.services.repos.Repo.list_contributors` plus
anonymous """
return self.__list_contributors(user, repo, anom=True)
diff --git a/pygithub3/tests/services/test_gists.py b/pygithub3/tests/services/test_gists.py
new file mode 100644
index 0000000..8cf93ed
--- /dev/null
+++ b/pygithub3/tests/services/test_gists.py
@@ -0,0 +1,130 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+import requests
+from mock import patch, Mock
+
+from pygithub3.tests.utils.core import TestCase
+from pygithub3.resources.base import json
+from pygithub3.services.gists import Gist, Comments
+from pygithub3.tests.utils.base import (mock_response, mock_response_result,
+ mock_json)
+from pygithub3.tests.utils.services import _
+
+json.dumps = Mock(side_effect=mock_json)
+json.loads = Mock(side_effect=mock_json)
+
+
+@patch.object(requests.sessions.Session, 'request')
+class TestGistService(TestCase):
+
+ def setUp(self):
+ self.gs = Gist()
+
+ def test_LIST_without_user(self, request_method):
+ request_method.return_value = mock_response_result()
+ self.gs.list().all()
+ self.assertEqual(request_method.call_args[0], ('get', _('gists')))
+
+ def test_LIST_with_user(self, request_method):
+ request_method.return_value = mock_response_result()
+ self.gs.list('octocat').all()
+ self.assertEqual(request_method.call_args[0],
+ ('get', _('users/octocat/gists')))
+
+ def test_LIST_public(self, request_method):
+ request_method.return_value = mock_response_result()
+ self.gs.public().all()
+ self.assertEqual(request_method.call_args[0],
+ ('get', _('gists/public')))
+
+ def test_LIST_starred(self, request_method):
+ request_method.return_value = mock_response_result()
+ self.gs.starred().all()
+ self.assertEqual(request_method.call_args[0],
+ ('get', _('gists/starred')))
+
+ def test_GET(self, request_method):
+ request_method.return_value = mock_response()
+ self.gs.get(1)
+ self.assertEqual(request_method.call_args[0],
+ ('get', _('gists/1')))
+
+ def test_CREATE(self, request_method):
+ request_method.return_value = mock_response('post')
+ self.gs.create(dict(public=True, files={
+ 'file1.txt': {'content': 'Some\ncontent'}}))
+ self.assertEqual(request_method.call_args[0],
+ ('post', _('gists')))
+
+ def test_UPDATE(self, request_method):
+ request_method.return_value = mock_response('patch')
+ self.gs.update(1, {'description': 'edited'})
+ self.assertEqual(request_method.call_args[0],
+ ('patch', _('gists/1')))
+
+ def test_STAR(self, request_method):
+ self.gs.star(1)
+ self.assertEqual(request_method.call_args[0],
+ ('put', _('gists/1/star')))
+
+ def test_UNSTAR(self, request_method):
+ request_method.return_value = mock_response('delete')
+ self.gs.unstar(1)
+ self.assertEqual(request_method.call_args[0],
+ ('delete', _('gists/1/star')))
+
+ def test_IS_STARRED(self, request_method):
+ request_method.return_value = mock_response()
+ self.gs.is_starred(1)
+ self.assertEqual(request_method.call_args[0],
+ ('head', _('gists/1/star')))
+
+ def test_FORK(self, request_method):
+ request_method.return_value = mock_response('post')
+ self.gs.fork(1)
+ self.assertEqual(request_method.call_args[0],
+ ('post', _('gists/1/fork')))
+
+ def test_DELETE(self, request_method):
+ request_method.return_value = mock_response('delete')
+ self.gs.delete(1)
+ self.assertEqual(request_method.call_args[0],
+ ('delete', _('gists/1')))
+
+
+@patch.object(requests.sessions.Session, 'request')
+class TestCommentService(TestCase):
+
+ def setUp(self):
+ self.cs = Comments()
+
+ def test_LIST(self, request_method):
+ request_method.return_value = mock_response_result()
+ self.cs.list(1).all()
+ self.assertEqual(request_method.call_args[0],
+ ('get', _('gists/1/comments')))
+
+ def test_GET(self, request_method):
+ request_method.return_value = mock_response()
+ self.cs.get(1)
+ self.assertEqual(request_method.call_args[0],
+ ('get', _('gists/comments/1')))
+
+ def test_CREATE(self, request_method):
+ request_method.return_value = mock_response('post')
+ self.cs.create(1, dict(body='comment'))
+ self.assertEqual(request_method.call_args[0],
+ ('post', _('gists/1/comments')))
+
+ def test_UPDATE(self, request_method):
+ request_method.return_value = mock_response('patch')
+ self.cs.update(1, dict(body='new_comment'))
+ self.assertEqual(request_method.call_args[0],
+ ('patch', _('gists/comments/1')))
+
+ def test_DELETE(self, request_method):
+ request_method.return_value = mock_response('delete')
+ self.cs.delete(1)
+ self.assertEqual(request_method.call_args[0],
+ ('delete', _('gists/comments/1')))
diff --git a/pygithub3/tests/services/test_repos.py b/pygithub3/tests/services/test_repos.py
index e77cc90..e21d474 100644
--- a/pygithub3/tests/services/test_repos.py
+++ b/pygithub3/tests/services/test_repos.py
@@ -5,7 +5,7 @@ import requests
from mock import patch, Mock
from pygithub3.tests.utils.core import TestCase
-from pygithub3.services.repos import (Repos, Collaborators, Commits, Downloads,
+from pygithub3.services.repos import (Repo, Collaborators, Commits, Downloads,
Forks, Keys, Watchers, Hooks)
from pygithub3.resources.base import json
from pygithub3.tests.utils.base import (mock_response, mock_response_result,
@@ -20,7 +20,7 @@ json.loads = Mock(side_effect=mock_json)
class TestRepoService(TestCase):
def setUp(self):
- self.rs = Repos()
+ self.rs = Repo()
self.rs.set_user('octocat')
self.rs.set_repo('octocat_repo')