Packages
infusion_runtime
0.1.0
The runtime shared by SDKs generated with Infusion: HTTP, retries, streaming, pagination
Current section
Files
Jump to
Current section
Files
infusion_runtime
README.md
README.md
# infusion_runtime
The runtime shared by every Elixir SDK generated with
[Infusion](https://github.com/mandar1045/infusion).
You don't use this package directly: an SDK generated by Infusion depends
on it, and its generated functions hand each API call to it. Keeping this
code out of the generated SDKs means a fix here reaches every SDK, and the
generated code stays short and readable.
What it does, built on [Req](https://hex.pm/packages/req):
* **Requests**: base URL, auth (bearer, basic, API key header or query),
JSON, nested form (`metadata[key]=value`) and multipart bodies,
per-call options
* **Retries** like the official SDKs: connection errors, timeouts, 408,
409, 429 and 5xx; `retry-after-ms`, `retry-after`, `x-should-retry`;
capped exponential backoff with jitter; idempotency keys reused across
retries
* **Decoding** JSON into the SDK's structs, with real union types and
unknown fields kept in `__extra__`
* **Streaming** Server-Sent Events as a lazy `Stream` of decoded events
* **Pagination** as a lazy `Stream` of every item: cursors, page numbers,
offsets, next-page URLs and `Link` headers
* **Errors** as one struct, `Infusion.Runtime.Error`, with a `type`
(`:rate_limit`, `:not_found`, ...), status, body and request ID
* **Telemetry**: `[:infusion, :request, :start | :stop | :exception]`
## Configuration
Settings come from the SDK's defaults, then your config, then the call:
```elixir
config :openai_sdk, api_key: System.fetch_env!("OPENAI_API_KEY"), max_retries: 5
OpenAI.Models.list(timeout: 5_000)
```
See `Infusion.Runtime.Config` for every setting.
## Testing
Point an SDK at a `Req.Test` stub:
```elixir
# config/test.exs
config :openai_sdk, req_options: [plug: {Req.Test, OpenAI}]
```
## License
Apache 2.0