-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/ollama-cohere-models
- Loading branch information
Showing
5 changed files
with
98 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Brevia Middleware classes""" | ||
from importlib.metadata import version | ||
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint | ||
from starlette.requests import Request | ||
from starlette.responses import Response | ||
from starlette.types import ASGIApp | ||
|
||
|
||
class VersionHeaderMiddleware(BaseHTTPMiddleware): | ||
"""Middleware to add version headers to response""" | ||
def __init__( | ||
self, | ||
app: ASGIApp, | ||
api_version: str = '', | ||
api_name: str = '', | ||
) -> None: | ||
super().__init__(app) | ||
self.api_version = api_version | ||
self.api_name = api_name | ||
|
||
async def dispatch( | ||
self, request: Request, call_next: RequestResponseEndpoint | ||
) -> Response: | ||
"""Add version header to response""" | ||
response = await call_next(request) | ||
response.headers['X-Brevia-Version'] = version('Brevia') | ||
# Add API version and name headers | ||
# about the custom API created with Brevia | ||
if self.api_name: | ||
response.headers['X-API-Name'] = self.api_name | ||
if self.api_version: | ||
response.headers['X-API-Version'] = self.api_version | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" Middleware module tests. """ | ||
from importlib.metadata import version | ||
import pytest | ||
from fastapi import FastAPI, Response | ||
from brevia.middleware import VersionHeaderMiddleware | ||
|
||
pytest_plugins = ('pytest_asyncio',) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_dispatch(): | ||
"""Test dispatch method of VersionHeaderMiddleware class.""" | ||
middleware = VersionHeaderMiddleware(app=FastAPI()) | ||
|
||
async def call_next_test(request): | ||
return Response() | ||
|
||
response = await middleware.dispatch(request=None, call_next=call_next_test) | ||
|
||
assert response.headers.get('X-Brevia-Version') == version('Brevia') | ||
assert response.headers.get('X-API-Version') is None | ||
assert response.headers.get('X-API-Name') is None | ||
|
||
middleware = VersionHeaderMiddleware( | ||
app=FastAPI(), | ||
api_version='1.0', | ||
api_name='Test API', | ||
) | ||
|
||
response = await middleware.dispatch(request=None, call_next=call_next_test) | ||
|
||
assert response.headers.get('X-Brevia-Version') == version('Brevia') | ||
assert response.headers.get('X-API-Version') == '1.0' | ||
assert response.headers.get('X-API-Name') == 'Test API' |