Skip to content

Commit 51a07b4

Browse files
committed
Added get keys
1 parent 14a4624 commit 51a07b4

File tree

9 files changed

+91
-131
lines changed

9 files changed

+91
-131
lines changed

docs/index.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,23 @@ Gitlab
149149
:members:
150150
:inherited-members:
151151

152-
Session
152+
Base
153153
------------------
154154

155155
.. autoclass:: gitlab.base.Base
156-
:members:
156+
:members:
157157

158-
Base
158+
Session
159159
------------------
160160

161-
.. autoclass:: gitlab.base.Base
161+
.. autoclass:: gitlab.session.Session
162162
:members:
163163

164+
Keys
165+
------------------
166+
167+
.. autoclass:: gitlab.keys.Keys
168+
:members:
164169

165170

166171
Indices and tables

gitlab/__init__.py

Lines changed: 2 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
from . import exceptions
1010
from .session import Session
1111
from .users import Users
12+
from .keys import Keys
1213
from .helper import deprecated, format_string
1314

1415

15-
class Gitlab(Session, Users):
16+
class Gitlab(Session, Users, Keys):
1617
"""
1718
Gitlab class
1819
@@ -41,130 +42,6 @@ def setsudo(self, user=None):
4142
pass
4243
else:
4344
self.headers['SUDO'] = user
44-
45-
def getsshkeys(self):
46-
"""
47-
Gets all the ssh keys for the current user
48-
49-
:return: a dictionary with the lists
50-
"""
51-
request = requests.get(
52-
self.keys_url, headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
53-
54-
if request.status_code == 200:
55-
return request.json()
56-
else:
57-
return False
58-
59-
def getsshkey(self, key_id):
60-
"""
61-
Get a single ssh key identified by key_id
62-
63-
:param key_id: the id of the key
64-
:return: the key itself
65-
"""
66-
request = requests.get(
67-
'{0}/{1}'.format(self.keys_url, key_id),
68-
headers=self.headers, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
69-
70-
if request.status_code == 200:
71-
return request.json()
72-
else:
73-
return False
74-
75-
def addsshkey(self, title, key):
76-
"""
77-
Add a new ssh key for the current user
78-
79-
:param title: title of the new key
80-
:param key: the key itself
81-
:return: true if added, false if it didn't add it (it could be because the name or key already exists)
82-
"""
83-
data = {'title': title, 'key': key}
84-
85-
request = requests.post(
86-
self.keys_url, headers=self.headers, data=data,
87-
verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
88-
89-
if request.status_code == 201:
90-
return True
91-
else:
92-
return False
93-
94-
def addsshkeyuser(self, user_id, title, key):
95-
"""
96-
Add a new ssh key for the user identified by id
97-
98-
:param user_id: id of the user to add the key to
99-
:param title: title of the new key
100-
:param key: the key itself
101-
:return: true if added, false if it didn't add it (it could be because the name or key already exists)
102-
"""
103-
data = {'title': title, 'key': key}
104-
105-
request = requests.post(
106-
'{0}/{1}/keys'.format(self.users_url, user_id), headers=self.headers,
107-
data=data, verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
108-
109-
if request.status_code == 201:
110-
return True
111-
else:
112-
return False
113-
114-
def deletesshkey(self, key_id):
115-
"""
116-
Deletes an sshkey for the current user identified by id
117-
118-
:param key_id: the id of the key
119-
:return: False if it didn't delete it, True if it was deleted
120-
"""
121-
request = requests.delete(
122-
'{0}/{1}'.format(self.keys_url, key_id), headers=self.headers,
123-
verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
124-
125-
if request.content == b'null':
126-
return False
127-
else:
128-
return True
129-
130-
def getprojects(self, page=1, per_page=20):
131-
"""
132-
Returns a dictionary of all the projects
133-
134-
:param page: Page number
135-
:param per_page: Records per page
136-
:return: list with the repo name, description, last activity,web url, ssh url, owner and if its public
137-
"""
138-
data = {'page': page, 'per_page': per_page}
139-
140-
request = requests.get(
141-
self.projects_url, params=data, headers=self.headers,
142-
verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
143-
144-
if request.status_code == 200:
145-
return request.json()
146-
else:
147-
return False
148-
149-
def getprojectsall(self, page=1, per_page=20):
150-
"""
151-
Returns a dictionary of all the projects for admins only
152-
153-
:param page: Page number
154-
:param per_page: Records per page
155-
:return: list with the repo name, description, last activity,web url, ssh url, owner and if its public
156-
"""
157-
data = {'page': page, 'per_page': per_page}
158-
159-
request = requests.get(
160-
'{0}/all'.format(self.projects_url), params=data, headers=self.headers,
161-
verify=self.verify_ssl, auth=self.auth, timeout=self.timeout)
162-
163-
if request.status_code == 200:
164-
return request.json()
165-
else:
166-
return False
167-
16845
def getprojectsowned(self, page=1, per_page=20):
16946
"""
17047
Returns a dictionary of all the projects for the current user

