Skip to content
This repository was archived by the owner on Dec 17, 2019. It is now read-only.

Commit 30033cb

Browse files
committed
services.gists.Comment done
1 parent 3a52231 commit 30033cb

File tree

5 files changed

+152
-1
lines changed

5 files changed

+152
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- encoding: utf-8 -*-
2+
3+
from pygithub3.requests.base import Request
4+
from pygithub3.resources.gists import Comment
5+
6+
class List(Request):
7+
8+
uri = 'gists/{gist_id}/comments'
9+
resource = Comment
10+
11+
12+
class Get(Request):
13+
14+
uri = 'gists/comments/{id}'
15+
resource = Comment
16+
17+
18+
class Create(Request):
19+
20+
uri = 'gists/{gist_id}/comments'
21+
resource = Comment
22+
body_schema = {
23+
'schema': ('body', ),
24+
'required': ('body', )
25+
}
26+
27+
28+
class Update(Request):
29+
30+
uri = 'gists/comments/{id}'
31+
resource = Comment
32+
body_schema = {
33+
'schema': ('body', ),
34+
'required': ('body', )
35+
}
36+
37+
38+
class Delete(Request):
39+
40+
uri = 'gists/comments/1'

pygithub3/resources/gists.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ class Gist(Resource):
3535

3636
def __str__(self):
3737
return '<Gist (%s)>' % getattr(self, 'description', '')
38+
39+
class Comment(Resource):
40+
41+
_dates = ('created_at', )
42+
_maps = {'user': User}
43+
44+
def __str__(self):
45+
return '<GistComment (%s)>' % getattr(self, 'user', '')

pygithub3/services/gists/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
# -*- encoding: utf-8 -*-
33

44
from pygithub3.services.base import Service
5+
from comments import Comments
56

67

78
class Gist(Service):
89
""" Consume `Gists API <http://developer.github.com/v3/gists>`_ """
910

1011
def __init__(self, **config):
12+
self.comments = Comments(**config)
1113
super(Gist, self).__init__(**config)
1214

1315
def list(self, user=None):
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
from pygithub3.services.base import Service, MimeTypeMixin
5+
6+
7+
class Comments(Service, MimeTypeMixin):
8+
""" Consume `Comments API
9+
<http://developer.github.com/v3/gists/comments>`_
10+
11+
.. note::
12+
This service support :ref:`mimetypes-section` configuration
13+
"""
14+
15+
def list(self, gist_id):
16+
""" Get gist's comments
17+
18+
:param int gist_id: Gist id
19+
:returns: A :doc:`result`
20+
"""
21+
request = self.request_builder('gists.comments.list', gist_id=gist_id)
22+
return self._get_result(request, **self._get_mimetype_as_header())
23+
24+
def get(self, id):
25+
""" Get a single comment
26+
27+
:param int id: Comment id
28+
"""
29+
request = self.request_builder('gists.comments.get', id=id)
30+
return self._get(request, **self._get_mimetype_as_header())
31+
32+
def create(self, gist_id, message):
33+
""" Create a comment
34+
35+
:param int gist_id: Gist id
36+
:param str message: Comment's message
37+
38+
.. warning::
39+
You must be authenticated
40+
41+
::
42+
comment_service.create(1, 'comment')
43+
"""
44+
request = self.request_builder('gists.comments.create',
45+
gist_id=gist_id, body={'body': message})
46+
return self._post(request, **self._get_mimetype_as_header())
47+
48+
def update(self, id, message):
49+
""" Update a comment
50+
51+
:param int id: Comment id
52+
:param str message: Comment's message
53+
"""
54+
request = self.request_builder('gists.comments.update', id=id,
55+
body={'body': message})
56+
return self._patch(request, **self._get_mimetype_as_header())
57+
58+
def delete(self, id):
59+
""" Delete a comment
60+
61+
:param int id: Comment id
62+
"""
63+
request = self.request_builder('gists.comments.delete', id=id)
64+
self._delete(request)

pygithub3/tests/services/test_gists.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from pygithub3.tests.utils.core import TestCase
88
from pygithub3.resources.base import json
9-
from pygithub3.services.gists import Gist
9+
from pygithub3.services.gists import Gist, Comments
1010
from pygithub3.tests.utils.base import (mock_response, mock_response_result,
1111
mock_json)
1212
from pygithub3.tests.utils.services import _
@@ -91,3 +91,40 @@ def test_DELETE(self, request_method):
9191
self.gs.delete(1)
9292
self.assertEqual(request_method.call_args[0],
9393
('delete', _('gists/1')))
94+
95+
96+
@patch.object(requests.sessions.Session, 'request')
97+
class TestCommentService(TestCase):
98+
99+
def setUp(self):
100+
self.cs = Comments()
101+
102+
def test_LIST(self, request_method):
103+
request_method.return_value = mock_response_result()
104+
self.cs.list(1).all()
105+
self.assertEqual(request_method.call_args[0],
106+
('get', _('gists/1/comments')))
107+
108+
def test_GET(self, request_method):
109+
request_method.return_value = mock_response()
110+
self.cs.get(1)
111+
self.assertEqual(request_method.call_args[0],
112+
('get', _('gists/comments/1')))
113+
114+
def test_CREATE(self, request_method):
115+
request_method.return_value = mock_response('post')
116+
self.cs.create(1, dict(body='comment'))
117+
self.assertEqual(request_method.call_args[0],
118+
('post', _('gists/1/comments')))
119+
120+
def test_UPDATE(self, request_method):
121+
request_method.return_value = mock_response('patch')
122+
self.cs.update(1, dict(body='new_comment'))
123+
self.assertEqual(request_method.call_args[0],
124+
('patch', _('gists/comments/1')))
125+
126+
def test_DELETE(self, request_method):
127+
request_method.return_value = mock_response('delete')
128+
self.cs.delete(1)
129+
self.assertEqual(request_method.call_args[0],
130+
('delete', _('gists/comments/1')))

0 commit comments

Comments
 (0)