|
1 | 1 | import requests
|
2 | 2 |
|
3 | 3 | from gogs_client._implementation.http_utils import RelativeHttpRequestor, append_url
|
4 |
| -from gogs_client.entities import GogsUser, GogsRepo |
| 4 | +from gogs_client.entities import GogsUser, GogsRepo, TokenInfo |
5 | 5 |
|
6 | 6 |
|
7 | 7 | class GogsApi(object):
|
8 | 8 | """
|
9 |
| - A Gogs client, serving as a wrapper around the Gogs HTTP API. |
| 9 | + A Gogs API entries, serving as a wrapper around the Gogs HTTP API. |
10 | 10 | """
|
11 | 11 |
|
12 | 12 | def __init__(self, base_url):
|
@@ -42,6 +42,68 @@ def authenticated_user(self, auth):
|
42 | 42 | response = self._get("/user", auth=auth)
|
43 | 43 | return GogsUser.from_json(self._check_ok(response).json())
|
44 | 44 |
|
| 45 | + def get_tokens(self, auth, username=None): |
| 46 | + """ |
| 47 | + Returns tokens defined for specified user. |
| 48 | + If no user specified uses user authenticated by the given authentication. |
| 49 | + Right now, authentication must be UsernamePassword (not Token). |
| 50 | +
|
| 51 | + :param auth.Authentication auth: authentication for user to retrieve |
| 52 | + :param str username: username of owner of tokens |
| 53 | +
|
| 54 | + :return: list of token info representation |
| 55 | + :rtype: List[TokenInfo] |
| 56 | + :raises NetworkFailure: if there is an error communicating with the server |
| 57 | + :raises ApiFailure: if the request cannot be serviced |
| 58 | + """ |
| 59 | + if username is None: |
| 60 | + username = self.authenticated_user(auth).username |
| 61 | + response = self._get("/users/{u}/tokens".format(u=username), auth=auth) |
| 62 | + return [TokenInfo.from_json(o) for o in self._check_ok(response).json()] |
| 63 | + |
| 64 | + def create_token(self, auth, name, username=None): |
| 65 | + """ |
| 66 | + Creates new token with specified name for specified user. |
| 67 | + If no user specified uses user authenticated by the given authentication. |
| 68 | + Right now, authentication must be UsernamePassword (not Token). |
| 69 | +
|
| 70 | + :param auth.Authentication auth: authentication for user to retrieve |
| 71 | + :param str name: name of new token |
| 72 | + :param str username: username of owner of new token |
| 73 | +
|
| 74 | + :return: representation of token info |
| 75 | + :rtype: TokenInfo |
| 76 | + :raises NetworkFailure: if there is an error communicating with the server |
| 77 | + :raises ApiFailure: if the request cannot be serviced |
| 78 | + """ |
| 79 | + if username is None: |
| 80 | + username = self.authenticated_user(auth).username |
| 81 | + data = {"name": name} |
| 82 | + response = self._post("/users/{u}/tokens".format(u=username), auth=auth, data=data) |
| 83 | + return TokenInfo.from_json(self._check_ok(response).json()) |
| 84 | + |
| 85 | + def ensure_token(self, auth, name, username=None): |
| 86 | + """ |
| 87 | + Creates new token if token with specified name for specified user does not exists. |
| 88 | + If no user specified uses user authenticated by the given authentication. |
| 89 | + Right now, authentication must be UsernamePassword (not Token). |
| 90 | +
|
| 91 | + :param auth.Authentication auth: authentication for user to retrieve |
| 92 | + :param str name: name of new token |
| 93 | + :param str username: username of owner of new token |
| 94 | +
|
| 95 | + :return: token authenticator |
| 96 | + :rtype: Token |
| 97 | + :raises NetworkFailure: if there is an error communicating with the server |
| 98 | + :raises ApiFailure: if the request cannot be serviced |
| 99 | + """ |
| 100 | + if username is None: |
| 101 | + username = self.authenticated_user(auth).username |
| 102 | + tokens = [token for token in self.get_tokens(auth, username) if token.name == name] |
| 103 | + if tokens: |
| 104 | + return tokens[0].as_token() |
| 105 | + return self.create_token(auth, name, username).as_token() |
| 106 | + |
45 | 107 | def create_repo(self, auth, name, description=None, private=False, auto_init=False,
|
46 | 108 | gitignore_templates=None, license_template=None, readme_template=None):
|
47 | 109 | """
|
|
0 commit comments