Skip to content

Commit

Permalink
Move to Async ClientSession and Ruff Checking (#45)
Browse files Browse the repository at this point in the history
* 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
GuyKh authored Sep 17, 2024
1 parent 0344b64 commit 7879a74
Show file tree
Hide file tree
Showing 20 changed files with 990 additions and 530 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
environment: Unittest
env:
IMS_TOKEN: '${{ secrets.IMS_TOKEN }}'
IMS_TOKEN: ${{ secrets.IMS_TOKEN }}

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand Down Expand Up @@ -61,6 +61,8 @@ jobs:
- name: Run UnitTests
run: >-
python3 -m unittest discover
env:
IMS_TOKEN: ${{ secrets.IMS_TOKEN }}

#- name: Publish package to TestPyPI
# uses: pypa/gh-action-pypi-publish@release/v1
Expand Down
32 changes: 32 additions & 0 deletions .ruff.toml
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
6 changes: 3 additions & 3 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ name = "pypi"
[packages]
pytest = "*"
pytest-cov = "*"
requests = "*"
loguru = "*"
aiohttp = "*"

[dev-packages]

[requires]
python_version = "3.9"
python_full_version = "3.9.6"
python_version = "3.12"
python_full_version = "3.12.0"
657 changes: 470 additions & 187 deletions Pipfile.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

## Components and Frameworks used

* [aiohttp](https://pypi.org/project/aiohttp/)
* [Loguru](https://pypi.org/project/loguru/)
* [Requests ](https://pypi.org/project/requests/)
* [urllib3](https://pypi.org/project/urllib3/)

## Installing
Expand All @@ -41,7 +41,7 @@ from ims_envista import IMSEnvista
ims = IMSEnvista("2cc57fb1-cda5-4965-af12-b397e5b8eb32")

# Get JERUSALEM stations for getting an id
[station for station in ims.get_all_stations_info() if station.name.startswith("JERUSALEM")]
[station for station in await ims.get_all_stations_info() if station.name.startswith("JERUSALEM")]
> [JERUSALEM GIVAT RAM(22) - Location: [Lat - 31.771 / Long - 35.197], Active, Owner: ims, RegionId: 7, Monitors: [
Rain(mm), WSmax(m / sec), WDmax(deg), WS(m / sec), WD(deg), STDwd(deg), TD(degC), RH( %), TDmax(degC), TDmin(
degC), Grad(w / m2), DiffR(w / m2), WS1mm(m / sec), Ws10mm(m / sec), Time(hhmm), NIP(
Expand All @@ -66,7 +66,7 @@ RAM_1m(249) - Location: [Lat - 31.7704 / Long - 35.1973], Active, Owner: ims, Re
mm)], StationTarget:]

# Get latest data by a station id
ims.get_latest_station_data(23)
await ims.get_latest_station_data(23)
> Station(23), Data: [Station: 23, Date: 2023 - 02 - 21
12: 00:00 + 02: 00, Readings: [(TD: 17.6°C), (TDmax: 17.8°C), (TDmin: 17.5°C), (RH: 58.0 %), (Rain: 0.0mm),
(WS: 2.8m / s), (WSmax: 3.7m / s), (WD: 285.0deg), (WDmax: 289.0deg), (STDwd: 10.5deg),
Expand Down
3 changes: 3 additions & 0 deletions env/pyvenv.cfg
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
7 changes: 6 additions & 1 deletion ims_envista/__init__.py
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",
]
78 changes: 78 additions & 0 deletions ims_envista/commons.py
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
8 changes: 4 additions & 4 deletions ims_envista/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Constant for ims-envista """
"""Constant for ims-envista."""

from .ims_variable import IMSVariable

Expand All @@ -9,9 +9,9 @@
GET_ALL_REGIONS_DATA_URL = ENVISTA_REGIONS_URL
GET_SPECIFIC_STATION_DATA_URL = ENVISTA_STATIONS_URL + "/{}"
GET_SPECIFIC_REGION_DATA_URL = ENVISTA_REGIONS_URL + "/{}"
GET_LATEST_STATION_DATA_URL = ENVISTA_STATIONS_URL + "/{}/data{}/latest"
GET_EARLIEST_STATION_DATA_URL = ENVISTA_STATIONS_URL + "/{}/data{}/earliest"
GET_DAILY_STATION_DATA_URL = ENVISTA_STATIONS_URL + "/{}/data{}/daily"
GET_LATEST_STATION_DATA_URL = ENVISTA_STATIONS_URL + "/{}/data/{}/latest"
GET_EARLIEST_STATION_DATA_URL = ENVISTA_STATIONS_URL + "/{}/data/{}/earliest"
GET_DAILY_STATION_DATA_URL = ENVISTA_STATIONS_URL + "/{}/data/{}/daily"
GET_STATION_DATA_BY_DATE_URL = GET_DAILY_STATION_DATA_URL + "/{}/{}/{}"

GET_MONTHLY_STATION_DATA_URL = ENVISTA_STATIONS_URL + "/{}/data{}/monthly"
Expand Down
Loading

0 comments on commit 7879a74

Please sign in to comment.