From 3a52231c943bc59b1ddc8800f96179cbe0d1523d Mon Sep 17 00:00:00 2001 From: David Medina Date: Tue, 3 Apr 2012 12:37:18 +0200 Subject: services.gists.Gist done --- pygithub3/services/gists/__init__.py | 132 +++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 pygithub3/services/gists/__init__.py (limited to 'pygithub3/services/gists') diff --git a/pygithub3/services/gists/__init__.py b/pygithub3/services/gists/__init__.py new file mode 100644 index 0000000..b983fea --- /dev/null +++ b/pygithub3/services/gists/__init__.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service + + +class Gist(Service): + """ Consume `Gists API `_ """ + + def __init__(self, **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 + """ + request = self.request_builder('gists.star', id=id) + self._put(request) + + def unstar(self, id): + """ Unstar a gist + + :param int id: Gist id + """ + 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 + """ + 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 + """ + + request = self.request_builder('gists.fork', id=id) + return self._post(request) + + def delete(self, id): + """ Delete a gist + + :param int id: Gist id + """ + request = self.request_builder('gists.delete', id=id) + return self._delete(request) -- cgit v1.2.3-59-g8ed1b From 30033cb33d828d96492c429407977a9f7912bf28 Mon Sep 17 00:00:00 2001 From: David Medina Date: Tue, 3 Apr 2012 19:06:02 +0200 Subject: services.gists.Comment done --- pygithub3/requests/gists/comments.py | 40 +++++++++++++++++++++ pygithub3/resources/gists.py | 8 +++++ pygithub3/services/gists/__init__.py | 2 ++ pygithub3/services/gists/comments.py | 64 ++++++++++++++++++++++++++++++++++ pygithub3/tests/services/test_gists.py | 39 ++++++++++++++++++++- 5 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 pygithub3/requests/gists/comments.py create mode 100644 pygithub3/services/gists/comments.py (limited to 'pygithub3/services/gists') diff --git a/pygithub3/requests/gists/comments.py b/pygithub3/requests/gists/comments.py new file mode 100644 index 0000000..32f01ec --- /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/1' diff --git a/pygithub3/resources/gists.py b/pygithub3/resources/gists.py index 8345f84..7e9550a 100644 --- a/pygithub3/resources/gists.py +++ b/pygithub3/resources/gists.py @@ -35,3 +35,11 @@ class Gist(Resource): def __str__(self): return '' % getattr(self, 'description', '') + +class Comment(Resource): + + _dates = ('created_at', ) + _maps = {'user': User} + + def __str__(self): + return '' % getattr(self, 'user', '') diff --git a/pygithub3/services/gists/__init__.py b/pygithub3/services/gists/__init__.py index b983fea..5fbfe7a 100644 --- a/pygithub3/services/gists/__init__.py +++ b/pygithub3/services/gists/__init__.py @@ -2,12 +2,14 @@ # -*- encoding: utf-8 -*- from pygithub3.services.base import Service +from comments import Comments class Gist(Service): """ Consume `Gists API `_ """ def __init__(self, **config): + self.comments = Comments(**config) super(Gist, self).__init__(**config) def list(self, user=None): diff --git a/pygithub3/services/gists/comments.py b/pygithub3/services/gists/comments.py new file mode 100644 index 0000000..397d583 --- /dev/null +++ b/pygithub3/services/gists/comments.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service, MimeTypeMixin + + +class Comments(Service, MimeTypeMixin): + """ Consume `Comments API + `_ + + .. 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 + """ + 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 + """ + request = self.request_builder('gists.comments.delete', id=id) + self._delete(request) diff --git a/pygithub3/tests/services/test_gists.py b/pygithub3/tests/services/test_gists.py index 66ff537..8cf93ed 100644 --- a/pygithub3/tests/services/test_gists.py +++ b/pygithub3/tests/services/test_gists.py @@ -6,7 +6,7 @@ from mock import patch, Mock from pygithub3.tests.utils.core import TestCase from pygithub3.resources.base import json -from pygithub3.services.gists import Gist +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 _ @@ -91,3 +91,40 @@ class TestGistService(TestCase): 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'))) -- cgit v1.2.3-59-g8ed1b From 37e871617148bfde801a499699bbf2205125d830 Mon Sep 17 00:00:00 2001 From: David Medina Date: Tue, 3 Apr 2012 23:18:52 +0200 Subject: Gists services doc --- docs/gists.rst | 37 ++++++++++++++++++++++++++++++++++++ docs/services.rst | 1 + pygithub3/services/gists/__init__.py | 20 +++++++++++++++++++ pygithub3/services/gists/comments.py | 7 +++++++ 4 files changed, 65 insertions(+) create mode 100644 docs/gists.rst (limited to 'pygithub3/services/gists') diff --git a/docs/gists.rst b/docs/gists.rst new file mode 100644 index 0000000..3b2915f --- /dev/null +++ b/docs/gists.rst @@ -0,0 +1,37 @@ +.. _Gists service: + +Gists services +=============== + +**Fast sample**:: + + from pygithub3 import Github + + auth = dict(login='octocat', password='pass') + gh = Github(**auth) + + octocat_gists = gh.gists.list() + the_first_gist = gh.gists.get(1) + + the_first_gist_comments = gh.gists.comments.list(1) + +Gist +----- + +.. autoclass:: pygithub3.services.gists.Gist + :members: + + .. attribute:: comments + + :ref:`Comments service` + +.. _Comments service: + +Comments +---------- + +.. autoclass:: pygithub3.services.gists.Comments + :members: + +.. _github gists doc: http://developer.github.com/v3/gists +.. _github comments doc: http://developer.github.com/v3/gists/comments diff --git a/docs/services.rst b/docs/services.rst index a0fd894..e686a6e 100644 --- a/docs/services.rst +++ b/docs/services.rst @@ -61,5 +61,6 @@ List of services users repos + gists .. _mimetypes: http://developer.github.com/v3/mime diff --git a/pygithub3/services/gists/__init__.py b/pygithub3/services/gists/__init__.py index 5fbfe7a..aadb136 100644 --- a/pygithub3/services/gists/__init__.py +++ b/pygithub3/services/gists/__init__.py @@ -96,6 +96,10 @@ class Gist(Service): """ Star a gist :param int id: Gist id + + .. warning :: + You must be authenticated + """ request = self.request_builder('gists.star', id=id) self._put(request) @@ -104,6 +108,10 @@ class Gist(Service): """ 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) @@ -112,6 +120,10 @@ class Gist(Service): """ 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) @@ -120,6 +132,10 @@ class Gist(Service): """ Fork a gist :param int id: Gist id + + .. warning :: + You must be authenticated + """ request = self.request_builder('gists.fork', id=id) @@ -129,6 +145,10 @@ class Gist(Service): """ 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 index 397d583..7f4955f 100644 --- a/pygithub3/services/gists/comments.py +++ b/pygithub3/services/gists/comments.py @@ -39,6 +39,7 @@ class Comments(Service, MimeTypeMixin): You must be authenticated :: + comment_service.create(1, 'comment') """ request = self.request_builder('gists.comments.create', @@ -50,6 +51,9 @@ class Comments(Service, MimeTypeMixin): :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}) @@ -59,6 +63,9 @@ class Comments(Service, MimeTypeMixin): """ 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) -- cgit v1.2.3-59-g8ed1b