Commit c0253111 authored by Vamshi Krishna's avatar Vamshi Krishna

GitLabUser: Fetch user id from username

parent 94735ff0
......@@ -4,10 +4,12 @@ Contains a representation of GitHub users.
from typing import Optional
from typing import Union
from IGitt import ElementDoesntExistError
from IGitt.GitLab import GitLabMixin
from IGitt.GitLab import GitLabOAuthToken
from IGitt.GitLab import GitLabPrivateToken
from IGitt.Interfaces.User import User
from IGitt.Interfaces import get
class GitLabUser(GitLabMixin, User):
......@@ -17,7 +19,7 @@ class GitLabUser(GitLabMixin, User):
def __init__(self,
token: Union[GitLabPrivateToken, GitLabOAuthToken],
identifier: Optional[int]=None):
identifier: Optional[Union[str, int]]=None):
"""
Creates a new user.
......@@ -25,8 +27,21 @@ class GitLabUser(GitLabMixin, User):
:param identifier: Pass None if it's you! Otherwise the id, integer.
"""
self._token = token
self._url = '/users/' + str(identifier) if identifier else '/user'
self._url = '/user'
self._id = identifier
if identifier:
if isinstance(identifier, str):
params = {'username': identifier}
resp = get(self._token, self.absolute_url('/users'), params)
if resp:
self._id = resp[0]['id']
self._url = '/users/' + str(resp[0]['id'])
else:
raise ElementDoesntExistError('This username does not '
'exist.')
else:
self._url = '/users/' + str(identifier)
@property
def username(self) -> str:
......
......@@ -26,4 +26,87 @@ interactions:
X-Request-Id: [a1bc867b-e2ef-4807-ad64-6ac554e0f385]
X-Runtime: ['0.111786']
status: {code: 200, message: OK}
- request:
body: '{}'
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
Content-Length: ['2']
Content-Type: [application/json]
User-Agent: [IGitt]
method: GET
uri: https://gitlab.com/api/v4/users?per_page=100&username=sils
response:
body: {string: '[{"id":104269,"name":"Lasse Schuirmann","username":"sils","state":"active","avatar_url":"https://secure.gravatar.com/avatar/ea9b9ed83df6acc43cc4ea26b0447ea7?s=80\u0026d=identicon","web_url":"https://gitlab.com/sils"}]'}
headers:
Cache-Control: ['max-age=0, private, must-revalidate']
Content-Length: ['217']
Content-Type: [application/json]
Date: ['Wed, 18 Jul 2018 13:38:40 GMT']
Etag: [W/"a37e5bb674e0601b24ce96829f3ce168"]
Link: ['<https://gitlab.com/api/v4/users?active=false&blocked=false&external=false&page=1&per_page=100&skip_ldap=false&username=sils&with_custom_attributes=false>;
rel="first", <https://gitlab.com/api/v4/users?active=false&blocked=false&external=false&page=1&per_page=100&skip_ldap=false&username=sils&with_custom_attributes=false>;
rel="last"']
RateLimit-Limit: ['600']
RateLimit-Observed: ['1']
RateLimit-Remaining: ['599']
RateLimit-Reset: ['1531921180']
RateLimit-ResetTime: ['Thu, 18 Jul 2018 13:39:40 GMT']
Server: [nginx]
Strict-Transport-Security: [max-age=31536000]
Vary: [Origin]
X-Content-Type-Options: [nosniff]
X-Frame-Options: [SAMEORIGIN]
X-Next-Page: ['']
X-Page: ['1']
X-Per-Page: ['100']
X-Prev-Page: ['']
X-Request-Id: [4675ac36-0e73-40bf-b13b-1c2e33953a1d]
X-Runtime: ['0.053204']
X-Total: ['1']
X-Total-Pages: ['1']
status: {code: 200, message: OK}
- request:
body: '{}'
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
Content-Length: ['2']
Content-Type: [application/json]
If-None-Match: [W/"a37e5bb674e0601b24ce96829f3ce168"]
User-Agent: [IGitt]
method: GET
uri: https://gitlab.com/api/v4/users?per_page=100&username=sils38838
response:
body: {string: '[]'}
headers:
Cache-Control: ['max-age=0, private, must-revalidate']
Content-Length: ['2']
Content-Type: [application/json]
Date: ['Sun, 22 Jul 2018 05:48:23 GMT']
Etag: [W/"d751713988987e9331980363e24189ce"]
Link: ['<https://gitlab.com/api/v4/users?active=false&blocked=false&external=false&page=1&per_page=100&skip_ldap=false&username=sils38838&with_custom_attributes=false>;
rel="first", <https://gitlab.com/api/v4/users?active=false&blocked=false&external=false&page=1&per_page=100&skip_ldap=false&username=sils38838&with_custom_attributes=false>;
rel="last"']
RateLimit-Limit: ['600']
RateLimit-Observed: ['1']
RateLimit-Remaining: ['599']
RateLimit-Reset: ['1532238563']
RateLimit-ResetTime: ['Mon, 22 Jul 2018 05:49:23 GMT']
Server: [nginx]
Strict-Transport-Security: [max-age=31536000]
Vary: [Origin]
X-Content-Type-Options: [nosniff]
X-Frame-Options: [SAMEORIGIN]
X-Next-Page: ['']
X-Page: ['1']
X-Per-Page: ['100']
X-Prev-Page: ['']
X-Request-Id: [09915158-489f-446d-bcdc-1c07413eb9c3]
X-Runtime: ['0.049650']
X-Total: ['0']
X-Total-Pages: ['1']
status: {code: 200, message: OK}
version: 1
import os
from IGitt import ElementDoesntExistError
from IGitt.GitLab import GitLabOAuthToken
from IGitt.GitLab.GitLabUser import GitLabUser
......@@ -20,6 +21,10 @@ class GitLabUserTest(IGittTestCase):
def test_user_id(self):
self.assertEqual(self.user.identifier, 1369631)
self.assertEqual(self.sils.identifier, 104269)
test_user = GitLabUser(self.token, 'sils')
self.assertEqual(test_user.identifier, 104269)
with self.assertRaises(ElementDoesntExistError):
GitLabUser(self.token, 'sils38838')
def test_username(self):
self.assertEqual(self.sils.username, 'sils')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment