-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: raw text data for gcp * fix: update cli test to conform with application json format * fix: fastapi editor agents to comply with application/json * fix: gcp editor agents to comply with application/json * docs: update fastapi docs to no longer use form data * fix: Vary header and specific allow origin * feat: add CORS middleware for fastapi to make it easier to use * feat: simpler CORS middleware for fastapi * refactor: use common origin matching for cors * docs: make docs display CORS middleware in reference section * docs: typo * fix: make mypy happy * fix: cors domain regex account for us deployment * chore: add todo for removing if else statement on Jan 31st * test: add unittest for the CORS regex.
- Loading branch information
1 parent
89d63ac
commit b575385
Showing
12 changed files
with
171 additions
and
46 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
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
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
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,3 @@ | ||
ENCORD_DOMAIN_REGEX = ( | ||
r"^https:\/\/(?:(?:cord-ai-development--[\w\d]+-[\w\d]+\.web.app)|(?:(?:dev|staging|app)\.(us\.)?encord\.com))$" | ||
) |
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,60 @@ | ||
""" | ||
Convenience method to easily extend FastAPI servers | ||
with the appropriate CORS Middleware to allow | ||
interactions from the Encord platform. | ||
""" | ||
|
||
import typing | ||
|
||
try: | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from starlette.types import ASGIApp | ||
except ModuleNotFoundError: | ||
print( | ||
'To use the `fastapi` dependencies, you must also install fastapi. `python -m pip install "fastapi[standard]"' | ||
) | ||
exit() | ||
|
||
from encord_agents.core.constants import ENCORD_DOMAIN_REGEX | ||
|
||
|
||
# Type checking does not work here because we do not enforce people to | ||
# install fastapi as they can use package for, e.g., task runner wo fastapi. | ||
class EncordCORSMiddleware(CORSMiddleware): # type: ignore [misc] | ||
""" | ||
Like a regular `fastapi.midleware.cors.CORSMiddleware` but matches against | ||
the Encord origin by default. | ||
**Example:** | ||
```python | ||
from fastapi import FastAPI | ||
from encord_agents.fastapi.cors import EncordCORSMiddleware | ||
app = FastAPI() | ||
app.add_middleware(EncordCORSMiddleware) | ||
``` | ||
The CORS middleware will allow POST requests from the Encord domain. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
app: ASGIApp, | ||
allow_origins: typing.Sequence[str] = (), | ||
allow_methods: typing.Sequence[str] = ("POST",), | ||
allow_headers: typing.Sequence[str] = (), | ||
allow_credentials: bool = False, | ||
allow_origin_regex: str = ENCORD_DOMAIN_REGEX, | ||
expose_headers: typing.Sequence[str] = (), | ||
max_age: int = 3600, | ||
) -> None: | ||
super().__init__( | ||
app, | ||
allow_origins, | ||
allow_methods, | ||
allow_headers, | ||
allow_credentials, | ||
allow_origin_regex, | ||
expose_headers, | ||
max_age, | ||
) |
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
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
Oops, something went wrong.