diff --git a/requirements.txt b/requirements.txt index fffd9f1..25fdacc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ PyHamcrest requests_mock dataclasses-json python-dotenv +git+https://github.com/ucsd-ets/awsed_python_client.git@Rebuild \ No newline at end of file diff --git a/src/dsmlp/app/factory.py b/src/dsmlp/app/factory.py index 879f47a..0b76399 100644 --- a/src/dsmlp/app/factory.py +++ b/src/dsmlp/app/factory.py @@ -1,8 +1,8 @@ -from dsmlp.ext.awsed import DefaultAwsedClient +from dsmlp.ext.awsed import ExternalAwsedClient from dsmlp.ext.kube import DefaultKubeClient class AppFactory: def __init__(self): - self.awsed_client = DefaultAwsedClient() + self.awsed_client = ExternalAwsedClient() self.kube_client = DefaultKubeClient() diff --git a/src/dsmlp/ext/awsed.py b/src/dsmlp/ext/awsed.py index c910478..2f86af3 100644 --- a/src/dsmlp/ext/awsed.py +++ b/src/dsmlp/ext/awsed.py @@ -3,32 +3,25 @@ import requests from dacite import from_dict -from dsmlp.plugin.awsed import AwsedClient, ListTeamsResponse, UnsuccessfulRequest, UserResponse +from dsmlp.plugin.awsed import AwsedClient, ListTeamsResponse, TeamJson, UnsuccessfulRequest, UserResponse +import awsed.client +import awsed.types -class DefaultAwsedClient(AwsedClient): +class ExternalAwsedClient(AwsedClient): def __init__(self): - self.endpoint = os.environ.get('AWSED_ENDPOINT') - self.awsed_api_key = os.environ.get('AWSED_API_KEY') + self.client = awsed.client.DefaultAwsedClient(endpoint=os.environ.get('AWSED_ENDPOINT'), + awsed_api_key=os.environ.get('AWSED_API_KEY')) def describe_user(self, username: str) -> UserResponse: - return self.dataclass_request(UserResponse, f"/users/{username}") + usrResultJson = self.client.describe_user(username) + return UserResponse(uid=usrResultJson.uid) def list_user_teams(self, username: str) -> ListTeamsResponse: - return self.dataclass_request(ListTeamsResponse, f"/teams?username={username}") - - def json_request(self, url): - result = requests.get(self.endpoint + url, headers=self.auth()) - - return result.json() - - def dataclass_request(self, data_class, url): - result = requests.get(self.endpoint + url, headers=self.auth()) - if result.status_code != 200: - raise UnsuccessfulRequest() - - return from_dict(data_class=data_class, data=result.json()) - - def auth(self): - headers = {'Authorization': 'AWSEd api_key=' + self.awsed_api_key} - return headers + usrTeams = self.client.list_teams(username) + teams = [] + + for team in usrTeams.teams: + teams.append(TeamJson(gid=team.gid)) + + return ListTeamsResponse(teams=teams) \ No newline at end of file diff --git a/tests/ext/test_awsed.py b/tests/ext/test_awsed.py deleted file mode 100644 index fdd5fd9..0000000 --- a/tests/ext/test_awsed.py +++ /dev/null @@ -1,51 +0,0 @@ -import os -import requests_mock -from hamcrest import assert_that, equal_to -from dsmlp.ext.awsed import DefaultAwsedClient -from dsmlp.plugin.awsed import ListTeamsResponse, TeamJson, UserResponse - - -class TestAwsedClient: - # noinspection PyMethodMayBeStatic - def setup_method(self) -> None: - os.environ['AWSED_ENDPOINT'] = 'https://awsed.ucsd.edu/api' - os.environ['AWSED_API_KEY'] = "1234" - - # noinspection PyMethodMayBeStatic - def teardown_method(self) -> None: - os.environ.pop('AWSED_ENDPOINT') - os.environ.pop('AWSED_API_KEY') - - def test_describe_user(self, requests_mock): - """test list courses by tag""" - requests_mock.get('https://awsed.ucsd.edu/api/users/user1', text=""" - { - "uid":1 - } - """) - c = DefaultAwsedClient() - user = c.describe_user("user1") - - assert_that(user, equal_to( - UserResponse(uid=1) - )) - - def test_list_user_teams(self, requests_mock): - """test list courses by tag""" - requests_mock.get('https://awsed.ucsd.edu/api/teams?username=user1', text=""" - { - "teams": [ - { - "gid": 1 - } - ] - } - """) - c = DefaultAwsedClient() - user = c.list_user_teams('user1') - - assert_that(user, equal_to( - ListTeamsResponse(teams=[ - TeamJson(gid=1) - ]) - ))