Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions gogs_client/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,47 @@ def pull(self):
:rtype: bool
"""
return self._pull


class GogsKey(object):
"""
An immutable representation of a Gogs SSH keys
"""

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')
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):
"""
The title of the key

:rtype: str
"""
return self._title

@property
def key(self):
"""
The content of the key

:rtype: str
"""
return self._key
50 changes: 49 additions & 1 deletion gogs_client/interface.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -287,6 +287,54 @@ def delete_user(self, auth, username):
path = "/admin/users/{}".format(username)
self._check_ok(self._delete(path, auth=auth))

def get_keys(self, auth):
"""
Returns the keys owned by the specified user.

:param auth.Authentication auth: authentication for user to retrieve.

: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
"""
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, title, key):
"""
Creates a new key with the specified title for the specified user.

:param auth.Authentication auth: authentication for user to retrieve.
:param str title: title for new key
:param str key: key content

: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
"""
data = {
"title": title,
"key": key
}
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):
Expand Down