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