gitlab/keys.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from gitlab.base import Base
2+
3+
4+
class Keys(Base):
5+
def keys(self, id):
6+
"""
7+
Get SSH key with user by ID of an SSH key. Note only administrators can lookup SSH key with user by ID of an
8+
SSH key.
9+
10+
>>> gitlab = Gitlab(host='http://localhost:10080', verify_ssl=False)
11+
>>> gitlab.login(user='root', password='5iveL!fe')
12+
>>> gitlab.keys(1)
13+
14+
:param id: The ID of an SSH key
15+
:return: Dictionary containing Key data
16+
"""
17+
return self.get('/keys/{id}'.format(id=id), default_response={})

gitlab_tests/base_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def setUp(self):
1212
self.user = os.environ.get('gitlab_user', 'root')
1313
self.password = os.environ.get('gitlab_password', '5iveL!fe')
1414
self.host = os.environ.get('gitlab_host', 'http://localhost:10080')
15-
self.gitlab = Gitlab(host=self.host, verify_ssl=False)
15+
self.gitlab = Gitlab(host=self.host, verify_ssl=False, suppress_http_error=False)
1616
self.gitlab.host = self.host
1717

1818
responses.add(

gitlab_tests/keys/__init__.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import responses
2+
from requests.exceptions import HTTPError
3+
4+
from gitlab_tests.base_test import BaseTest
5+
from response_data import not_found
6+
from response_data.keys import get_keys
7+
8+
9+
class TestKeys(BaseTest):
10+
@responses.activate
11+
def test_keys(self):
12+
responses.add(
13+
responses.GET,
14+
self.gitlab.api_url + '/keys/1',
15+
json=get_keys,
16+
status=201,
17+
content_type='application/json'
18+
)
19+
20+
self.assertEqual(
21+
self.gitlab.keys(1),
22+
get_keys
23+
)
24+
25+
@responses.activate
26+
def test_keys_with_bad_data(self):
27+
responses.add(
28+
responses.GET,
29+
self.gitlab.api_url + '/keys/1',
30+
json=not_found,
31+
status=404,
32+
content_type='application/json'
33+
)
34+
35+
self.assertRaises(
36+
HTTPError,
37+
self.gitlab.keys, 1
38+
)
39+
40+
# Don't raise exception when suppress http error is True
41+
self.gitlab.suppress_http_error = True
42+
self.assertFalse(self.gitlab.keys(1))
43+
self.gitlab.suppress_http_error = False

integration_tests/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ def setUp(self):
1515
self.password = os.environ.get('gitlab_password', '5iveL!fe')
1616
self.host = os.environ.get('gitlab_host', 'http://gitlab:80')
1717

18-
self.gitlab = Gitlab(host=self.host, verify_ssl=False)
18+
self.gitlab = Gitlab(host=self.host, verify_ssl=False, suppress_http_error=False)
1919

2020
self.gitlab.login(user=self.user, password=self.password)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from requests.exceptions import HTTPError
2+
3+
from integration_tests.base import BaseTest
4+
from response_data.keys import get_keys
5+
6+
7+
class TestKeys(BaseTest):
8+
def test_get(self):
9+
self.assertEqual(
10+
get_keys.keys(),
11+
self.gitlab.keys(1).keys())
12+
13+
self.assertRaises(
14+
HTTPError,
15+
self.gitlab.keys, 100
16+
)

response_data/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
not_found = {"message":"404 Not found"}

response_data/keys/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
get_keys = {"id":1,"title":"thekid@thekid-main","key":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDUSjsL6K0VQMjZY001ajN6rkRwkgC3++AcNqe46i8Xc/FIM/FT62yTvPoxjXuv7gP6bnZql9jTpXx3PH9LQHfdHNg7znwlMHh43wWawKv/HgYDvN3JEsgKisTlhRX2UNrDj7nDDKCkxKtLCGx4Gtx7G0+Z6zl964OR8r88VWghg+nXmq77HTgxdEPXmB3GR04gKXyOfj7P+uAooSP8CkStFR5I04mYiHFu35zyG+v6Mb7irxNVcSMrKe3T8quV7Pb0uUhFKbK/q1isuNV/w0hYlT2k0kZyutjaGj++M5BEm+CLvODUwAkHa+lYQF1srVQoQRc2iPcYDuZZXBnzIdW5 thekid@thekid-main","created_at":"2017-07-29T17:24:12.438Z","can_push":False,"user":{"name":"Administrator","username":"root","id":1,"state":"active","avatar_url":"http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon","web_url":"http://8d5b153b8e26/root","created_at":"2017-07-24T23:13:30.103Z","bio":None,"location":None,"skype":"","linkedin":"","twitter":"","website_url":"","organization":None,"last_sign_in_at":"2017-07-25T18:49:56.200Z","confirmed_at":"2017-07-24T23:13:29.799Z","last_activity_on":"2017-07-25","email":"admin@example.com","color_scheme_id":1,"projects_limit":100000,"current_sign_in_at":"2017-07-29T17:08:44.916Z","identities":[],"can_create_group":True,"can_create_project":True,"two_factor_enabled":False,"external":False}}

0 commit comments

Comments
 (0)