Skip to content

Commit c637ed7

Browse files
feat(examples): rewrite examples from Rust to Python
1 parent b8824e0 commit c637ed7

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

docs/readme-pypi.md

Whitespace-only changes.

examples/api_calls.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Step 2 — API Calls
3+
==================
4+
Use a previously generated bearer token to call Openapi endpoints.
5+
Run token_generation.py first, then paste the token below (or pass it via env).
6+
7+
Usage:
8+
OPENAPI_TOKEN=<token> python examples/api_calls.py
9+
"""
10+
11+
import os
12+
13+
from openapi_python_sdk.client import Client
14+
15+
token = os.environ.get("OPENAPI_TOKEN", "<your_token>")
16+
17+
client = Client(token=token)
18+
19+
# GET request with query params
20+
resp = client.request(
21+
method="GET",
22+
url="https://test.imprese.openapi.it/advance",
23+
params={"denominazione": "altravia", "provincia": "RM", "codice_ateco": "6201"},
24+
)
25+
print("GET response:", resp)
26+
27+
# POST request with a JSON payload
28+
resp = client.request(
29+
method="POST",
30+
url="https://test.postontarget.com/fields/country",
31+
payload={"limit": 0, "query": {"country_code": "IT"}},
32+
)
33+
print("POST response:", resp)

examples/token_generation.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Step 1 — Token Generation
3+
=========================
4+
Authenticate with your credentials and create a short-lived bearer token
5+
scoped to the API endpoints you need to call.
6+
7+
Usage:
8+
OPENAPI_USERNAME=<user> OPENAPI_APIKEY=<key> python examples/token_generation.py
9+
"""
10+
11+
import os
12+
13+
from openapi_python_sdk.client import OauthClient
14+
15+
username = os.environ.get("OPENAPI_USERNAME", "<your_username>")
16+
apikey = os.environ.get("OPENAPI_APIKEY", "<your_apikey>")
17+
18+
oauth = OauthClient(username=username, apikey=apikey, test=True)
19+
20+
# Create a token valid for 1 hour, scoped to the endpoints you need
21+
resp = oauth.create_token(
22+
scopes=[
23+
"GET:test.imprese.openapi.it/advance",
24+
"POST:test.postontarget.com/fields/country",
25+
],
26+
ttl=3600,
27+
)
28+
token = resp["token"]
29+
print(f"Token created: {token}")
30+
31+
# Revoke the token when you are done
32+
oauth.delete_token(id=token)
33+
print("Token revoked.")

0 commit comments

Comments
 (0)