diff options
author | 2012-04-23 17:44:10 +0200 | |
---|---|---|
committer | 2012-04-23 17:44:10 +0200 | |
commit | c7c03100fd0584b759bb75d461a12f5bcd5aabba (patch) | |
tree | 16267d3fb9919933d826ddcc8aa05f04b9b0a00f /pygithub3 | |
parent | :sparkles: Release 0.3 :sparkles: (diff) | |
download | python-github3-c7c03100fd0584b759bb75d461a12f5bcd5aabba.tar.xz python-github3-c7c03100fd0584b759bb75d461a12f5bcd5aabba.zip |
Baseline Orgs API implementation
Diffstat (limited to 'pygithub3')
-rw-r--r-- | pygithub3/github.py | 9 | ||||
-rw-r--r-- | pygithub3/requests/orgs/__init__.py | 27 | ||||
-rw-r--r-- | pygithub3/resources/orgs.py | 2 | ||||
-rw-r--r-- | pygithub3/services/orgs/__init__.py | 51 | ||||
-rw-r--r-- | pygithub3/tests/services/test_orgs.py | 44 |
5 files changed, 132 insertions, 1 deletions
diff --git a/pygithub3/github.py b/pygithub3/github.py index 0b302a1..ba403e6 100644 --- a/pygithub3/github.py +++ b/pygithub3/github.py @@ -17,9 +17,11 @@ class Github(object): from pygithub3.services.users import User from pygithub3.services.repos import Repo from pygithub3.services.gists import Gist + from pygithub3.services.orgs import Org self._users = User(**config) self._repos = Repo(**config) self._gists = Gist(**config) + self._orgs = Org(**config) @property def remaining_requests(self): @@ -47,3 +49,10 @@ class Github(object): :ref:`Gists service <Gists service>` """ return self._gists + + @property + def orgs(self): + """ + :ref:`Orgs service <Orgs service>` + """ + return self._orgs diff --git a/pygithub3/requests/orgs/__init__.py b/pygithub3/requests/orgs/__init__.py new file mode 100644 index 0000000..a490a17 --- /dev/null +++ b/pygithub3/requests/orgs/__init__.py @@ -0,0 +1,27 @@ +# -*- encoding: utf-8 -*- + +from pygithub3.requests.base import Request +from pygithub3.resources.orgs import Org + + +class List(Request): + uri = 'users/{user}/orgs' + resource = Org + + def clean_uri(self): + if not self.user: + return 'user/orgs' + + +class Get(Request): + uri = 'orgs/{name}' + resource = Org + + +class Update(Request): + uri = 'orgs/{name}' + resource = Org + body_schema = { + 'schema': ('billing_email', 'company', 'email', 'location', 'name'), + 'required': (), + } diff --git a/pygithub3/resources/orgs.py b/pygithub3/resources/orgs.py index 3996172..be6769a 100644 --- a/pygithub3/resources/orgs.py +++ b/pygithub3/resources/orgs.py @@ -11,4 +11,4 @@ class Org(Resource): _dates = ('created_at', ) def __str__(self): - return '<Org (%s)>' % getattr(self, 'name', '') + return '<Org (%s)>' % getattr(self, 'login', '') diff --git a/pygithub3/services/orgs/__init__.py b/pygithub3/services/orgs/__init__.py new file mode 100644 index 0000000..28f6b16 --- /dev/null +++ b/pygithub3/services/orgs/__init__.py @@ -0,0 +1,51 @@ +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service + + +class Org(Service): + """ Consume `Orgs API <http://developer.github.com/v3/orgs>`_ """ + + def list(self, user=None): + """ Get user's orgs + + :param str user: Username + :returns: A :doc:`result` + + If you call it without user and you are authenticated, get the + authenticated user's orgs, public and private. + + If you call it with a user, get the user's public orgs. + + :: + + org_service.list('copitux') + org_service.list() + """ + request = self.request_builder('orgs.list', user=user) + return self._get_result(request) + + def get(self, name): + """ Get a single org + + :param str name: Org name + """ + request = self.request_builder('orgs.get', name=name) + return self._get(request) + + def update(self, name, data): + """ Update a single org + + :param str name: Org name + :param dict data: Input. See `github orgs doc`_ + + .. warning :: + You must be authenticated + + :: + + org_service.update(dict(company='ACME Development', + location='Timbuctoo')) + """ + request = self.request_builder('orgs.update', name=name, body=data) + return self._patch(request) diff --git a/pygithub3/tests/services/test_orgs.py b/pygithub3/tests/services/test_orgs.py new file mode 100644 index 0000000..a6416ff --- /dev/null +++ b/pygithub3/tests/services/test_orgs.py @@ -0,0 +1,44 @@ +#!/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.orgs import Org +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 TestOrgService(TestCase): + + def setUp(self): + self.org = Org() + + def test_LIST_without_user(self, request_method): + request_method.return_value = mock_response_result() + self.org.list().all() + self.assertEqual(request_method.call_args[0], ('get', _('user/orgs'))) + + def test_LIST_with_user(self, request_method): + request_method.return_value = mock_response_result() + self.org.list('octocat').all() + self.assertEqual(request_method.call_args[0], + ('get', _('users/octocat/orgs'))) + + def test_GET(self, request_method): + request_method.return_value = mock_response() + self.org.get('acme') + self.assertEqual(request_method.call_args[0], ('get', _('orgs/acme'))) + + def test_UPDATE(self, request_method): + request_method.return_value = mock_response('patch') + self.org.update('acme', {'company': 'ACME Widgets'}) + self.assertEqual(request_method.call_args[0], + ('patch', _('orgs/acme'))) |