lifeastroapi 0.3.0__tar.gz → 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/CHANGELOG.md +28 -0
  2. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/PKG-INFO +20 -2
  3. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/README.md +18 -0
  4. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/pyproject.toml +2 -2
  5. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/scripts/manifest.json +9 -2
  6. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/_client.py +2 -0
  7. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/_http.py +37 -12
  8. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/_version.py +1 -1
  9. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/errors.py +18 -0
  10. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/numerology.py +4 -2
  11. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/test_client.py +41 -3
  12. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/.gitignore +0 -0
  13. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/LICENSE +0 -0
  14. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/examples/async_example.py +0 -0
  15. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/examples/error_handling.py +0 -0
  16. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/examples/pdf_report.py +0 -0
  17. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/examples/quickstart.py +0 -0
  18. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/scripts/costs.json +0 -0
  19. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/scripts/generate.py +0 -0
  20. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/scripts/generate_pdf.mjs +0 -0
  21. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/scripts/live_sweep.py +0 -0
  22. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/scripts/pdf_goldens.mjs +0 -0
  23. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/scripts/samples.py +0 -0
  24. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/__init__.py +0 -0
  25. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/_core.py +0 -0
  26. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/_params.py +0 -0
  27. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/__init__.py +0 -0
  28. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_branding.py +0 -0
  29. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_browser.py +0 -0
  30. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_chrome.py +0 -0
  31. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_devtools.py +0 -0
  32. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_gotenberg.py +0 -0
  33. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_js.py +0 -0
  34. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_options.py +0 -0
  35. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_outline.py +0 -0
  36. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_print_css.py +0 -0
  37. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_reports.py +0 -0
  38. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/__init__.py +0 -0
  39. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/colors.py +0 -0
  40. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/dasha_analysis.py +0 -0
  41. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/horoscope.py +0 -0
  42. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/kundli_brihad.py +0 -0
  43. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/kundli_brihad_sections.py +0 -0
  44. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/kundli_detailed.py +0 -0
  45. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/kundli_lite.py +0 -0
  46. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/locales.py +0 -0
  47. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/mangal_dosha.py +0 -0
  48. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/match_making.py +0 -0
  49. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/north_indian_chart.py +0 -0
  50. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/numerology.py +0 -0
  51. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/planet_glyphs.py +0 -0
  52. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/sade_sati.py +0 -0
  53. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/shared.py +0 -0
  54. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_templates/varshaphal.py +0 -0
  55. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_threads.py +0 -0
  56. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/pdf/_websocket.py +0 -0
  57. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/py.typed +0 -0
  58. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/__init__.py +0 -0
  59. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/ashtakavarga.py +0 -0
  60. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/calendar.py +0 -0
  61. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/chart.py +0 -0
  62. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/dasha.py +0 -0
  63. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/eclipses.py +0 -0
  64. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/festivals.py +0 -0
  65. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/geo.py +0 -0
  66. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/horoscope.py +0 -0
  67. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/lalkitab.py +0 -0
  68. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/meta.py +0 -0
  69. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/milan.py +0 -0
  70. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/muhurta.py +0 -0
  71. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/narrative.py +0 -0
  72. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/panchang.py +0 -0
  73. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/planet_moments.py +0 -0
  74. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/prashna.py +0 -0
  75. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/remedies.py +0 -0
  76. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/reports.py +0 -0
  77. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/tarot.py +0 -0
  78. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/transit.py +0 -0
  79. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/varshaphal.py +0 -0
  80. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/resources/western.py +0 -0
  81. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/src/lifeastroapi/types.py +0 -0
  82. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/conftest.py +0 -0
  83. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/fixtures/examples.json +0 -0
  84. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/pdf_devtools_stub.py +0 -0
  85. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/pdf_support.py +0 -0
  86. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/test_async_client.py +0 -0
  87. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/test_manifest.py +0 -0
  88. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/test_pdf_branding_options.py +0 -0
  89. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/test_pdf_chrome.py +0 -0
  90. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/test_pdf_engine.py +0 -0
  91. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/test_pdf_gotenberg.py +0 -0
  92. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/test_pdf_js.py +0 -0
  93. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/test_pdf_outline.py +0 -0
  94. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/test_pdf_settle.py +0 -0
  95. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/test_pdf_templates.py +0 -0
  96. {lifeastroapi-0.3.0 → lifeastroapi-1.0.0}/tests/test_version.py +0 -0
