-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to Async ClientSession and Ruff Checking (#45)
* Move to async methods * ruff fixes * Fix Tests * Update minimum python version * Fix CI * Requirements * Fix Token usage * Fix commons import * Async tests * Async tests * Fix bad tests * Update README.md * Fix Requirements * Remove requests * Update Pipfile
- Loading branch information
Showing
20 changed files
with
990 additions
and
530 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,32 @@ | ||
# The contents of this file is based on https://github.com/home-assistant/core/blob/dev/pyproject.toml | ||
|
||
target-version = "py312" | ||
|
||
[lint] | ||
select = [ | ||
"ALL", | ||
] | ||
|
||
ignore = [ | ||
"ANN101", # Missing type annotation for `self` in method | ||
"ANN401", # Dynamically typed expressions (typing.Any) are disallowed | ||
"D102", | ||
"D103", # no docstrings on public methods | ||
"D105", # no docstrings on magic methods | ||
"D107", # no docstrings for methods | ||
"D203", # no-blank-line-before-class (incompatible with formatter) | ||
"D212", # multi-line-summary-first-line (incompatible with formatter) | ||
"COM812", # incompatible with formatter | ||
"ISC001", # incompatible with formatter | ||
"S101", # Assert, | ||
"E501" # Line too long | ||
] | ||
|
||
[lint.flake8-pytest-style] | ||
fixture-parentheses = false | ||
|
||
[lint.pyupgrade] | ||
keep-runtime-typing = true | ||
|
||
[lint.mccabe] | ||
max-complexity = 25 |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,3 @@ | ||
home = /usr/bin | ||
include-system-site-packages = false | ||
version = 3.10.7 |
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 |
---|---|---|
@@ -1,2 +1,7 @@ | ||
"""Module providing IMS (Israel Meteorological Service) python API wrapper for Envista.""" | ||
"""Module providing IMS (Israel Meteorological Service) API wrapper for Envista.""" | ||
from .commons import IMSEnvistaError | ||
from .ims_envista import IMSEnvista | ||
|
||
__all__ = [ | ||
"IMSEnvista", "IMSEnvistaError", | ||
] |
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 @@ | ||
"""IMS Envista Commons.""" | ||
|
||
import http | ||
import logging | ||
from json import JSONDecodeError | ||
from typing import Any | ||
from uuid import UUID | ||
|
||
from aiohttp import ( | ||
ClientError, | ||
ClientSession, | ||
TraceRequestChunkSentParams, | ||
TraceRequestEndParams, | ||
TraceRequestStartParams, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class IMSEnvistaError(Exception): | ||
""" | ||
Exception raised for errors in the IMS Envista API. | ||
Attributes | ||
---------- | ||
error -- description of the error | ||
""" | ||
|
||
def __init__(self, error: str) -> None: | ||
self.error = error | ||
super().__init__(f"{self.error}") | ||
|
||
async def on_request_start_debug(session: ClientSession, context,params: TraceRequestStartParams) -> None: # noqa: ANN001, ARG001 | ||
logger.debug("HTTP %s: %s", params.method, params.url) | ||
|
||
|
||
async def on_request_chunk_sent_debug( | ||
session: ClientSession, context, params: TraceRequestChunkSentParams # noqa: ANN001, ARG001 | ||
) -> None: | ||
if (params.method in ("POST", "PUT")) and params.chunk: | ||
logger.debug("HTTP Content %s: %s", params.method, params.chunk) | ||
|
||
|
||
async def on_request_end_debug(session: ClientSession, context, params: TraceRequestEndParams) -> None: # noqa: ANN001, ARG001 | ||
response_text = await params.response.text() | ||
logger.debug("HTTP %s Response <%s>: %s", params.method, params.response.status, response_text) | ||
|
||
|
||
def get_headers(token: UUID | str) -> dict[str, str]: | ||
return { | ||
"Accept": "application/vnd.github.v3.text-match+json", | ||
"Authorization": f"ApiToken {token!s}" | ||
} | ||
|
||
async def get( | ||
session: ClientSession, url: str, token: UUID | str, headers: dict | None = None | ||
) -> dict[str, Any]: | ||
try: | ||
if not headers: | ||
headers = get_headers(token) | ||
|
||
resp = await session.get(url=url, headers=headers) | ||
json_resp: dict = await resp.json(content_type=None) | ||
except TimeoutError as ex: | ||
msg = f"Failed to communicate with IMS Envista API due to time out: ({ex!s})" | ||
raise IMSEnvistaError(msg) from ex | ||
except ClientError as ex: | ||
msg = f"Failed to communicate with IMS Envistadue to ClientError: ({ex!s})" | ||
raise IMSEnvistaError(msg) from ex | ||
except JSONDecodeError as ex: | ||
msg = f"Received invalid response from IMS Envista API: {ex!s}" | ||
raise IMSEnvistaError(msg) from ex | ||
|
||
if resp.status != http.HTTPStatus.OK: | ||
msg = f"Received Error from IMS Envista API: {resp.status, resp.reason}" | ||
raise IMSEnvistaError(msg) | ||
|
||
return json_resp |
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
Oops, something went wrong.