You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Pydantic v2 models for all request and response types — validated, typed, IDE-friendly
11
-
- Streaming log support via iterators
12
-
-`FunctionBuilder` client for the [OpenFaaS Pro Function Builder API](https://docs.openfaas.com/openfaas-pro/builder/) — build and push function images from source
13
-
-`FAAS_DEBUG=1` environment variable for request/response logging (auth headers redacted)
14
-
- Context manager support for automatic connection cleanup
7
+
- Manage functions, namespaces, and secrets
8
+
- Invoke functions synchronously or asynchronously
9
+
- Stream function logs
10
+
- Basic auth and [OpenFaaS IAM](https://docs.openfaas.com/openfaas-pro/iam/overview/) support
11
+
- Build and push function images from source via the [Function Builder API](https://docs.openfaas.com/openfaas-pro/builder/)
15
12
16
13
## Requirements
17
14
@@ -72,20 +69,26 @@ with open("/var/secrets/basic-auth-password") as f:
### OpenFaaS IAM — external IdP via client credentials
76
73
77
-
Subclass `requests.auth.AuthBase` directly to implement your own strategy:
74
+
For workloads outside Kubernetes, use `ClientCredentialsTokenSource`to obtain tokens from an external IdP and exchange them for an OpenFaaS gateway JWT:
78
75
79
76
```python
80
-
importrequests.auth
77
+
from openfaas_sdk importClient, TokenAuth, ClientCredentialsTokenSource
with Client("https://gateway.example.com", auth=auth) as client:
91
+
functions = client.get_functions("openfaas-fn")
89
92
```
90
93
91
94
### OpenFaaS IAM — Kubernetes workload identity
@@ -110,28 +113,6 @@ with Client("https://gateway.example.com", auth=auth) as client:
110
113
111
114
`TokenAuth` also implements the `TokenSource` protocol, so it is automatically used as the `function_token_source` for per-function scoped token exchange when calling `get_function_token()`.
112
115
113
-
### OpenFaaS IAM — external IdP via client credentials
114
-
115
-
For workloads outside Kubernetes, use `ClientCredentialsTokenSource` to obtain tokens from an external IdP and exchange them for an OpenFaaS gateway JWT:
116
-
117
-
```python
118
-
from openfaas_sdk import Client, TokenAuth, ClientCredentialsTokenSource
with Client("https://gateway.example.com", auth=auth) as client:
132
-
functions = client.get_functions("openfaas-fn")
133
-
```
134
-
135
116
### Custom token sources
136
117
137
118
If the built-in token sources don't fit your setup, you can implement your own. The `TokenSource` protocol requires a single method, `token() -> str`, that returns a raw OIDC JWT. The implementation can contain any logic you need, for example reading a token from a file, calling an external API, or signing a JWT locally:
@@ -160,7 +141,9 @@ auth = TokenAuth(
160
141
161
142
### Per-function scoped tokens
162
143
163
-
`get_function_token()` exchanges the current identity token for a short-lived token scoped to a specific function (audience `"<namespace>:<function-name>"`). Use this token when invoking functions directly:
144
+
> **Advanced use case.** For most scenarios, use [`invoke_function`](#function-invocation) with `use_function_auth=True`, which handles token exchange automatically. The method below is for cases where you need full control over function invocation outside of the SDK.
145
+
146
+
`get_function_token()` exchanges the current identity token for a short-lived token scoped to a specific function (audience `"<namespace>:<function-name>"`). Use this token when invoking the function directly without going through the SDK:
@@ -274,12 +254,17 @@ for msg in client.get_logs("env", namespace="openfaas-fn", since=since):
274
254
275
255
### Function invocation
276
256
277
-
`invoke_function` returns the raw `requests.Response` from the function. Non-2xx responses are **not** raised as exceptions — function responses are application-level and the caller decides how to interpret them.
257
+
#### Synchronous invocation
258
+
259
+
`invoke_function` returns the raw `requests.Response` from the function. Unlike gateway API calls, non-2xx status codes are not raised as exceptions, function responses are application-level and it is up to the caller to interpret them.
`invoke_function_async` queues the invocation via the gateway's `/async-function/` route and returns immediately with a `202 Accepted` response. The function result is not returned synchronously.
287
+
`invoke_function_async` queues the invocation via the gateway's `/async-function/` route and returns immediately with a `202 Accepted` response. The function result is not returned synchronously.
When OpenFaaS IAM is enabled, use `use_function_auth=True` to automatically
319
-
obtain a per-function scoped token and attach it as a Bearer token. This
320
-
requires the client to be configured with a `TokenAuth` (or any
321
-
`function_token_source`):
303
+
When OpenFaaS IAM is enabled, use `use_function_auth=True` to automatically obtain a per-function scoped token and attach it as a Bearer token. This requires the client to be configured with a `TokenAuth` (or an explicit [`function_token_source`](#using-a-separate-identity-provider-for-function-invocations)):
322
304
323
305
```python
324
306
from openfaas_sdk import Client, TokenAuth, ServiceAccountTokenSource
### Using a separate identity provider for function invocations
389
371
390
-
Set `FAAS_DEBUG=1` to log all requests and responses. The `Authorization` header is automatically redacted.
391
-
392
-
```bash
393
-
FAAS_DEBUG=1 python my_script.py
394
-
```
395
-
396
-
Configure the log level in your application to see the output:
372
+
When using `use_function_auth=True` on function invocations, the client needs a token source to obtain per-function scoped tokens. If the client is configured with `TokenAuth`, it is used automatically. You can also pass a `TokenAuth` instance explicitly as `function_token_source` to use a different identity provider for function invocations than the one used for gateway API calls:
0 commit comments