Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(face): add general headers #104

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion alice/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union
from typing import Dict, Union

from pydantic import Field, model_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
Expand Down Expand Up @@ -35,6 +35,9 @@ class Config(BaseSettings):
default=True,
description="This optional feature allows users to configure specific caches during service invocation, optimizing the performance of the application.",
)
headers: Union[Dict[str, str], None] = Field(
default=None, description="Configure header to add to all the entry points"
)

@model_validator(mode="after")
def validate_urls(self) -> "Config":
Expand Down
37 changes: 26 additions & 11 deletions alice/face/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def from_config(config: Config) -> "Face":
send_agent=config.send_agent,
verbose=config.verbose,
session=session,
headers=config.headers,
)

def __init__(
Expand All @@ -31,33 +32,46 @@ def __init__(
timeout: Union[float, None] = None,
send_agent: bool = True,
verbose: bool = False,
headers: Union[Dict[str, str], None] = None,
):
self.api_key = api_key
self.url = url
self.timeout = timeout
self.send_agent = send_agent
self.verbose = verbose
self.session = session
self.headers = headers

def healthcheck(self) -> Response:
response = self.session.get(
url=self.url + "/healthcheck", headers={"apikey": self.api_key}
)
return response

def _get_headers(
self, headers: Union[Dict[str, str], None] = None
) -> Dict[str, str]:
function_headers = {}
config_headers = {}

if headers is not None:
function_headers = headers
if self.headers is not None:
config_headers = self.headers

headers = {"apikey": self.api_key} | function_headers | config_headers
return headers

def selfie(
self,
media: bytes,
extract_liveness: bool = True,
extract_face_profile: bool = True,
headers: Union[Dict[str, str], None] = None,
) -> Result[SelfieResult, FaceError]:
if headers is None:
headers = {}

response = self.session.post(
url=f"{self.url}/selfie",
headers={"apikey": self.api_key} | headers,
headers=self._get_headers(headers),
files={"media": media},
data={
"extract_liveness": extract_liveness,
Expand All @@ -74,12 +88,9 @@ def document(
image: bytes,
headers: Union[Dict[str, str], None] = None,
) -> Result[DocumentResult, FaceError]:
if headers is None:
headers = {}

response = self.session.post(
url=f"{self.url}/document",
headers={"apikey": self.api_key} | headers,
headers=self._get_headers(headers),
files={"image": image},
)

Expand All @@ -92,10 +103,11 @@ def match_profiles(
self,
face_profile_probe: bytes,
face_profile_target: bytes,
headers: Union[Dict[str, str], None] = None,
) -> Result[MatchResult, FaceError]:
response = self.session.post(
url=f"{self.url}/match/profiles",
headers={"apikey": self.api_key},
headers=self._get_headers(headers),
files={
"face_profile_probe": face_profile_probe,
"face_profile_target": face_profile_target,
Expand All @@ -107,11 +119,14 @@ def match_profiles(
return Failure(FaceError.from_response(response))

def match_media(
self, selfie_media: bytes, document_media: bytes
self,
selfie_media: bytes,
document_media: bytes,
headers: Union[Dict[str, str], None] = None,
) -> Result[MatchResult, FaceError]:
response = self.session.post(
url=f"{self.url}/match/media",
headers={"apikey": self.api_key},
headers=self._get_headers(headers),
files={"selfie_media": selfie_media, "document_media": document_media},
)
if response.status_code == 200:
Expand Down
Loading