Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,41 @@
"keepit",
"notif",
"werein"
]
],

"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.ruff": "explicit",
"source.organizeImports.ruff": "explicit"
},
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.ruff": "explicit",
"source.organizeImports.ruff": "explicit"
},
"editor.rulers": [
120
],
"editor.tabSize": 4
},
"ruff.enable": true,
"ruff.organizeImports": true,
"ruff.fixAll": true,
"files.exclude": {
"**/__pycache__": true,
"**/.pytest_cache": true,
"**/.mypy_cache": true,
"**/.ruff_cache": true
},
"files.watcherExclude": {
"**/.git/objects/**": true,
"**/.git/subtree-cache/**": true,
"**/node_modules/**": true,
"**/.hg/store/**": true,
"**/.venv/**": true,
"**/.mypy_cache/**": true,
"**/.ruff_cache/**": true
}
}
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ update: ## Update dependencies
uv sync --upgrade

sync: ## Sync dependencies
uv sync
uv sync

check: clean format lint pre-commit test ## Run all checks and tests
45 changes: 29 additions & 16 deletions src/mistapi/__api_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
This package is licensed under the MIT License.

--------------------------------------------------------------------------------
This module manages API requests with Mist Cloud. It is used to
This module manages API requests with Mist Cloud. It is used to
* generate the URL based on the provided parameters
* add the required HTTP Headers to the request
* report error if any
Expand Down Expand Up @@ -64,13 +64,16 @@ def _url(self, uri) -> str:
"""
logger.debug("apirequest:_url:https://%s%s", self._cloud_uri, uri)
return f"https://{self._cloud_uri}{uri}"

def _log_proxy(self) -> None:
pwd_regex = r':([^:@]*)@'
pwd_regex = r":([^:@]*)@"
if self._session.proxies.get("https"):
logger.info(f"apirequest:sending request to proxy server {re.sub(pwd_regex, ':*********@', self._session.proxies['https'])}")
print(f"apirequest:sending request to proxy server {re.sub(pwd_regex, ':*********@', self._session.proxies['https'])}")

logger.info(
f"apirequest:sending request to proxy server {re.sub(pwd_regex, ':*********@', self._session.proxies['https'])}"
)
print(
f"apirequest:sending request to proxy server {re.sub(pwd_regex, ':*********@', self._session.proxies['https'])}"
)

def _next_apitoken(self) -> None:
logger.info("apirequest:_next_apitoken:rotating API Token")
Expand Down Expand Up @@ -190,7 +193,9 @@ def mist_get(self, uri: str, query: dict = None) -> APIResponse:
return self.mist_get(uri, query)
logger.error(f"apirequest:mist_get:HTTP error occurred: {http_err}")
if resp:
logger.error(f"apirequest:mist_get:HTTP error description: {resp.json()}")
logger.error(
f"apirequest:mist_get:HTTP error description: {resp.json()}"
)
except Exception as err:
logger.error(f"apirequest:mist_get:Other error occurred: {err}")
logger.error("apirequest:mist_get:Exception occurred", exc_info=True)
Expand Down Expand Up @@ -232,7 +237,7 @@ def mist_post(self, uri: str, body: dict = None) -> APIResponse:
logger.debug(f"apirequest:mist_post:request body:{resp.request.body}")
resp.raise_for_status()
except requests.exceptions.ProxyError as proxy_error:
logger.error(f"apirequest:mist_post:Proxy Error: {proxy_error}")
logger.error(f"apirequest:mist_post:Proxy Error: {proxy_error}")
proxy_failed = True
except requests.exceptions.ConnectionError as connexion_error:
logger.error(f"Capirequest:mist_post:Connection Error: {connexion_error}")
Expand All @@ -246,7 +251,9 @@ def mist_post(self, uri: str, body: dict = None) -> APIResponse:
return self.mist_post(uri, body)
logger.error(f"apirequest:mist_post: HTTP error occurred: {http_err}")
if resp:
logger.error(f"apirequest:mist_post: HTTP error description: {resp.json()}")
logger.error(
f"apirequest:mist_post: HTTP error description: {resp.json()}"
)
except Exception as err:
logger.error(f"apirequest:mist_post: Other error occurred: {err}")
logger.error("apirequest:mist_post: Exception occurred", exc_info=True)
Expand Down Expand Up @@ -288,7 +295,7 @@ def mist_put(self, uri: str, body: dict = None) -> APIResponse:
logger.debug(f"apirequest:mist_put:request body:{resp.request.body}")
resp.raise_for_status()
except requests.exceptions.ProxyError as proxy_error:
logger.error(f"apirequest:mist_put:Proxy Error: {proxy_error}")
logger.error(f"apirequest:mist_put:Proxy Error: {proxy_error}")
proxy_failed = True
except requests.exceptions.ConnectionError as connexion_error:
logger.error(f"apirequest:mist_put:Connection Error: {connexion_error}")
Expand All @@ -302,7 +309,9 @@ def mist_put(self, uri: str, body: dict = None) -> APIResponse:
return self.mist_put(uri, body)
logger.error(f"apirequest:mist_put: HTTP error occurred: {http_err}")
if resp:
logger.error(f"apirequest:mist_put: HTTP error description: {resp.json()}")
logger.error(
f"apirequest:mist_put: HTTP error description: {resp.json()}"
)
except Exception as err:
logger.error(f"apirequest:mist_put: Other error occurred: {err}")
logger.error("apirequest:mist_put: Exception occurred", exc_info=True)
Expand Down Expand Up @@ -336,7 +345,7 @@ def mist_delete(self, uri: str, query: dict = None) -> APIResponse:
)
resp.raise_for_status()
except requests.exceptions.ProxyError as proxy_error:
logger.error(f"apirequest:mist_delete:Proxy Error: {proxy_error}")
logger.error(f"apirequest:mist_delete:Proxy Error: {proxy_error}")
proxy_failed = True
except requests.exceptions.ConnectionError as connexion_error:
logger.error(f"apirequest:mist_delete:Connection Error: {connexion_error}")
Expand Down Expand Up @@ -403,7 +412,7 @@ def mist_post_file(self, uri: str, multipart_form_data: dict = {}) -> APIRespons
None,
json.dumps(multipart_form_data[key]),
)
except:
except (OSError, json.JSONDecodeError):
logger.error(
f"apirequest:mist_post_file:multipart_form_data:"
f"Unable to parse JSON object {key} "
Expand All @@ -427,10 +436,12 @@ def mist_post_file(self, uri: str, multipart_form_data: dict = {}) -> APIRespons
)
resp.raise_for_status()
except requests.exceptions.ProxyError as proxy_error:
logger.error(f"apirequest:mist_post_file:Proxy Error: {proxy_error}")
logger.error(f"apirequest:mist_post_file:Proxy Error: {proxy_error}")
proxy_failed = True
except requests.exceptions.ConnectionError as connexion_error:
logger.error(f"apirequest:mist_post_file:Connection Error: {connexion_error}")
logger.error(
f"apirequest:mist_post_file:Connection Error: {connexion_error}"
)
except HTTPError as http_err:
if http_err.response.status_code == 429:
logger.warning(
Expand All @@ -441,7 +452,9 @@ def mist_post_file(self, uri: str, multipart_form_data: dict = {}) -> APIRespons
return self.mist_post_file(uri, multipart_form_data)
logger.error(f"apirequest:mist_post_file: HTTP error occurred: {http_err}")
if resp:
logger.error(f"apirequest:mist_post_file: HTTP error description: {resp.json()}")
logger.error(
f"apirequest:mist_post_file: HTTP error description: {resp.json()}"
)
except Exception as err:
logger.error(f"apirequest:mist_post_file: Other error occurred: {err}")
logger.error("apirequest:mist_post_file: Exception occurred", exc_info=True)
Expand Down
19 changes: 7 additions & 12 deletions src/mistapi/__api_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ class APIResponse:
Class used to pass API Responses
"""

