A client library for accessing SpaceTraders API
First, create a client:
from spacetraders import Client
client = Client(base_url="https://api.spacetraders.io/v2")
If the endpoints you're going to hit require authentication, use AuthenticatedClient
instead:
from spacetraders import AuthenticatedClient
client = AuthenticatedClient(base_url="https://api.spacetraders.io/v2", token="SuperSecretToken")
Now call your endpoint and use your models:
from spacetraders.models import GetMyAgentResponse200
from spacetraders.types import Response
response: Response[GetMyAgentResponse200] = client.agents.get_my_agent()
Or do the same thing with an async version:
from spacetraders.models import GetMyAgentResponse200
from spacetraders.types import Response
response: Response[GetMyAgentResponse200] = await client.agents.get_my_agent()
By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle.
client = AuthenticatedClient(
base_url="https://internal_api.example.com",
token="SuperSecretToken",
verify_ssl="/path/to/certificate_bundle.pem",
)
You can also disable certificate validation altogether, but beware that this is a security risk.
client = AuthenticatedClient(
base_url="https://internal_api.example.com",
token="SuperSecretToken",
verify_ssl=False
)
There are more settings on the generated Client
class which let you control more runtime behavior, check out the docstring on that class for more info.
Things to know:
client.default
This project is generated from SpaceTradersAPI/api-docs. The following steps outline how to refresh from a new schema:
git clone https://github.com/SpaceTradersAPI/api-docs /tmp/api-docs
/tmp/api-docs
and generate a single OpenAPI bundle npx @redocly/openapi-cli@latest bundle -o bundle.json
poetry run openapi-python-client update --path /tmp/api-docs/bundle.json --meta none --config openapi-client.yml --custom-template-path=openapi-client-template
This project uses Poetry to manage dependencies and packaging. Here are the basics:
poetry publish --build -r <your-repository-name>
or, if for public PyPI, just poetry publish --build
If you want to install this client into another project without publishing it (e.g. for development) then:
poetry add <path-to-this-client>
from that projectpoetry build -f wheel
pip install <path-to-wheel>