|
| 1 | +"""This class takes a base URL as an argument when it's initialized, |
| 2 | +which is the endpoint for the RESTFUL API that we'll be interacting with. |
| 3 | +The create(), read(), update(), and delete() methods each correspond to |
| 4 | +the CRUD operations that can be performed on the API """ |
| 5 | + |
| 6 | +import requests |
| 7 | +import json |
| 8 | + |
| 9 | + |
| 10 | +from ..organizations.organizations import Organization |
| 11 | +from ..users.user import User |
| 12 | +from ..user_session.user_session import UserSession |
| 13 | + |
| 14 | +class ApiClient: |
| 15 | + """ |
| 16 | + This class takes a base URL as an argument when it's initialized, |
| 17 | + which is the endpoint for the RESTFUL API that |
| 18 | + we'll be interacting with. The create(), read(), update(), and delete() |
| 19 | + methods each correspond to the CRUD |
| 20 | + operations that can be performed on the API """ |
| 21 | + |
| 22 | + def __init__(self, endpoint, host, headers, authtoken, authorization, failure_retry, exceptions: bool, |
| 23 | + errors: bool, timeout: int, max_requests: int, retry_on_error: bool): |
| 24 | + self.authorization = authorization |
| 25 | + self.authtoken = authtoken |
| 26 | + self.headers = headers |
| 27 | + self.host = host |
| 28 | + self.endpoint = endpoint |
| 29 | + self.failure_retry = failure_retry |
| 30 | + self.exceptions = exceptions |
| 31 | + self.errors = errors |
| 32 | + self.timeout = timeout |
| 33 | + self.max_requests = max_requests |
| 34 | + self.retry_on_error = retry_on_error |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + def get(self, url, headers=None, params=None): |
| 39 | + """ |
| 40 | + Perform an HTTP GET request with the specified URL and parameters. |
| 41 | +
|
| 42 | + :param url: The URL to send the request to. |
| 43 | + :param headers: Optional dictionary of headers to include in the request. |
| 44 | + :param params: Optional dictionary of URL parameters to include in the request. |
| 45 | + :return: The response from the server. |
| 46 | + """ |
| 47 | + return self._call_request('GET', url, headers=headers, params=params) |
| 48 | + |
| 49 | + def put(self, url, headers=None, params=None, data=None, json=None): |
| 50 | + """ |
| 51 | + Perform an HTTP PUT request with the specified URL and parameters. |
| 52 | +
|
| 53 | + :param url: The URL to send the request to. |
| 54 | + :param headers: Optional dictionary of headers to include in the request. |
| 55 | + :param params: Optional dictionary of URL parameters to include in the request. |
| 56 | + :param data: Optional dictionary, list of tuples, or bytes to include in the body of the request. |
| 57 | + :param json: Optional JSON data to include in the body of the request. |
| 58 | + :return: The response from the server. |
| 59 | + """ |
| 60 | + return self._call_request('PUT', url, headers=headers, params=params, data=data, json=json) |
| 61 | + |
| 62 | + def post(self, url, headers=None, params=None, data=None, json=None): |
| 63 | + """ |
| 64 | + Perform an HTTP POST request with the specified URL and parameters. |
| 65 | +
|
| 66 | + :param url: The URL to send the request to. |
| 67 | + :param headers: Optional dictionary of headers to include in the request. |
| 68 | + :param params: Optional dictionary of URL parameters to include in the request. |
| 69 | + :param data: Optional dictionary, list of tuples, or bytes to include in the body of the request. |
| 70 | + :param json: Optional JSON data to include in the body of the request. |
| 71 | + :return: The response from the server. |
| 72 | + """ |
| 73 | + return self._call_request('POST', url, headers=headers, params=params, data=data, json=json) |
| 74 | + |
| 75 | + def delete(self, url, headers=None, params=None): |
| 76 | + """ |
| 77 | + Perform an HTTP DELETE request with the specified URL and parameters. |
| 78 | +
|
| 79 | + :param url: The URL to send the request to. |
| 80 | + :param headers: Optional dictionary of headers to include in the request. |
| 81 | + :param params: Optional dictionary of URL parameters to include in the request. |
| 82 | + :return: The response from the server. |
| 83 | + """ |
| 84 | + return self._call_request('DELETE', url, headers=headers, params=params) |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + def _call_request(self, method, url_path, headers=None, params=None, data=None, json=None): |
| 89 | + url = f"{self.endpoint}/{url_path}" |
| 90 | + retries = self.failure_retry + 1 |
| 91 | + |
| 92 | + while retries > 0: |
| 93 | + try: |
| 94 | + response = requests.request(method, url, data=data, headers=headers, params=params, json=json) |
| 95 | + |
| 96 | + if response.status_code >= 400: |
| 97 | + if self.errors: |
| 98 | + raise Exception(f"API returned an error: {response.text}") |
| 99 | + elif retries > 1: |
| 100 | + retries -= 1 |
| 101 | + else: |
| 102 | + return None |
| 103 | + else: |
| 104 | + return response |
| 105 | + |
| 106 | + except Exception as e: |
| 107 | + if self.exceptions: |
| 108 | + raise e |
| 109 | + elif retries > 1: |
| 110 | + retries -= 1 |
| 111 | + else: |
| 112 | + return None |
| 113 | + |
| 114 | + def login(self, email, password): |
| 115 | + url = "user-session" |
| 116 | + data = { |
| 117 | + "user": { |
| 118 | + "email": email, |
| 119 | + "password": password |
| 120 | + |
| 121 | + } |
| 122 | + } |
| 123 | + data = json.dumps(data) |
| 124 | + self.api_client = ApiClient( |
| 125 | + host=self.host, endpoint=self.endpoint, authtoken=self.authtoken, |
| 126 | + headers=self.headers, authorization=self.authorization, |
| 127 | + timeout=self.timeout, failure_retry=self.failure_retry, exceptions=self.exceptions, errors=self.errors, |
| 128 | + max_requests=self.max_requests, retry_on_error=self.retry_on_error |
| 129 | + ) |
| 130 | + |
| 131 | + response = UserSession(url = url,headers = self.headers, data = data, api_client=self.api_client, endpoint=self.endpoint).login() |
| 132 | + self.auth_token = self.get_authtoken(response) |
| 133 | + return response |
| 134 | + |
| 135 | + def logout(self): |
| 136 | + url = "user-session" |
| 137 | + self.headers['authtoken'] = self.auth_token |
| 138 | + response = UserSession(url = url,headers = self.headers, api_client = self.api_client, endpoint=self.endpoint).logout() |
| 139 | + return response |
| 140 | + |
| 141 | + def get_authtoken(self, response): |
| 142 | + return response['user']['authtoken'] |
| 143 | + |
| 144 | + |
| 145 | + def get_user(self): |
| 146 | + return User(self.endpoint, self.auth_token, self.headers,self.api_client).get_user() |
| 147 | + |
| 148 | + def update_user(self, user_data): |
| 149 | + return User(self.endpoint, self.auth_token, self.headers,self.api_client).update_user(user_data) |
| 150 | + |
| 151 | + def active_user(self, user_activation_token, user_data): |
| 152 | + return User(self.endpoint, self.auth_token, self.headers,self.api_client).update_user(user_activation_token, user_data) |
| 153 | + |
| 154 | + def request_password(self, user_data): |
| 155 | + return User(self.endpoint, self.auth_token, self.headers,self.api_client).request_password(user_data) |
| 156 | + |
| 157 | + def reset_password(self, user_data): |
| 158 | + return User(self.endpoint, self.auth_token, self.headers,self.api_client).reset_password(user_data) |
| 159 | + |
| 160 | + def get_organizations(self): |
| 161 | + return Organization(self.endpoint, self.auth_token, self.headers,self.api_client).get_organizations() |
| 162 | + |
| 163 | + def get_organization(self, organization_uid): |
| 164 | + return Organization(self.endpoint, self.auth_token, self.headers,self.api_client).get_organizations(organization_uid) |
| 165 | + |
| 166 | + def get_organization_roles(self, organization_uid): |
| 167 | + return Organization(self.endpoint, self.auth_token, self.headers,self.api_client).get_organization_roles(organization_uid) |
| 168 | + |
| 169 | + def organization_add_users(self, organization_uid): |
| 170 | + return Organization(self.endpoint, self.auth_token, self.headers,self.api_client).organization_add_users(organization_uid) |
| 171 | + |
| 172 | + def transfer_organizations_onership(self, organization_uid): |
| 173 | + return Organization(self.endpoint, self.auth_token, self.headers,self.api_client).transfer_organizations_onership(organization_uid) |
| 174 | + |
| 175 | + def organization_stacks(self, organization_uid): |
| 176 | + return Organization(self.endpoint, self.auth_token, self.headers,self.api_client).organization_stacks(organization_uid) |
| 177 | + |
| 178 | + def organization_logs(self, organization_uid): |
| 179 | + return Organization(self.endpoint, self.auth_token, self.headers,self.api_client).organization_logs(organization_uid) |
| 180 | + |
| 181 | + |
0 commit comments