Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions OpenAPI/kiota/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ export class SaveRequestHandler implements Middleware {
}
```

### [Python](#tab/python)

```python
class SaveRequestHandler(BaseMiddleware):
async def send(
self, request: httpx.Request, transport: httpx.AsyncBaseTransport
) -> httpx.Response:
print(f"Request: {request.content}")
return await super().send(request, transport)
```

---

## Register middleware
Expand All @@ -65,6 +76,13 @@ const handlers = MiddlewareFactory.getDefaultMiddlewares();
handlers.unshift(new SaveRequestHandler());
```

### [Python](#tab/python)

```python
handlers = KiotaClientFactory.get_default_middleware(options=None)
handlers.append(SaveRequestHandler())
```

---

Next you need to create a delegate chain so the middleware handlers are registered in the right order.
Expand All @@ -84,6 +102,12 @@ var httpMessageHandler =
// this step is not required for TypeScript
```

### [Python](#tab/python)

```python
# this step is not required for Python
```

---

Finally, create a request adapter using an HTTP client. This adapter can then be used when submitting requests from the generated client. This design means different adapters/middleware can be used when calling APIs and therefore gives flexibility to how and when a middleware handler is used.
Expand All @@ -104,6 +128,14 @@ const adapter = new FetchRequestAdapter(authProvider, undefined, undefined, http
const client = createApiClient(adapter);
```

### [Python](#tab/python)

```python
http_client = KiotaClientFactory.create_with_custom_middleware(middleware=handlers)
adapter = HttpxRequestAdapter(auth_provider, http_client=http_client)
client = ApiClient(adapter) # the name of the client will vary based on your generation parameters
```

---

## Middleware handlers
Expand Down