diff options
author | 2012-02-21 01:24:06 +0100 | |
---|---|---|
committer | 2012-02-21 01:24:06 +0100 | |
commit | ce95f908d9153e7370185c4660b49f718d253f58 (patch) | |
tree | d79bb9a645332883d5d45cd1ee14fb31ed2655de /pygithub3 | |
parent | Add mimetype support to repos.commits service (diff) | |
download | python-github3-ce95f908d9153e7370185c4660b49f718d253f58.tar.xz python-github3-ce95f908d9153e7370185c4660b49f718d253f58.zip |
Repos.downloads service done
Diffstat (limited to 'pygithub3')
-rw-r--r-- | pygithub3/requests/repos/downloads.py | 34 | ||||
-rw-r--r-- | pygithub3/resources/repos.py | 12 | ||||
-rw-r--r-- | pygithub3/services/repos.py | 37 | ||||
-rw-r--r-- | pygithub3/tests/services/test_repos.py | 34 |
4 files changed, 116 insertions, 1 deletions
diff --git a/pygithub3/requests/repos/downloads.py b/pygithub3/requests/repos/downloads.py new file mode 100644 index 0000000..8a9dd91 --- /dev/null +++ b/pygithub3/requests/repos/downloads.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from . import Request + +from pygithub3.resources.repos import Download + + +class List(Request): + + uri = '/repos/{user}/{repo}/downloads' + resource = Download + + +class Get(Request): + + uri = '/repos/{user}/{repo}/downloads/{id}' + resource = Download + + +class Create(Request): + + uri = '/repos/{user}/{repo}/downloads' + resource = Download + body_schema = { + 'schema': ('name', 'size', 'description', 'content_type'), + 'required': ('name', 'size') + } + + +class Delete(Request): + + uri = '/repos/{user}/{repo}/downloads/{id}' + resource = Download diff --git a/pygithub3/resources/repos.py b/pygithub3/resources/repos.py index de55b72..865f039 100644 --- a/pygithub3/resources/repos.py +++ b/pygithub3/resources/repos.py @@ -1,6 +1,8 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- +from collections import OrderedDict + from .base import Resource from .users import User from .orgs import Org @@ -100,3 +102,13 @@ class Branch(Resource): def __str__(self): return '<Branch (%s)>' % getattr(self, 'name', '') + + +class Download(Resource): + + def ball_to_upload(self): + return OrderedDict({ + 'key': self.path, 'acl': self.acl, 'success_action_status': '201', + 'Filename': self.name, 'AWSAccessKeyId': self.accesskeyid, + 'Policy': self.policy, 'Signature': self.signature, + 'Content-Type': self.mime_type}) diff --git a/pygithub3/services/repos.py b/pygithub3/services/repos.py index d9de000..d88338f 100644 --- a/pygithub3/services/repos.py +++ b/pygithub3/services/repos.py @@ -1,9 +1,43 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- +import requests + from .base import Service, MimeTypeMixin +class Downloads(Service): + + def list(self, user=None, repo=None): + request = self.make_request('repos.downloads.list', + user=user, repo=repo) + return self._get_result(request) + + def get(self, id, user=None, repo=None): + request = self.make_request('repos.downloads.get', + id=id, user=user, repo=repo) + return self._get(request) + + def create(self, data, user=None, repo=None): + request = self.make_request('repos.downloads.create', + body=data, user=user, repo=repo) + download = self._post(request) + + # TODO: improve it. e.g Manage all with file desc + def upload(file_path): + body = download.ball_to_upload() + body['file'] = (file_path, open(file_path, 'rb')) + return requests.post(download.s3_url, files=body) + + download.upload = upload + return download + + def delete(self, id=id, user=None, repo=None): + request = self.make_request('repos.downloads.delete', + id=id, user=user, repo=repo) + self._delete(request) + + class Commits(Service, MimeTypeMixin): """ TODO: Pagination structure differs from usual @@ -47,6 +81,7 @@ class Commits(Service, MimeTypeMixin): comment_id=cid, user=user, repo=repo) self._delete(request) + class Collaborator(Service): def list(self, user=None, repo=None): @@ -74,6 +109,8 @@ class Repo(Service): def __init__(self, **config): self.collaborators = Collaborator(**config) + self.commits = Commits(**config) + self.downloads = Downloads(**config) super(Repo, self).__init__(**config) def list(self, user=None, type='all'): diff --git a/pygithub3/tests/services/test_repos.py b/pygithub3/tests/services/test_repos.py index 963e12a..615f883 100644 --- a/pygithub3/tests/services/test_repos.py +++ b/pygithub3/tests/services/test_repos.py @@ -6,7 +6,7 @@ from unittest import TestCase import requests from mock import patch, Mock -from pygithub3.services.repos import Repo, Collaborator, Commits +from pygithub3.services.repos import Repo, Collaborator, Commits, Downloads from pygithub3.resources.base import json from pygithub3.tests.utils.base import (mock_response, mock_response_result, mock_json) @@ -236,3 +236,35 @@ class TestCommitsService(TestCase): self.cs.delete_comment(1) self.assertEqual(request_method.call_args[0], ('delete', _('repos/oct/re_oct/comments/1'))) + + +@patch.object(requests.sessions.Session, 'request') +class TestDownloadsService(TestCase): + + def setUp(self): + self.ds = Downloads(user='oct', repo='re_oct') + + def test_LIST(self, request_method): + request_method.return_value = mock_response_result() + self.ds.list().all() + self.assertEqual(request_method.call_args[0], + ('get', _('repos/oct/re_oct/downloads'))) + + def test_GET(self, request_method): + request_method.return_value = mock_response() + self.ds.get(1) + self.assertEqual(request_method.call_args[0], + ('get', _('repos/oct/re_oct/downloads/1'))) + + def test_DELETE(self, request_method): + request_method.return_value = mock_response('delete') + self.ds.delete(1) + self.assertEqual(request_method.call_args[0], + ('delete', _('repos/oct/re_oct/downloads/1'))) + + def test_CREATE(self, request_method): + request_method.return_value = mock_response('post') + download = self.ds.create({'name': 'some', 'size': 100}) + self.assertEqual(request_method.call_args[0], + ('post', _('repos/oct/re_oct/downloads'))) + self.assertTrue(hasattr(download, 'upload')) |