Configuration
You can change four things when you create a client: the API key, the server address, how long it waits for an answer, and how many times it tries a failed request again. The defaults suit most scripts, so you may only need the key.
| Setting | Argument | Environment variable | Default |
|---|---|---|---|
| API key | api_key | THRUSTLAB_API_KEY | none, you must give one |
| Server address | base_url | THRUSTLAB_BASE_URL | https://thrustlab.com |
| Timeout, in seconds | timeout | none | 30 |
| Retries | max_retries | none | 3 |
An argument wins over an environment variable.
Keep the key out of your code
Set THRUSTLAB_API_KEY in your shell or in your deployment settings, then create the client with no arguments:
from thrustlab import Client
client = Client()If you want to pass the key yourself, read it from somewhere safe, such as your secret store. Don't type it into a script that you share or commit.
The library only checks that a key is present. A wrong or revoked key fails on your first request with an AuthenticationError.
Point the library at another server
Set THRUSTLAB_BASE_URL, or pass base_url, to talk to a different ThrustLab server. You might do this to try a local copy of the site:
client = Client(base_url="http://localhost:8080")A slash at the end of the address is removed for you.
Change the timeout and the retries
The timeout is how long the library waits for the server on one request. It does not limit how long a simulation may run. To wait for a simulation, see Async resources.
client = Client(timeout=60, max_retries=5)Set max_retries=0 to turn retries off. Retries and timeouts explains what the library retries and how long it waits between tries.
Use your own HTTP client
The library uses the httpx package to send requests. If you need a proxy, special certificates or request logging, build an httpx.Client and hand it over:
import httpx
http = httpx.Client(timeout=45, proxy="http://proxy.example.com:3128")
client = Client(http_client=http)The library still adds your API key and its own headers to each request. When you pass http_client, the timeout argument is ignored, because your client already has its own timeout.
If the key is missing
Client() raises a ConfigurationError when it finds no key in the argument or in the environment. The message tells you to pass api_key or set THRUSTLAB_API_KEY.