@@ -1,5 +1,33 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.0 — 2026-11-27
4
+
5
+ This is the first stable release. From 1.0.0 on, a change that needs a code change on your side
6
+ only comes with a new major version. The install is the same and has no new dependency.
7
+
8
+ - **Easier key setup.** The API key is trimmed of spaces and line breaks around it, so a key read
9
+ from a file or a quoted `.env` value works as it is.
10
+ - **The key never appears in an error message.** When the HTTP library rejects a request header,
11
+ its message quoted the value. The SDK now replaces the key with `***` before raising
12
+ `LifeAstroConnectionError`.
13
+ - **Clear error for a reply that is not JSON.** Such a reply did not come from the API: a proxy,
14
+ firewall or CDN in between answered, usually with an HTML page. It now raises the error of its
15
+ HTTP status with `code == "non_json_response"`; the message gives the status and the start of
16
+ the body.
17
+ - README: what to do on a Python older than 3.9, the `proxy=` argument on older httpx, and the
18
+ `non_json_response` error.
19
+
20
+ **Changes that may need a code change**
21
+
22
+ - A 200 reply with a body that is not JSON now raises instead of returning the text.
23
+ Before: `text = client.request(path)` gave you the body as a `str`.
24
+ Now: `except LifeAstroError as err:` with `err.code == "non_json_response"`; `err.body` holds
25
+ the text.
26
+ - `numerology.full` requires `dob`. The API always required it; a call without it now raises
27
+ `TypeError` before any request is made, instead of a 400 from the API.
28
+ Before: `client.numerology.full(name="Arjun Mehta")` was accepted by the SDK.
29
+ Now: `client.numerology.full(dob="1990-01-15", name="Arjun Mehta")`
30
+
3
31
  ## 0.3.0 — 2026-11-22
4
32
 
5
33
  Upgrading from 0.2.0: your code keeps working. The install is the same and has no new dependency.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: lifeastroapi
3
- Version: 0.3.0
3
+ Version: 1.0.0
4
4
  Summary: Official Python SDK for LifeAstroAPI: Vedic astrology and Hindu calendar API (panchang, festivals, kundli, dasha, kundli matching, muhurat, horoscopes, numerology, tarot, Western astrology) in 10 languages, with branded PDF reports.
5
5
  Project-URL: Homepage, https://lifeastroapi.com
6
6
  Project-URL: Documentation, https://lifeastroapi.com/docs/
@@ -11,7 +11,7 @@ Author-email: Ayanshu Life <support@lifeastroapi.com>
11
11
  License-Expression: MIT
12
12
  License-File: LICENSE
13
13
  Keywords: astrology,astrology-api,festivals,hindu-calendar,horoscope,jyotish,kundli,kundli-matching,kundli-pdf,lifeastroapi,muhurat,numerology,panchang,pdf-reports,tarot,vedic-astrology,western-astrology
14
- Classifier: Development Status :: 4 - Beta
14
+ Classifier: Development Status :: 5 - Production/Stable
15
15
  Classifier: Intended Audience :: Developers
16
16
  Classifier: Operating System :: OS Independent
17
17
  Classifier: Programming Language :: Python
@@ -61,6 +61,17 @@ location-aware calculations and pay-per-call billing. Free tier: 1,500 credits a
61
61
  pip install lifeastroapi
62
62
  ```
63
63
 
64
+ **Python 3.9 or later is required.** On an older Python, `pip` answers "No matching distribution
65
+ found". There you can call the API with the standard library alone:
66
+
67
+ ```python
68
+ import json, urllib.request
69
+
70
+ url = "https://api.lifeastroapi.com/v1/panchang/basic?lat=19.076&lon=72.8777&tz=Asia/Kolkata&date=2026-11-20"
71
+ req = urllib.request.Request(url, headers={"Authorization": "Bearer dv_live_..."})
72
+ day = json.load(urllib.request.urlopen(req, timeout=30))
73
+ ```
74
+
64
75
  Get an API key at **https://lifeastroapi.com/dashboard/keys**.
65
76
 
66
77
  ## Quick start
@@ -164,6 +175,9 @@ client = LifeAstro(
164
175
  )
165
176
  ```
166
177
 
