From ae98075ae717906989d743c14612094771cd7f4b Mon Sep 17 00:00:00 2001 From: vitalii omelchuk Date: Tue, 7 Feb 2017 17:51:53 +0200 Subject: [PATCH 1/3] key add/list api calls --- gogs_client/entities.py | 31 +++++++++++++++++++++++++++++ gogs_client/interface.py | 43 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/gogs_client/entities.py b/gogs_client/entities.py index a117a0f..972eaae 100644 --- a/gogs_client/entities.py +++ b/gogs_client/entities.py @@ -240,3 +240,34 @@ def pull(self): :rtype: bool """ return self._pull + + +class GogsKey(object): + + def __init__(self, title, key): + self._title = title + self._key = key + + @staticmethod + def from_json(parsed_json): + title = json_get(parsed_json, 'title') + key = json_get(parsed_json, 'key') + return GogsKey(title, key) + + @property + def title(self): + """ + The title of the key + + :rtype: str + """ + return self._title + + @property + def key(self): + """ + The content of the key + + :rtype: str + """ + return self._key diff --git a/gogs_client/interface.py b/gogs_client/interface.py index 95ac587..a6bca6f 100644 --- a/gogs_client/interface.py +++ b/gogs_client/interface.py @@ -1,7 +1,7 @@ import requests from gogs_client._implementation.http_utils import RelativeHttpRequestor, append_url -from gogs_client.entities import GogsUser, GogsRepo +from gogs_client.entities import GogsUser, GogsRepo, GogsKey from gogs_client.auth import Token @@ -287,6 +287,47 @@ def delete_user(self, auth, username): path = "/admin/users/{}".format(username) self._check_ok(self._delete(path, auth=auth)) + def get_keys(self, auth, username=None): + """ + Returns the keys owned by the specified user. If no user is specified, + uses the user authenticated by ``auth``. + + :param auth.Authentication auth: authentication for user to retrieve. + :param str username: username of owner of keys + + :return: list of keys + :rtype: List[GogsKey] + :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}/keys".format(u=username), auth=auth) + return [GogsKey.from_json(o) for o in self._check_ok(response).json()] + + def create_key(self, auth, key, username=None): + """ + Creates a new key with the specified title for the specified user. + If no user is specified, uses user authenticated by ``auth``. + + :param auth.Authentication auth: authentication for user to retrieve. + :param entities.GogsKey key: new key + :param str username: username of owner of new token + + :return: new key representation + :rtype: GogsKey + :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 + data = { + "title": key.title, + "key": key.key + } + response = self._post("/user/keys".format(u=username), auth=auth, data=data) + return GogsKey.from_json(self._check_ok(response).json()) + # Helper methods def _delete(self, path, auth=None, **kwargs): From 2de33d063e32b48a79fd020c53581a6f02a05b9b Mon Sep 17 00:00:00 2001 From: vitalii omelchuk Date: Tue, 14 Feb 2017 15:19:42 +0200 Subject: [PATCH 2/3] * docs have been added to GogsKey class * delete_key method have been added to api --- gogs_client/entities.py | 17 +++++++++++++++-- gogs_client/interface.py | 32 +++++++++++++++++++------------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/gogs_client/entities.py b/gogs_client/entities.py index 972eaae..ca20921 100644 --- a/gogs_client/entities.py +++ b/gogs_client/entities.py @@ -243,16 +243,29 @@ def pull(self): class GogsKey(object): + """ + An immutable representation of a Gogs SSH keys + """ - def __init__(self, title, key): + def __init__(self, key_id, title, key): self._title = title self._key = key + self._id = key_id @staticmethod def from_json(parsed_json): title = json_get(parsed_json, 'title') key = json_get(parsed_json, 'key') - return GogsKey(title, key) + key_id = json_get(parsed_json, 'id') + return GogsKey(key_id, title, key) + + @property + def key_id(self): + """ + The key id + :rtype: int + """ + return self._id @property def title(self): diff --git a/gogs_client/interface.py b/gogs_client/interface.py index a6bca6f..6edcf97 100644 --- a/gogs_client/interface.py +++ b/gogs_client/interface.py @@ -287,47 +287,53 @@ def delete_user(self, auth, username): path = "/admin/users/{}".format(username) self._check_ok(self._delete(path, auth=auth)) - def get_keys(self, auth, username=None): + def get_keys(self, auth): """ - Returns the keys owned by the specified user. If no user is specified, - uses the user authenticated by ``auth``. + Returns the keys owned by the specified user. :param auth.Authentication auth: authentication for user to retrieve. - :param str username: username of owner of keys :return: list of keys :rtype: List[GogsKey] :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}/keys".format(u=username), auth=auth) + response = self._get("/user/keys", auth=auth) return [GogsKey.from_json(o) for o in self._check_ok(response).json()] - def create_key(self, auth, key, username=None): + def create_key(self, auth, key): """ Creates a new key with the specified title for the specified user. - If no user is specified, uses user authenticated by ``auth``. :param auth.Authentication auth: authentication for user to retrieve. :param entities.GogsKey key: new key - :param str username: username of owner of new token :return: new key representation :rtype: GogsKey :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 data = { "title": key.title, "key": key.key } - response = self._post("/user/keys".format(u=username), auth=auth, data=data) + response = self._post("/user/keys", auth=auth, data=data) return GogsKey.from_json(self._check_ok(response).json()) + def delete_key(self, auth, key): + """ + Delete the key for the specified user. + + :param auth.Authentication auth: authentication for user to retrieve. + :param entities.GogsKey key: key to delete + + :return: new key representation + :rtype: GogsKey + :raises NetworkFailure: if there is an error communicating with the server + :raises ApiFailure: if the request cannot be serviced + """ + self._check_ok(self._delete("/user/keys/{}".format(key.key_id), auth=auth)) + # Helper methods def _delete(self, path, auth=None, **kwargs): From 34492e80a62dbf2c642cb67cc120214446680d8a Mon Sep 17 00:00:00 2001 From: vitalii omelchuk Date: Tue, 14 Feb 2017 15:24:33 +0200 Subject: [PATCH 3/3] create_key api method have been changed --- gogs_client/interface.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/gogs_client/interface.py b/gogs_client/interface.py index 6edcf97..b475d79 100644 --- a/gogs_client/interface.py +++ b/gogs_client/interface.py @@ -301,12 +301,13 @@ def get_keys(self, auth): response = self._get("/user/keys", auth=auth) return [GogsKey.from_json(o) for o in self._check_ok(response).json()] - def create_key(self, auth, key): + def create_key(self, auth, title, key): """ Creates a new key with the specified title for the specified user. :param auth.Authentication auth: authentication for user to retrieve. - :param entities.GogsKey key: new key + :param str title: title for new key + :param str key: key content :return: new key representation :rtype: GogsKey @@ -314,8 +315,8 @@ def create_key(self, auth, key): :raises ApiFailure: if the request cannot be serviced """ data = { - "title": key.title, - "key": key.key + "title": title, + "key": key } response = self._post("/user/keys", auth=auth, data=data) return GogsKey.from_json(self._check_ok(response).json())