radarbg 0.1.0__py3-none-any.whl
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.
radarbg/__init__.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""radarbg - tiny client for the Radar data and file APIs (https://radarbg.com/?ref=pypi).
|
|
2
|
+
|
|
3
|
+
No key: 25 calls a day per IP. With a free key (500 calls a month): Client(api_key="...").
|
|
4
|
+
"""
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import urllib.error
|
|
8
|
+
import urllib.request
|
|
9
|
+
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
BASE = "https://radarbg.com"
|
|
12
|
+
UA = f"radarbg-python/{__version__} (+https://radarbg.com/docs)"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class RadarError(Exception):
|
|
16
|
+
def __init__(self, status, body):
|
|
17
|
+
self.status, self.body = status, body
|
|
18
|
+
msg = body.get("error") or body.get("message") if isinstance(body, dict) else body
|
|
19
|
+
super().__init__(f"HTTP {status}: {msg}")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Client:
|
|
23
|
+
def __init__(self, api_key=None, base=BASE, timeout=60):
|
|
24
|
+
self.api_key = api_key or os.environ.get("RADARBG_API_KEY")
|
|
25
|
+
self.base, self.timeout = base.rstrip("/"), timeout
|
|
26
|
+
|
|
27
|
+
def _req(self, method, path, body=None):
|
|
28
|
+
h = {"User-Agent": UA, "Accept": "application/json"}
|
|
29
|
+
data = None
|
|
30
|
+
if body is not None:
|
|
31
|
+
data = json.dumps(body).encode()
|
|
32
|
+
h["Content-Type"] = "application/json"
|
|
33
|
+
if self.api_key:
|
|
34
|
+
h["X-API-Key"] = self.api_key
|
|
35
|
+
req = urllib.request.Request(self.base + path, data=data, headers=h, method=method)
|
|
36
|
+
try:
|
|
37
|
+
with urllib.request.urlopen(req, timeout=self.timeout) as r:
|
|
38
|
+
return json.loads(r.read().decode())
|
|
39
|
+
except urllib.error.HTTPError as e:
|
|
40
|
+
raw = e.read().decode(errors="replace")
|
|
41
|
+
try:
|
|
42
|
+
raw = json.loads(raw)
|
|
43
|
+
except ValueError:
|
|
44
|
+
pass
|
|
45
|
+
raise RadarError(e.code, raw) from None
|
|
46
|
+
|
|
47
|
+
def call(self, api, **params):
|
|
48
|
+
"""POST /v1/<api> with params as JSON, e.g. call("iban-validator", iban="DE89...")."""
|
|
49
|
+
return self._req("POST", "/v1/" + api.strip("/"), params)
|
|
50
|
+
|
|
51
|
+
def apis(self):
|
|
52
|
+
"""List of (name, summary) for every API in the OpenAPI spec."""
|
|
53
|
+
spec = self._req("GET", "/v1/openapi.json")
|
|
54
|
+
out = []
|
|
55
|
+
for path, ops in spec.get("paths", {}).items():
|
|
56
|
+
op = ops.get("post") or ops.get("get") or {}
|
|
57
|
+
out.append((path.rsplit("/", 1)[-1], op.get("summary", "")))
|
|
58
|
+
return out
|
|
59
|
+
|
|
60
|
+
def __getattr__(self, name):
|
|
61
|
+
if name.startswith("_"):
|
|
62
|
+
raise AttributeError(name)
|
|
63
|
+
return lambda **p: self.call(name.replace("_", "-"), **p)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def call(api, **params):
|
|
67
|
+
return Client().call(api, **params)
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: radarbg
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Tiny client for Radar APIs: IBAN, EU VAT (VIES), SEPA XML, HS codes, PDF and 140 more. 25 free calls a day, no key.
|
|
5
|
+
License: MIT
|
|
6
|
+
Project-URL: Homepage, https://radarbg.com/?ref=pypi
|
|
7
|
+
Project-URL: Documentation, https://radarbg.com/docs?ref=pypi
|
|
8
|
+
Keywords: iban,vat,vies,sepa,hs-code,api,pdf
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# radarbg
|
|
18
|
+
|
|
19
|
+
Tiny Python client (no dependencies) for the [Radar APIs](https://radarbg.com/?ref=pypi): IBAN checks, EU VAT numbers via VIES, HS tariff codes, SEPA, currency rates, PDF and about 140 more.
|
|
20
|
+
|
|
21
|
+
- No key: 25 calls a day per IP.
|
|
22
|
+
- Free key (500 calls a month): `POST https://radarbg.com/v1/signup {"email": "..."}`, then `Client(api_key="...")` or set `RADARBG_API_KEY`.
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
pip install radarbg
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 1. Check IBANs before you pay a supplier
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from radarbg import Client
|
|
32
|
+
r = Client().call("iban-validator", ibans=["DE89 3704 0044 0532 0130 00", "GB00 WEST 1234 5698 7654 32"])
|
|
33
|
+
for it in r["items"]:
|
|
34
|
+
print(it.get("iban"), it.get("valid"), it.get("bankName"))
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## 2. Look up an HS tariff code
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from radarbg import Client
|
|
41
|
+
c = Client()
|
|
42
|
+
print(c.hs_code_tariff_lookup(mode="search", q="cotton socks")) # same as c.call("hs-code-tariff-lookup", q=...)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 3. See every API
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from radarbg import Client
|
|
49
|
+
for name, summary in Client().apis():
|
|
50
|
+
print(name, "-", summary)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Errors raise `radarbg.RadarError` with the HTTP status and the API's message. Full docs: https://radarbg.com/docs?ref=pypi
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
radarbg/__init__.py,sha256=HmHPCRpb_6lyn9sqlqqWg3cQx88ojseq1pkr0bl23NE,2438
|
|
2
|
+
radarbg-0.1.0.dist-info/licenses/LICENSE,sha256=0ugepb1rAC8lFq-5ApTeCKV9wAQ-AI-VD5NVyxiK6C0,257
|
|
3
|
+
radarbg-0.1.0.dist-info/METADATA,sha256=P7xrZ3LMEIwOaNRQf2eN9kBDRyusYaUFwBUIVfG5LQ4,1749
|
|
4
|
+
radarbg-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
radarbg-0.1.0.dist-info/top_level.txt,sha256=5ARFQKd6196M_aukYqzPOueYVTHo-B79QpRSTwsDVvs,8
|
|
6
|
+
radarbg-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
radarbg
|