178
+ The `proxy=` argument of `httpx.Client` needs httpx 0.26 or later; on an older httpx write
179
+ `httpx.Client(proxies="http://proxy:3128")`.
180
+
167
181
  Both clients are context managers (`with` / `async with`) and have `close()`
168
182
  (a coroutine on `AsyncLifeAstro`). The SDK closes only the httpx client it created itself, never
169
183
  one you pass in.
@@ -353,6 +367,10 @@ except LifeAstroError as err:
353
367
  | `LifeAstroTimeoutError` | the request timed out (a `LifeAstroConnectionError`) |
354
368
  | `lifeastroapi.pdf.PdfError` | a PDF could not be made (`status == 0`; see [PDF reports](#pdf-reports)) |
355
369
 
370
+ A response that is not JSON (an HTML page from a proxy or firewall in between, for example)
371
+ raises the error of its status with `code == "non_json_response"`; the message carries the HTTP
372
+ status and the start of the body.
373
+
356
374
  All of them extend `LifeAstroError`. Requests are retried automatically (`max_retries`, default
357
375
  2) on 408, 425, 429, 500, 502, 503, 504, network errors and timeouts, with exponential backoff
358
376
  (0.5 s, 1 s, 2 s, ... up to 8 s) or the server's `Retry-After` (up to 30 s).
@@ -25,6 +25,17 @@ location-aware calculations and pay-per-call billing. Free tier: 1,500 credits a
25
25
  pip install lifeastroapi
26
26
  ```
27
27
 
28
+ **Python 3.9 or later is required.** On an older Python, `pip` answers "No matching distribution
29
+ found". There you can call the API with the standard library alone:
30
+
31
+ ```python
32
+ import json, urllib.request
33
+
34
+ url = "https://api.lifeastroapi.com/v1/panchang/basic?lat=19.076&lon=72.8777&tz=Asia/Kolkata&date=2026-11-20"
35
+ req = urllib.request.Request(url, headers={"Authorization": "Bearer dv_live_..."})
36
+ day = json.load(urllib.request.urlopen(req, timeout=30))
37
+ ```
38
+
28
39
  Get an API key at **https://lifeastroapi.com/dashboard/keys**.
29
40
 
30
41
  ## Quick start
@@ -128,6 +139,9 @@ client = LifeAstro(
128
139
  )
129
140
  ```
130
141
 
142
+ The `proxy=` argument of `httpx.Client` needs httpx 0.26 or later; on an older httpx write
143
+ `httpx.Client(proxies="http://proxy:3128")`.
144
+
131
145
  Both clients are context managers (`with` / `async with`) and have `close()`
132
146
  (a coroutine on `AsyncLifeAstro`). The SDK closes only the httpx client it created itself, never
133
147
  one you pass in.
@@ -317,6 +331,10 @@ except LifeAstroError as err:
317
331
  | `LifeAstroTimeoutError` | the request timed out (a `LifeAstroConnectionError`) |
318
332
  | `lifeastroapi.pdf.PdfError` | a PDF could not be made (`status == 0`; see [PDF reports](#pdf-reports)) |
319
333
 
334
+ A response that is not JSON (an HTML page from a proxy or firewall in between, for example)
335
+ raises the error of its status with `code == "non_json_response"`; the message carries the HTTP
336
+ status and the start of the body.
337
+
320
338
  All of them extend `LifeAstroError`. Requests are retried automatically (`max_retries`, default
321
339
  2) on 408, 425, 429, 500, 502, 503, 504, network errors and timeouts, with exponential backoff
322
340
  (0.5 s, 1 s, 2 s, ... up to 8 s) or the server's `Retry-After` (up to 30 s).
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "lifeastroapi"
7
- version = "0.3.0"
7
+ version = "1.0.0"
8
8
  description = "Official Python SDK for LifeAstroAPI: Vedic astrology and Hindu calendar API (panchang, festivals, kundli, dasha, kundli matching, muhurat, horoscopes, numerology, tarot, Western astrology) in 10 languages, with branded PDF reports."
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -32,7 +32,7 @@ keywords = [
32
32
  "lifeastroapi",
33
33
  ]
34
34
  classifiers = [
35
- "Development Status :: 4 - Beta",
35
+ "Development Status :: 5 - Production/Stable",
36
36
  "Intended Audience :: Developers",
37
37
  "Operating System :: OS Independent",
38
38
  "Programming Language :: Python",
@@ -13102,7 +13102,14 @@
13102
13102
  "shape": "numerology",
13103
13103
  "cost": 20,
13104
13104
  "pathParams": [],
13105
- "extras": [],
13105
+ "extras": [
13106
+ {
13107
+ "wire": "dob",
13108
+ "type": "string",
13109
+ "required": true,
13110
+ "field": "dob"
13111
+ }
13112
+ ],
13106
13113
  "params": [
13107
13114
  {
13108
13115
  "name": "name",
@@ -13118,7 +13125,7 @@
13118
13125
  "name": "dob",
13119
13126
  "node": "dob",
13120
13127
  "type": "str",
13121
- "required": false,
13128
+ "required": true,
13122
13129
  "kind": "scalar",
13123
13130
  "wires": [
13124
13131
  "dob"
@@ -30,6 +30,8 @@ def _resolve_config(
30
30
  on_response: ResponseHook | None,
31
31
  ) -> Config:
32
32
  key = api_key if api_key is not None else os.environ.get(API_KEY_ENV)
33
+ # A key read from a file or a quoted .env value often carries a space or newline.
34
+ key = key.strip() if key else key
33
35
  if not key:
34
36
  raise LifeAstroError(
35
37
  "LifeAstro: an API key is required. Pass api_key=... or set "
@@ -11,6 +11,7 @@ import contextlib
11
11
  import json
12
12
  import math
13
13
  import random
14
+ import re
14
15
  import time
15
16
  from collections.abc import Mapping
16
17
  from dataclasses import dataclass, field
@@ -27,6 +28,7 @@ from .errors import (
27
28
  LifeAstroError,
28
29
  LifeAstroTimeoutError,
29
30
  error_from_response,
31
+ non_json_error,
30
32
  )
31
33
  from .types import QueryValue, RateLimit, RequestOptions, ResponseHook, ResponseInfo
32
34
 
@@ -159,13 +161,14 @@ def _emit(config: Config, opts: RequestOptions, response: httpx.Response, path:
159
161
  hook(info)
160
162
 
161
163
 
162
- def _parse_body(content: bytes) -> Any:
164
+ def _parse_body(content: bytes) -> tuple[Any, bool]:
165
+ """The parsed body, and whether it was not JSON (then the body is its text)."""
163
166
  if not content:
164
- return None
167
+ return None, False
165
168
  try:
166
- return json.loads(content)
169
+ return json.loads(content), False
167
170
  except ValueError:
168
- return content.decode("utf-8", errors="replace")
171
+ return content.decode("utf-8", errors="replace"), True
169
172
 
170
173
 
171
174
  def _success(body: Any, raw: bool) -> Any:
@@ -176,12 +179,22 @@ def _success(body: Any, raw: bool) -> Any:
176
179
  return body
177
180
 
178
181
 
179
- def _transport_error(exc: httpx.RequestError, timeout: float) -> LifeAstroConnectionError:
182
+ _BEARER = re.compile(r"(Bearer\s+)[^\s'\"]+")
183
+
184
+
185
+ def _transport_error(
186
+ exc: httpx.RequestError, timeout: float, api_key: str = ""
187
+ ) -> LifeAstroConnectionError:
180
188
  err: LifeAstroConnectionError
181
189
  if isinstance(exc, httpx.TimeoutException):
182
190
  err = LifeAstroTimeoutError(f"Request timed out after {timeout:g}s")
183
191
  else:
184
- err = LifeAstroConnectionError(f"Network request failed: {exc}")
192
+ # httpx quotes a header value it rejects: never let the key into the message.
193
+ detail = str(exc)
194
+ if api_key:
195
+ detail = detail.replace(api_key, "***")
196
+ detail = _BEARER.sub(r"\1***", detail)
197
+ err = LifeAstroConnectionError(f"Network request failed: {detail}")
185
198
  err.__cause__ = exc
186
199
  return err
187
200
 
@@ -215,15 +228,21 @@ class SyncTransport:
215
228
  try:
216
229
  response = self._http.request("GET", url, headers=headers, timeout=timeout)
217
230
  except httpx.RequestError as exc:
218
- err: LifeAstroError = _transport_error(exc, timeout)
231
+ err: LifeAstroError = _transport_error(exc, timeout, self.config.api_key)
219
232
  else:
220
- body = _parse_body(response.content)
233
+ body, non_json = _parse_body(response.content)
221
234
  if 200 <= response.status_code < 300:
222
235
  _emit(self.config, opts, response, path)
236
+ if non_json:
237
+ # A 2xx that is not JSON is not an API answer (a proxy or captive page).
238
+ raise non_json_error(response.status_code, body)
223
239
  return _success(body, opts.get("raw", False))
224
240
  answered = response
225
241
  retry_after = parse_retry_after(response.headers.get("retry-after"))
226
- err = error_from_response(response.status_code, body, retry_after)
242
+ if non_json:
243
+ err = non_json_error(response.status_code, body, retry_after)
244
+ else:
245
+ err = error_from_response(response.status_code, body, retry_after)
227
246
  if attempt >= self.config.max_retries or not _retryable(err):
228
247
  if answered is not None:
229
248
  _emit(self.config, opts, answered, path)
@@ -261,15 +280,21 @@ class AsyncTransport:
261
280
  try:
262
281
  response = await self._http.request("GET", url, headers=headers, timeout=timeout)
263
282
  except httpx.RequestError as exc:
264
- err: LifeAstroError = _transport_error(exc, timeout)
283
+ err: LifeAstroError = _transport_error(exc, timeout, self.config.api_key)
265
284
  else:
266
- body = _parse_body(response.content)
285
+ body, non_json = _parse_body(response.content)
267
286
  if 200 <= response.status_code < 300:
268
287
  _emit(self.config, opts, response, path)
288
+ if non_json:
289
+ # A 2xx that is not JSON is not an API answer (a proxy or captive page).
290
+ raise non_json_error(response.status_code, body)
269
291
  return _success(body, opts.get("raw", False))
270
292
  answered = response
271
293
  retry_after = parse_retry_after(response.headers.get("retry-after"))
272
- err = error_from_response(response.status_code, body, retry_after)
294
+ if non_json:
295
+ err = non_json_error(response.status_code, body, retry_after)
296
+ else:
297
+ err = error_from_response(response.status_code, body, retry_after)
273
298
  if attempt >= self.config.max_retries or not _retryable(err):
274
299
  if answered is not None:
275
300
  _emit(self.config, opts, answered, path)
@@ -1,3 +1,3 @@
1
1
  """SDK version, sent in the ``User-Agent`` header."""
2
2
 
3
- __version__ = "0.3.0"
3
+ __version__ = "1.0.0"
@@ -134,7 +134,25 @@ def error_from_response(status: int, body: Any, retry_after: float | None = None
134
134
  "body": body,
135
135
  }
136
136
  text = message if isinstance(message, str) and message else f"HTTP {status}"
137
+ return _build(status, text, retry_after, kwargs)
137
138
 
139
+
140
+ def non_json_error(status: int, text: str, retry_after: float | None = None) -> LifeAstroError:
141
+ """The error for a response whose body is not JSON: it did not come from the API
142
+ (an HTML page from a proxy, firewall or CDN in between). Same class per status as
143
+ :func:`error_from_response`, with code ``non_json_response``."""
144
+ snippet = " ".join(text.split())[:120]
145
+ message = (
146
+ f"The response did not come from the API as JSON (HTTP {status}). "
147
+ f"A proxy, firewall or CDN in between may have answered. Body starts: {snippet}"
148
+ )
149
+ kwargs: dict[str, Any] = {"code": "non_json_response", "status": status, "body": text}
150
+ return _build(status, message, retry_after, kwargs)
151
+
152
+
153
+ def _build(
154
+ status: int, text: str, retry_after: float | None, kwargs: dict[str, Any]
155
+ ) -> LifeAstroError:
138
156
  if status == 400:
139
157
  return BadRequestError(text, **kwargs)
140
158
  if status == 401:
@@ -131,8 +131,8 @@ class Numerology:
131
131
  def full(
132
132
  self,
133
133
  *,
134
+ dob: str,
134
135
  name: str | None = None,
135
- dob: str | None = None,
136
136
  lang: Locale | None = None,
137
137
  request_options: RequestOptions | None = None,
138
138
  ) -> Any:
@@ -146,6 +146,7 @@ class Numerology:
146
146
  put(_q, "name", name)
147
147
  put(_q, "dob", dob)
148
148
  put(_q, "lang", lang)
149
+ put(_q, "dob", dob)
149
150
  return self._c.request("/v1/numerology/full", _q, request_options=request_options)
150
151
 
151
152
  def personal_periods(
@@ -334,8 +335,8 @@ class AsyncNumerology:
334
335
  async def full(
335
336
  self,
336
337
  *,
338
+ dob: str,
337
339
  name: str | None = None,
338
- dob: str | None = None,
339
340
  lang: Locale | None = None,
340
341
  request_options: RequestOptions | None = None,
341
342
  ) -> Any:
@@ -349,6 +350,7 @@ class AsyncNumerology:
349
350
  put(_q, "name", name)
350
351
  put(_q, "dob", dob)
351
352
  put(_q, "lang", lang)
353
+ put(_q, "dob", dob)
352
354
  return await self._c.request("/v1/numerology/full", _q, request_options=request_options)
353
355
 
354
356
  async def personal_periods(
@@ -233,13 +233,30 @@ def test_maps_status_to_error_class(status: int, cls: type[LifeAstroError]) -> N
233
233
 
234
234
 
235
235
  def test_unknown_4xx_is_the_base_error_with_a_default_code() -> None:
236
- client = _failing(418, "teapot")
236
+ client = _failing(418, {"teapot": True})
237
237
  with pytest.raises(LifeAstroError) as info:
238
238
  client.chart.planets(**BIRTH)
239
239
  assert type(info.value) is LifeAstroError
240
240
  assert info.value.code == "bad_request"
241
241
  assert info.value.message == "HTTP 418"
242
- assert info.value.body == "teapot"
242
+ assert info.value.body == {"teapot": True}
243
+
244
+
245
+ def test_html_error_page_is_non_json_response_with_the_class_of_its_status() -> None:
246
+ client = _failing(403, "<html><body>Access denied</body></html>")
247
+ with pytest.raises(PermissionDeniedError) as info:
248
+ client.chart.planets(**BIRTH)
249
+ assert info.value.code == "non_json_response"
250
+ assert "HTTP 403" in info.value.message
251
+ assert "<html><body>Access denied" in info.value.message
252
+
253
+
254
+ def test_non_json_success_body_raises_non_json_response() -> None:
255
+ client = _failing(200, "plain text")
256
+ with pytest.raises(LifeAstroError) as info:
257
+ client.request("/v1/text")
258
+ assert info.value.code == "non_json_response"
259
+ assert info.value.status == 200
243
260
 
244
261
 
245
262
  def test_maps_429_with_retry_after() -> None:
@@ -467,6 +484,28 @@ def test_raises_when_no_api_key_is_provided() -> None:
467
484
  assert info.value.code == "missing_api_key"
468
485
 
469
486
 
487
+ def test_trims_the_api_key_and_rejects_a_blank_one() -> None:
488
+ rec = Recorder()
489
+ make_client(rec, api_key=" dv_live_test\n").request("/v1/x")
490
+ assert rec.last.headers["Authorization"] == "Bearer dv_live_test"
491
+ with pytest.raises(LifeAstroError) as info:
492
+ LifeAstro(api_key=" \n")
493
+ assert info.value.code == "missing_api_key"
494
+
495
+
496
+ def test_a_transport_error_never_carries_the_key() -> None:
497
+ def fail(request: httpx.Request) -> httpx.Response:
498
+ raise httpx.LocalProtocolError(
499
+ f"Illegal header value {request.headers['Authorization']!r}", request=request
500
+ )
501
+
502
+ client = make_client(Recorder(fail), api_key="dv_live_se cret")
503
+ with pytest.raises(LifeAstroConnectionError) as info:
504
+ client.request("/v1/x")
505
+ assert "cret" not in str(info.value)
506
+ assert "dv_live_se" not in str(info.value)
507
+
508
+
470
509
  def test_reads_the_api_key_from_the_environment(monkeypatch: pytest.MonkeyPatch) -> None:
471
510
  monkeypatch.setenv("LIFEASTRO_API_KEY", "dv_live_env")
472
511
  rec = Recorder()
@@ -573,7 +612,6 @@ def test_request_hits_an_arbitrary_path() -> None:
573
612
  [
574
613
  ([1, 2, 3], [1, 2, 3]),
575
614
  ({"foo": 1, "bar": 2}, {"foo": 1, "bar": 2}),
576
- ("plain text", "plain text"),
577
615
  ],
578
616
  )
579
617
  def test_non_enveloped_bodies_are_returned_as_is(body: Any, expected: Any) -> None:
File without changes
File without changes