-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccb74aa
commit a163c52
Showing
5 changed files
with
107 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import requests | ||
import json | ||
from typing import List, Dict | ||
|
||
from django.conf import settings | ||
|
||
|
||
class ConnectRESTClient: | ||
def __init__(self): | ||
self.base_url = settings.CONNECT_API_URL | ||
self.headers = { | ||
"Content-Type": "application/json; charset: utf-8", | ||
"Authorization": self.get_auth_token(), | ||
} | ||
|
||
def get_auth_token(self) -> str: | ||
request = requests.post( | ||
url=settings.OIDC_OP_TOKEN_ENDPOINT, | ||
data={ | ||
"client_id": settings.OIDC_RP_CLIENT_ID, | ||
"client_secret": settings.OIDC_RP_CLIENT_SECRET, | ||
"grant_type": "client_credentials", | ||
}, | ||
) | ||
token = request.json().get("access_token") | ||
return f"Bearer {token}" | ||
|
||
def list_classifiers( | ||
self, project_uuid: str, user_email: str | ||
) -> List[Dict[str, str]]: | ||
request = requests.get( | ||
url=f"{self.base_url}/v1/organization/project/list_classifier/", | ||
headers=self.headers, | ||
params={"project_uuid": project_uuid, "user_email": user_email}, | ||
) | ||
|
||
return request.json() | ||
|
||
def list_authorizations(self, project_uuid: str) -> List[str]: | ||
classifiers = self.list_classifiers(project_uuid=project_uuid) | ||
|
||
return [classifier.get("authorization_uuid") for classifier in classifiers] | ||
|
||
def get_authorization_classifier( | ||
self, project_uuid: str, authorization_uuid: str | ||
) -> str: | ||
""" | ||
Recives a authorization UUID and returns the respective classifier UUID | ||
""" | ||
classifiers = self.list_classifiers(project_uuid) | ||
classifier = filter( | ||
lambda classifier: classifier["authorization_uuid"] == authorization_uuid, | ||
classifiers, | ||
) | ||
|
||
return classifier.get("uuid") | ||
|
||
def remove_authorization( | ||
self, project_uuid: str, authorization_uuid: str, user_email: str | ||
): | ||
classifier_uuid = self.get_authorization_classifier( | ||
project_uuid, authorization_uuid | ||
) | ||
request = requests.delete( | ||
url=f"{self.base_url}/v1/organization/project/destroy_classifier/", | ||
headers=self.headers, | ||
json=json.dumps({"uuid": classifier_uuid, "user_email": user_email}), | ||
) | ||
|
||
return request.json() | ||
|
||
def create_classifier(self, **kwargs): | ||
request = requests.post( | ||
url=f"{self.base_url}/v1/organization/project/create_classifier/", | ||
headers=self.headers, | ||
json=json.dumps({**kwargs, "classifier_type": "bothub"}), | ||
) | ||
return request.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters