Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout argument for http client #4

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,15 @@ También tiene opción `--help` que muestra la ayuda particular de este sub-coma
│ frts [FRTS]... List of frt codes separated by ' ' [default: None] │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ──────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ * --api-base-url TEXT [env var: ENERBIT_API_BASE_URL] [default: None] [required] │
│ * --api-username TEXT [env var: ENERBIT_API_USERNAME] [default: None] [required] │
│ * --api-password TEXT [env var: ENERBIT_API_PASSWORD] [default: None] [required] │
│ --since [%Y-%m-%d|%Y%m%d] [default: (yesterday)] │
│ --until [%Y-%m-%d|%Y%m%d] [default: (today)] │
│ --timezone TEXT [default: America/Bogota] │
│ --out-format [csv|jsonl] Output file format [default: jsonl] │
│ --frt-file PATH Path file with one frt code per line [default: None] │
│ --help Show this message and exit. │
│ * --api-base-url TEXT [env var: ENERBIT_API_BASE_URL] [default: None] [required] │
│ * --api-username TEXT [env var: ENERBIT_API_USERNAME] [default: None] [required] │
│ * --api-password TEXT [env var: ENERBIT_API_PASSWORD] [default: None] [required] │
│ --since [%Y-%m-%d|%Y%m%d] [default: (yesterday)] │
│ --until [%Y-%m-%d|%Y%m%d] [default: (today)] │
│ --timezone TEXT [default: America/Bogota] │
│ --out-format [csv|jsonl] Output file format [default: jsonl] │
│ --frt-file PATH Path file with one frt code per line [default: None] │
│ --timeout INTEGER RANGE [0<=x<=20] [default: 5] │
│ --help Show this message and exit. │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
```
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
install_requires=[
"httpx",
"typer",
"pydantic",
"pydantic>2",
"tzdata",
"urlpath",
"rich",
Expand Down
2 changes: 1 addition & 1 deletion src/enerbitdso/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.9
0.1.10
10 changes: 9 additions & 1 deletion src/enerbitdso/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,16 @@ def fetch(
frt_file: pathlib.Path = typer.Option(
None, help="Path file with one frt code per line"
),
timeout: int = typer.Option(
5,
min=0,
max=20,
),
frts: list[str] = typer.Argument(None, help="List of frt codes separated by ' '"),
):
if timeout is not None:
enerbit.set_http_timeout(timeout)

try:
ebclient = enerbit.get_client(api_base_url, api_username, api_password)
except Exception:
Expand Down Expand Up @@ -99,7 +107,7 @@ def fetch(
)
except Exception:
err_console.print(f"Failed to fetch usage records for frt code '{f}'")
# err_console.print_exception()
err_console.print_exception()
continue

match out_format:
Expand Down
5 changes: 4 additions & 1 deletion src/enerbitdso/enerbit.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class ScheduleMeasurementRecord(pydantic.BaseModel):
reactive_energy_imported: float
reactive_energy_exported: float

def set_http_timeout(timeout):
global TIMEOUT
TIMEOUT = httpx.Timeout(timeout, read=60)

def get_auth_token(base_url, username, password):
path = "/auth/token/"
Expand Down Expand Up @@ -106,7 +109,7 @@ def get_schedule_measurement_records(
response.raise_for_status()
records = response.json()
records = sorted(records, key=lambda r: r["time_local_utc"])
measurement_records = [ScheduleMeasurementRecord.model_validate(r) for r in records]
measurement_records = records
measurement_records = scale_measurement_records(
measurement_records, scale=WATT_HOUR_TO_KILOWATT_HOUR
)
Expand Down