def _raise_if_known_error(self, response: Response, endpoint: str) -> None: if response.status_code == 401: raise Unauthorized() if response.status_code == 404: raise NotFound(urljoin(self.url, endpoint)) if self._has_json_response(response): body = response.json() is_name_taken: Optional[bool] = None if isinstance(body, Dict): is_name_taken = body.get( "errors", {}).get("name") == ["has already been taken"] if response.status_code == 422: if is_name_taken: raise NameTaken raise ValidationError(body) if response.status_code == 429: error_code: Optional[str] = None try: error_code = response.json()["errors"]["code"] except: pass if error_code == "INSUFFICIENT_REMAINING_STORAGE": raise InsufficientStorage()
def post( self, endpoint: str, payload: Optional[Dict] = None, team: Optional[str] = None, retry: bool = False, error_handlers: Optional[list] = None, debug: bool = False, ): """Post something new on the server trough HTTP Parameters ---------- endpoint : str Recipient of the HTTP operation payload : dict What you want to put on the server (typically json encoded) retry : bool Retry to perform the operation. Set to False on recursive calls. refresh : bool Flag for use the refresh token instead debug : bool Debugging flag. In this case failed requests get printed Returns ------- dict Dictionary which contains the server response """ if payload is None: payload = {} if error_handlers is None: error_handlers = [] response = requests.post(urljoin(self.url, endpoint), json=payload, headers=self._get_headers(team)) if response.status_code == 401: raise Unauthorized() if response.status_code != 200: for error_handler in error_handlers: error_handler(response.status_code, response.json()) if debug: print( f"Client get request response ({response.json()}) with unexpected status " f"({response.status_code}). " f"Client: ({self})" f"Request: (endpoint={endpoint}, payload={payload})") if retry: time.sleep(10) return self.post(endpoint, payload=payload, retry=False) return self._decode_response(response, debug)
def get( self, endpoint: str, team: Optional[str] = None, retry: bool = False, raw: bool = False, debug: bool = False, ): """Get something from the server trough HTTP Parameters ---------- endpoint : str Recipient of the HTTP operation retry : bool Retry to perform the operation. Set to False on recursive calls. raw : bool Flag for returning raw response debug : bool Debugging flag. In this case failed requests get printed Returns ------- dict Dictionary which contains the server response Raises ------ NotFound Resource not found Unauthorized Action is not authorized """ response = requests.get(urljoin(self.url, endpoint), headers=self._get_headers(team)) if response.status_code == 401: raise Unauthorized() if response.status_code == 404: raise NotFound(urljoin(self.url, endpoint)) if response.status_code != 200 and retry: if debug: print( f"Client get request response ({response.json()}) with unexpected status " f"({response.status_code}). " f"Client: ({self})" f"Request: (endpoint={endpoint})") time.sleep(10) return self.get(endpoint=endpoint, retry=False) if raw: return response else: return self._decode_response(response, debug)
def put( self, endpoint: str, payload: Dict, team: Optional[str] = None, retry: bool = False, debug: bool = False, ): """Put something on the server trough HTTP Parameters ---------- endpoint : str Recipient of the HTTP operation payload : dict What you want to put on the server (typically json encoded) retry : bool Retry to perform the operation. Set to False on recursive calls. debug : bool Debugging flag. In this case failed requests get printed Returns ------- dict Dictionary which contains the server response """ response = requests.put(urljoin(self.url, endpoint), json=payload, headers=self._get_headers(team)) if response.status_code == 401: raise Unauthorized() if response.status_code == 429: error_code = response.json()["errors"]["code"] if error_code == "INSUFFICIENT_REMAINING_STORAGE": raise InsufficientStorage() if response.status_code != 200 and retry: if debug: print( f"Client get request response ({response.json()}) with unexpected status " f"({response.status_code}). " f"Client: ({self})" f"Request: (endpoint={endpoint}, payload={payload})") time.sleep(10) return self.put(endpoint, payload=payload, retry=False) return self._decode_response(response, debug)