mirror of
https://github.com/khornberg/octokit.py
synced 2026-05-20 20:04:47 +03:00
Enhancement: method calls return self with response attributes
This commit is contained in:
+14
-1
@@ -151,8 +151,21 @@ class Octokit(Base):
|
||||
url, data_kwargs = self._form_url(kwargs, definition['url'])
|
||||
requests_kwargs.update(self._data(data_kwargs, definition.get('params'), method))
|
||||
requests_kwargs.update(self._auth(requests_kwargs))
|
||||
return getattr(requests, method)(url, **requests_kwargs)
|
||||
_response = getattr(requests, method)(url, **requests_kwargs)
|
||||
attributes = _response.json()
|
||||
setattr(self, '_response', _response)
|
||||
setattr(self, 'json', attributes)
|
||||
setattr(self, 'response', self._convert_to_object(attributes))
|
||||
return self
|
||||
|
||||
_api_call.__name__ = name
|
||||
_api_call.__doc__ = definition['description']
|
||||
return _api_call
|
||||
|
||||
def _convert_to_object(self, item):
|
||||
if isinstance(item, dict):
|
||||
return type('ResponseData', (object, ), {k: self._convert_to_object(v) for k, v in item.items()})
|
||||
if isinstance(item, list):
|
||||
return list((self._convert_to_object(value) for index, value in enumerate(item)))
|
||||
else:
|
||||
return item
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
from collections import namedtuple
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
@@ -82,3 +83,68 @@ class TestClientMethods(object):
|
||||
requests.get.assert_called_once_with(
|
||||
'https://api.github.com/applications/grants/404', params=params, headers=Octokit().headers
|
||||
)
|
||||
|
||||
def test_returned_object_is_self(self, mocker):
|
||||
mocker.patch('requests.patch')
|
||||
headers = {'accept': 'application/vnd.github.squirrel-girl-preview', 'Content-Type': 'application/json'}
|
||||
data = {'state': 'open'}
|
||||
sut = Octokit().issues.edit(owner='testUser', repo='testRepo', number=1)
|
||||
requests.patch.assert_called_once_with(
|
||||
'https://api.github.com/repos/testUser/testRepo/issues/1', data=json.dumps(data), headers=headers
|
||||
)
|
||||
assert sut.__class__.__name__ == 'Octokit'
|
||||
|
||||
def test_returned_object_has_requests_response_object(self, mocker):
|
||||
patch = mocker.patch('requests.patch')
|
||||
Response = namedtuple('Response', ['json'])
|
||||
patch.return_value = Response(json=lambda: {})
|
||||
sut = Octokit().issues.edit(owner='testUser', repo='testRepo', number=1)
|
||||
assert sut._response.__class__.__name__ == 'Response'
|
||||
|
||||
def test_returned_object_has_json_attribute(self, mocker):
|
||||
patch = mocker.patch('requests.patch')
|
||||
Request = namedtuple('Request', ['json'])
|
||||
patch.return_value = Request(json=lambda: data)
|
||||
data = {'documentation_url': 'https://developer.github.com/v3', 'message': 'Not Found'}
|
||||
sut = Octokit().issues.edit(owner='testUser', repo='testRepo', number=1)
|
||||
assert sut.json == data
|
||||
|
||||
def test_returned_object_has_response_attributes(self, mocker):
|
||||
patch = mocker.patch('requests.patch')
|
||||
data = {
|
||||
"id": 1,
|
||||
"number": 1347,
|
||||
"state": "open",
|
||||
"title": "Found a bug",
|
||||
"user": {
|
||||
"login": "octocat",
|
||||
"site_admin": False
|
||||
},
|
||||
"labels": [{
|
||||
"id": 208045946,
|
||||
}, {
|
||||
"id": 208045947,
|
||||
}],
|
||||
}
|
||||
Request = namedtuple('Request', ['json'])
|
||||
patch.return_value = Request(json=lambda: data)
|
||||
sut = Octokit().issues.edit(owner='testUser', repo='testRepo', number=1)
|
||||
assert sut.response.id == 1
|
||||
assert sut.response.number == 1347
|
||||
assert sut.response.state == 'open'
|
||||
assert str(sut.response.user) == '<class \'octokit.ResponseData\'>'
|
||||
assert sut.response.user.login == 'octocat'
|
||||
assert sut.response.user.site_admin is False
|
||||
assert sut.response.labels[0].id == 208045946
|
||||
|
||||
def test_returned_object_is_a_list(self, mocker):
|
||||
patch = mocker.patch('requests.patch')
|
||||
data = [{
|
||||
"id": 208045946,
|
||||
}, {
|
||||
"id": 208045947,
|
||||
}]
|
||||
Request = namedtuple('Request', ['json'])
|
||||
patch.return_value = Request(json=lambda: data)
|
||||
sut = Octokit().issues.edit(owner='testUser', repo='testRepo', number=1)
|
||||
assert sut.response[0].id == 208045946
|
||||
|
||||
Reference in New Issue
Block a user