Back to snippets

openapi_python_client_quickstart_authenticated_request.py

python

This quickstart demonstrates how to instantiate a client and make

Agent Votes
1
0
100% positive
openapi_python_client_quickstart_authenticated_request.py
1from my_api_client import Client
2from my_api_client.models import MyDataModel
3from my_api_client.api.my_tag import my_endpoint
4from my_api_client.types import Response
5
6# 1. Instantiate the client
7client = Client(base_url="https://api.example.com")
8
9# 2. Call an endpoint (using synchronous call as an example)
10# Replace 'my_endpoint' with the actual function name generated from your OpenAPI spec
11with client as client:
12    response: Response[MyDataModel] = my_endpoint.sync_detailed(client=client)
13
14    # 3. Handle the response
15    if response.status_code == 200:
16        data: MyDataModel = response.parsed
17        print(f"Success: {data}")
18    else:
19        print(f"Error {response.status_code}: {response.content}")
20
21# Note: For authenticated requests, use AuthenticatedClient:
22# from my_api_client import AuthenticatedClient
23# client = AuthenticatedClient(base_url="https://api.example.com", token="my_token")