Skip to content

Commit 9b62f02

Browse files
committed
first commit
0 parents  commit 9b62f02

File tree

6 files changed

+447
-0
lines changed

6 files changed

+447
-0
lines changed

.gitignore

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
#.idea/
161+
162+
main.py

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
# OpenApi IT Python Client
3+
4+
This client is used to interact with the API found at [openapi.it](https://openapi.it/)
5+
6+
## Pre-requisites
7+
8+
Before using the OpenApi IT Python Client, you will need an account at [openapi.it](https://openapi.it/) and an API key to the sandbox and/or production environment
9+
10+
## Installation
11+
12+
You can install the OpenApi IT Python Client with the following command using go get:
13+
14+
```bash
15+
pip install openapi-cli-python
16+
```
17+
18+
## Usage
19+
20+
21+
```python
22+
from openapi.client import Client, OauthClient
23+
24+
# Initialize the oauth client on the sandbox environment
25+
oauth_client = OauthClient(
26+
username="<your_username>", apikey="<your_apikey>", test=True
27+
)
28+
29+
# Create a token for a list of scopes
30+
resp = oauth_client.create_token(
31+
scopes=[
32+
"GET:test.imprese.openapi.it/advance",
33+
"POST:test.postontarget.com/fields/country",
34+
],
35+
ttl=3600,
36+
)
37+
token = resp["token"]
38+
39+
# Initialize the client
40+
client = Client(token=token)
41+
42+
# Make a request with params
43+
resp = client.request(
44+
method="GET",
45+
url="https://test.imprese.openapi.it/advance",
46+
params={"denominazione": "altravia", "provincia": "RM", "codice_ateco": "6201"},
47+
)
48+
49+
# Make a request with a payload
50+
resp = client.request(
51+
method="POST",
52+
url="https://test.postontarget.com/fields/country",
53+
payload={"limit": 0, "query": { "country_code": "IT"}}
54+
)
55+
56+
# Delete the token
57+
resp = oauth_client.delete_token(id=token)
58+
```
59+
60+
## Contributing
61+
62+
Contributions are always welcome!
63+
64+
See `contributing.md` for ways to get started.
65+
66+
Please adhere to this project's `code of conduct`.
67+
68+
69+
## License
70+
71+
[MIT](https://choosealicense.com/licenses/mit/)
72+
73+
74+
## Authors
75+
76+
- [@maiku1008](https://www.github.com/maiku1008)
77+
- [@openapi-it](https://github.com/openapi-it)

openapi/__init__.py

Whitespace-only changes.

openapi/client.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import base64
2+
from typing import Any, Dict
3+
4+
import httpx
5+
6+
OAUTH_BASE_URL = "https://oauth.openapi.it"
7+
TEST_OAUTH_BASE_URL = "https://test.oauth.openapi.it"
8+
9+
10+
class OauthClient:
11+
def __init__(self, username: str, apikey: str, test: bool = False):
12+
self.client = httpx.Client()
13+
self.url: str = TEST_OAUTH_BASE_URL if test else OAUTH_BASE_URL
14+
self.auth_header: str = (
15+
"Basic " + base64.b64encode(f"{username}:{apikey}".encode("utf-8")).decode()
16+
)
17+
self.headers: Dict[str, Any] = {
18+
"Authorization": self.auth_header,
19+
"Content-Type": "application/json",
20+
}
21+
22+
def get_scopes(self, limit: bool = False) -> Dict[str, Any]:
23+
params = {"limit": int(limit)}
24+
url = f"{self.url}/scopes"
25+
return self.client.get(url=url, headers=self.headers, params=params).json()
26+
27+
def create_token(self, scopes: list[str] = [], ttl: int = 0) -> Dict[str, Any]:
28+
payload = {"scopes": scopes, "ttl": ttl}
29+
url = f"{self.url}/token"
30+
return self.client.post(url=url, headers=self.headers, json=payload).json()
31+
32+
def get_token(self, scope: str = None) -> Dict[str, Any]:
33+
params = {"scope": scope or ""}
34+
url = f"{self.url}/token"
35+
return self.client.get(url=url, headers=self.headers, params=params).json()
36+
37+
def delete_token(self, id: str) -> Dict[str, Any]:
38+
url = f"{self.url}/token/{id}"
39+
return self.client.delete(url=url, headers=self.headers).json()
40+
41+
def get_counters(self, period: str, date: str) -> Dict[str, Any]:
42+
url = f"{self.url}/counters/{period}/{date}"
43+
return self.client.get(url=url, headers=self.headers).json()
44+
45+
46+
class Client:
47+
def __init__(self, token: str):
48+
self.client = httpx.Client()
49+
self.auth_header: str = f"Bearer {token}"
50+
self.headers: Dict[str, str] = {
51+
"Authorization": self.auth_header,
52+
"Content-Type": "application/json",
53+
}
54+
55+
def request(
56+
self,
57+
method: str = "GET",
58+
url: str = None,
59+
payload: Dict[str, Any] = None,
60+
params: Dict[str, Any] = None,
61+
) -> Dict[str, Any]:
62+
payload = payload or {}
63+
params = params or {}
64+
url = url or ""
65+
return self.client.request(
66+
method=method,
67+
url=url,
68+
headers=self.headers,
69+
json=payload,
70+
params=params,
71+
).json()

0 commit comments

Comments
 (0)