def __init__(
self,
response: Response ,
url: str,
proxy_error: bool = False
) -> None:
def __init__(self, response: Response, url: str, proxy_error: bool = False) -> None:
"""
PARAMS
-----------
Expand All @@ -41,7 +36,7 @@ def __init__(
self.headers = None
self.status_code = None
self.proxy_error = proxy_error

if response is not None:
self.headers = response.headers
self.status_code = response.status_code
Expand Down Expand Up @@ -82,17 +77,17 @@ def _check_next(self) -> None:
page = int(page_str)
if limit * page < total:
uri = f"/api/{self.url.split('/api/')[1]}"
self.next = uri.replace(f"page={page}", f"page={page+1}")
self.next = uri.replace(f"page={page}", f"page={page + 1}")
logger.debug(f"apiresponse:_check_next:set next to {self.next}")
except:
except ValueError:
logger.error(
f"apiresponse:_check_next:"
f"unable to convert total({total})/limit({limit})/page({page}) to int"
f"unable to convert total({total_str})/limit({limit_str})/page({page_str}) to int"
)
logger.error(
"apirequest:mist_post_file:Exception occurred", exc_info=True
"apiresponse:_check_next:Exception occurred", exc_info=True
)
console.error(
f"Unable to convert total "
f"({total})/limit({limit})/page({page}) to int"
f"({total_str})/limit({limit_str})/page({page_str}) to int"
)
Loading
Loading