diff --git a/gogs_client/__init__.py b/gogs_client/__init__.py index bfa58bb..1112302 100644 --- a/gogs_client/__init__.py +++ b/gogs_client/__init__.py @@ -1,4 +1,4 @@ from gogs_client.auth import Authentication, Token, UsernamePassword -from gogs_client.entities import GogsUser, GogsRepo, TokenInfo +from gogs_client.entities import GogsUser, GogsRepo from gogs_client.interface import GogsApi, ApiFailure, NetworkFailure from gogs_client.updates import GogsUserUpdate diff --git a/gogs_client/auth.py b/gogs_client/auth.py index ec9b712..619f2ab 100644 --- a/gogs_client/auth.py +++ b/gogs_client/auth.py @@ -1,6 +1,7 @@ """ Various classes for Gogs authentication """ +from gogs_client.entities import json_get class Authentication(object): @@ -22,11 +23,27 @@ class Token(Authentication): """ An immutable represention of a Gogs authentication token """ - def __init__(self, token): + def __init__(self, token, name=None): """ :param str token: contents of Gogs authentication token """ self._token = token + self._name = name + + @staticmethod + def from_json(parsed_json): + name = json_get(parsed_json, "name") + sha1 = json_get(parsed_json, "sha1") + return Token(sha1, name) + + @property + def name(self): + """ + The name of the token + + :rtype: str + """ + return self._name @property def token(self): diff --git a/gogs_client/entities.py b/gogs_client/entities.py index 35a275e..e4d7e5d 100644 --- a/gogs_client/entities.py +++ b/gogs_client/entities.py @@ -1,7 +1,6 @@ """ Various immutable classes that represent Gogs entities. """ -from gogs_client.auth import Token def json_get(parsed_json, key): @@ -242,27 +241,3 @@ def pull(self): """ return self._pull - -class TokenInfo(Token): - """ - An immutable representation of a Gogs user token information - """ - - def __init__(self, name, sha1): - self._name = name - Token.__init__(self, sha1) - - @staticmethod - def from_json(parsed_json): - name = json_get(parsed_json, "name") - sha1 = json_get(parsed_json, "sha1") - return TokenInfo(name, sha1) - - @property - def name(self): - """ - The name of the token - - :rtype: str - """ - return self._name diff --git a/gogs_client/interface.py b/gogs_client/interface.py index 7bfbe69..c8a6ee3 100644 --- a/gogs_client/interface.py +++ b/gogs_client/interface.py @@ -1,7 +1,8 @@ import requests from gogs_client._implementation.http_utils import RelativeHttpRequestor, append_url -from gogs_client.entities import GogsUser, GogsRepo, TokenInfo +from gogs_client.entities import GogsUser, GogsRepo +from gogs_client.auth import Token class GogsApi(object): @@ -51,15 +52,15 @@ def get_tokens(self, auth, username=None): :param auth.Authentication auth: authentication for user to retrieve :param str username: username of owner of tokens - :return: list of token info representation - :rtype: List[TokenInfo] + :return: list of token representation + :rtype: List[Token] :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ if username is None: username = self.authenticated_user(auth).username response = self._get("/users/{u}/tokens".format(u=username), auth=auth) - return [TokenInfo.from_json(o) for o in self._check_ok(response).json()] + return [Token.from_json(o) for o in self._check_ok(response).json()] def create_token(self, auth, name, username=None): """ @@ -71,8 +72,8 @@ def create_token(self, auth, name, username=None): :param str name: name of new token :param str username: username of owner of new token - :return: representation of token info - :rtype: TokenInfo + :return: new token representation + :rtype: Token :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ @@ -80,7 +81,7 @@ def create_token(self, auth, name, username=None): username = self.authenticated_user(auth).username data = {"name": name} response = self._post("/users/{u}/tokens".format(u=username), auth=auth, data=data) - return TokenInfo.from_json(self._check_ok(response).json()) + return Token.from_json(self._check_ok(response).json()) def ensure_token(self, auth, name, username=None): """ @@ -92,8 +93,8 @@ def ensure_token(self, auth, name, username=None): :param str name: name of new token :param str username: username of owner of new token - :return: token authenticator - :rtype: TokenInfo + :return: token representation + :rtype: Token :raises NetworkFailure: if there is an error communicating with the server :raises ApiFailure: if the request cannot be serviced """ diff --git a/tests/interface_test.py b/tests/interface_test.py index af997f8..a2d0be8 100644 --- a/tests/interface_test.py +++ b/tests/interface_test.py @@ -51,8 +51,7 @@ def setUp(self): "auth_username", "password") self.expected_repo = gogs_client.GogsRepo.from_json(json.loads(self.repo_json_str)) self.expected_user = gogs_client.GogsUser.from_json(json.loads(self.user_json_str)) - self.expected_token = gogs_client.TokenInfo.from_json(json.loads(self.token_json_str)) - self.token = gogs_client.Token(self.expected_token.token) + self.token = gogs_client.Token.from_json(json.loads(self.token_json_str)) @responses.activate def test_create_repo1(self): @@ -249,10 +248,10 @@ def test_ensure_token(self): responses.add(responses.GET, uri, body="[]", status=200) responses.add(responses.POST, uri, body=self.token_json_str, status=200) responses.add(responses.GET, uri, body="["+self.token_json_str+"]", status=200) - token = self.client.ensure_token(self.username_password, self.expected_token.name, self.username_password.username) - self.assert_tokens_equals(token, self.expected_token) - token = self.client.ensure_token(self.username_password, self.expected_token.name, self.username_password.username) - self.assert_tokens_equals(token, self.expected_token) + token = self.client.ensure_token(self.username_password, self.token.name, self.username_password.username) + self.assert_tokens_equals(token, self.token) + token = self.client.ensure_token(self.username_password, self.token.name, self.username_password.username) + self.assert_tokens_equals(token, self.token) @responses.activate def test_ensure_auth_token(self): @@ -263,10 +262,10 @@ def test_ensure_auth_token(self): responses.add(responses.POST, uri, body=self.token_json_str, status=200) tokens = self.client.get_tokens(self.username_password) self.assertEqual(tokens, []) - tokeninfo = self.client.create_token(self.username_password, self.expected_token.name) - self.assert_tokens_equals(tokeninfo, self.expected_token) - token = self.client.ensure_token(self.username_password, self.expected_token.name) - self.assert_tokens_equals(token, self.expected_token) + tokeninfo = self.client.create_token(self.username_password, self.token.name) + self.assert_tokens_equals(tokeninfo, self.token) + token = self.client.ensure_token(self.username_password, self.token.name) + self.assert_tokens_equals(token, self.token) # helper methods