Skip to content

Commit 35ca07f

Browse files
committed
docs: add custom headers guidance to README
1 parent 8e38d75 commit 35ca07f

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,66 @@ async def main():
200200
return api_response
201201
```
202202

203+
### Custom Headers
204+
205+
#### Default Headers
206+
207+
You can set default headers that will be sent with every request by accessing the API client and using the `set_default_header` method:
208+
209+
```python
210+
from openfga_sdk import ClientConfiguration, OpenFgaClient
211+
212+
213+
async def main():
214+
configuration = ClientConfiguration(
215+
api_url=FGA_API_URL,
216+
store_id=FGA_STORE_ID,
217+
authorization_model_id=FGA_MODEL_ID,
218+
)
219+
220+
async with OpenFgaClient(configuration) as fga_client:
221+
# Set default headers that will be sent with every request
222+
fga_client._api_client.set_default_header("X-Custom-Header", "default-value")
223+
fga_client._api_client.set_default_header("X-Request-Source", "my-app")
224+
225+
api_response = await fga_client.read_authorization_models()
226+
return api_response
227+
```
228+
229+
#### Per-Request Headers
230+
231+
You can also send custom headers on a per-request basis by using the `options` parameter. Per-request headers will override any default headers with the same name.
232+
233+
```python
234+
from openfga_sdk import ClientConfiguration, OpenFgaClient
235+
from openfga_sdk.client.models import ClientCheckRequest
236+
237+
238+
async def main():
239+
configuration = ClientConfiguration(
240+
api_url=FGA_API_URL,
241+
store_id=FGA_STORE_ID,
242+
authorization_model_id=FGA_MODEL_ID,
243+
)
244+
245+
async with OpenFgaClient(configuration) as fga_client:
246+
# Add custom headers to a specific request
247+
result = await fga_client.check(
248+
body=ClientCheckRequest(
249+
user="user:anne",
250+
relation="viewer",
251+
object="document:roadmap",
252+
),
253+
options={
254+
"headers": {
255+
"X-Request-ID": "123e4567-e89b-12d3-a456-426614174000",
256+
"X-Custom-Header": "custom-value", # Overrides default header value
257+
}
258+
}
259+
)
260+
return result
261+
```
262+
203263
#### Synchronous Client
204264

205265
To run outside of an async context, the project exports a synchronous client

0 commit comments

Comments
 (0)