Skip to content

Commit

Permalink
Merge pull request #64 from alice-biometrics/feature/make-it-compatib…
Browse files Browse the repository at this point in the history
…le-with-pydantic-v2

chore(pydantic): make it compatible with future pydantic release
  • Loading branch information
miguel-lorenzo committed Jun 30, 2023
2 parents 3eb3d9c + 394b856 commit ad5f300
Show file tree
Hide file tree
Showing 40 changed files with 295 additions and 270 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ jobs:
run: lume -test
- name: Example [Onboarding]
run: python examples/onboarding.py
- name: Example [Onboarding Get Users]
run: python examples/onboarding_get_users.py
- name: Example [Onboarding Report V0]
run: python examples/onboarding_report_v0.py
- name: Example [Onboarding with Identification]
Expand Down
3 changes: 2 additions & 1 deletion alice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

ROOT_PATH = os.path.abspath(os.path.dirname(__file__))

__version__ = open(f"{ROOT_PATH}/VERSION").read()[:-1]
with open(f"{ROOT_PATH}/VERSION") as f:
__version__ = f.read()[:-1]

__all__ = public_api.__all__
7 changes: 4 additions & 3 deletions alice/auth/auth_errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Dict

from meiga import Error
from requests import Response
Expand All @@ -9,7 +10,7 @@
class AuthError(Error):
operation: str
code: int
message: Dict[str, str]
message: dict[str, str] | None # type: ignore

def __str__(self) -> str:
return self.__repr__()
Expand All @@ -18,7 +19,7 @@ def __repr__(self) -> str:
return f"[AuthError: [operation: {self.operation} | code: {self.code} | message: {self.message}]]"

@staticmethod
def from_response(operation: str, response: Response) -> "AuthError":
def from_response(operation: str, response: Response) -> AuthError:
code = response.status_code
try:
message = response.json()
Expand Down
5 changes: 2 additions & 3 deletions alice/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Union

from pydantic.fields import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field
from requests import Session


Expand All @@ -20,4 +19,4 @@ class Config:
)
send_agent: bool = Field(default=True)
verbose: bool = Field(default=False)
session: Union[Session, None] = None
session: Union[Session, None] = Field(default=None)
3 changes: 1 addition & 2 deletions alice/onboarding/models/bounding_box.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pydantic import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field


class BoundingBox(BaseModel):
Expand Down
3 changes: 1 addition & 2 deletions alice/onboarding/models/report/checks/check.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Optional

from pydantic import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field


class Check(BaseModel):
Expand Down
7 changes: 3 additions & 4 deletions alice/onboarding/models/report/compliance/device_out.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Optional
from typing import Union

from pydantic import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field


class DeviceOut(BaseModel):
Expand All @@ -14,7 +13,7 @@ class DeviceOut(BaseModel):
platform: str
platform_version: str
model: str
ip: Optional[str] = None
ip: Union[str, None] = Field(default=None)

def __hash__(self) -> int:
return hash((type(self),) + tuple(self.__dict__.values()))
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from pydantic.main import BaseModel
from pydantic import BaseModel

from alice.onboarding.models.report.compliance.device_out import DeviceOut
from alice.onboarding.models.report.compliance.user_event_out import UserEventOut
Expand Down
4 changes: 2 additions & 2 deletions alice/onboarding/models/report/document/document_field.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List, Optional

from meiga import Error, Result, Success, isFailure
from pydantic.main import BaseModel
from pydantic import BaseModel, Field

from alice.onboarding.models.report.checks.check import Check

Expand All @@ -14,7 +14,7 @@ class ReportV1Field(BaseModel):
name: str
value: Optional[str]
score: Optional[int]
checks: List[Check] = []
checks: List[Check] = Field(default=[])

def get_check(self, check_key: str) -> Result[Check, Error]:
for doc_check in self.checks:
Expand Down
9 changes: 4 additions & 5 deletions alice/onboarding/models/report/document/document_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from typing import List, Union

from meiga import Error, Result, Success, isFailure
from pydantic import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field

from alice.onboarding.models.report.checks.check import Check
from alice.onboarding.models.report.document.document_field import ReportV1Field
Expand All @@ -16,9 +15,9 @@


class DocumentSidesDetailReport(BaseModel):
front: Union[DocumentSideReport, None] = None
back: Union[DocumentSideReport, None] = None
internal: Union[DocumentSideReport, None] = None
front: Union[DocumentSideReport, None] = Field(default=None)
back: Union[DocumentSideReport, None] = Field(default=None)
internal: Union[DocumentSideReport, None] = Field(default=None)

def get_completed_sides(self) -> int:
completed_sides = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pydantic import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field

from alice.onboarding.models.report.document.document_type import DocumentType

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from datetime import datetime
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Union

from meiga import Error, Result, Success, isFailure
from pydantic import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field

from alice.onboarding.models.report.document.document_field import ReportV1Field
from alice.onboarding.models.report.document.document_side import DocumentSide
Expand All @@ -26,8 +25,8 @@ class DocumentSideReport(BaseModel):
description="Document side media resources"
)
meta: DocumentSideReportMeta
created_at: Optional[datetime]
forensics_scores: Optional[Dict[str, Any]]
created_at: Union[datetime, None] = Field(default=None)
forensics_scores: Union[Dict[str, Any], None] = Field(default=None)

def get_field(self, field_name: str) -> Result[ReportV1Field, Error]:
for side_field in self.fields:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pydantic import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field


class DocumentSideReportMeta(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import datetime

from pydantic import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field

from alice.onboarding.models.report.document.document_side import DocumentSide

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class OtherTrustedDocumentReport(BaseModel):
"""
A other trusted document report_v1 collects all the information extracted for a document during the onboarding process
The other trusted document report_v1 collects all the information extracted for a document during the onboarding process
"""

id: str = Field(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Optional

from pydantic import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field


class OtherTrustedDocumentReportMeta(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion alice/onboarding/models/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Report(BaseModel):
min_length=16,
max_length=36,
)
version: int = Field(default=1, description="Report version", const=True)
version: int = Field(default=1, description="Report version")
created_at: datetime = Field(description="Report creation time in ISO 8601 format")
summary: ReportSummary = Field(description="User summary")
selfies: List[SelfieReport] = Field(description="It collects all user selfies")
Expand Down
3 changes: 1 addition & 2 deletions alice/onboarding/models/report/selfie/selfie_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from typing import Dict, List, Optional

from meiga import Error, Result, Success, isFailure
from pydantic import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field

from alice.onboarding.models.report.checks.check import Check
from alice.onboarding.models.report.shared.href import Href
Expand Down
3 changes: 1 addition & 2 deletions alice/onboarding/models/report/shared/bounding_box.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pydantic import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field


class BoundingBox(BaseModel):
Expand Down
3 changes: 1 addition & 2 deletions alice/onboarding/models/report/shared/href.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Dict, Optional

from pydantic import Field
from pydantic.main import BaseModel
from pydantic import BaseModel, Field

from alice.onboarding.models.report.shared.bounding_box import BoundingBox

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional

from pydantic.main import BaseModel
from pydantic import BaseModel


class ExternalUserData(BaseModel):
Expand Down
8 changes: 4 additions & 4 deletions alice/onboarding/models/user_info.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Optional

from pydantic import BaseModel
from pydantic import BaseModel, Field


class UserInfo(BaseModel):
first_name: Optional[str] = None
last_name: Optional[str] = None
email: Optional[str] = None
first_name: Optional[str] = Field(default=None)
last_name: Optional[str] = Field(default=None)
email: Optional[str] = Field(default=None)
58 changes: 17 additions & 41 deletions alice/onboarding/onboarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,15 @@ def get_users_status(
self,
verbose: Optional[bool] = False,
page: int = 1,
page_size: int = 0,
page_size: int = 10,
descending: bool = True,
authorized: bool = False,
sort_by: Union[str, None] = None,
filter_field: Union[str, None] = None,
filter_value: Union[str, None] = None,
) -> Result[List[Dict[str, str]], Union[OnboardingError, AuthError]]:
) -> Result[
Dict[str, Union[List[Dict[str, str]], int]], Union[OnboardingError, AuthError]
]:
"""
Returns every UserStatus available for all the Users in the onboarding platform ordered by creation date.
Expand All @@ -301,7 +303,7 @@ def get_users_status(
Returns
-------
A Result where if the operation is successful it returns list of dict which represent the status of each user.
A Result where if the operation is successful it returns a dict which represent the status of each user.
Otherwise, it returns an OnboardingError or AuthError.
"""
verbose = self.verbose or verbose
Expand Down Expand Up @@ -381,7 +383,11 @@ def add_user_feedback(

@early_return
def add_selfie(
self, user_id: str, media_data: bytes, verbose: Optional[bool] = False
self,
user_id: str,
media_data: bytes,
wait_for_completion: Optional[bool] = True,
verbose: Optional[bool] = False,
) -> Result[bool, Union[OnboardingError, AuthError]]:
"""
Expand All @@ -394,6 +400,8 @@ def add_selfie(
User identifier
media_data
Binary media data.
wait_for_completion
This setting specifies whether or not the request should return immediately or wait for the operation to complete before returning.
verbose
Used for print service response as well as the time elapsed
Expand All @@ -405,10 +413,13 @@ def add_selfie(
"""
verbose = self.verbose or verbose
response = self.onboarding_client.add_selfie(
user_id=user_id, media_data=media_data, verbose=verbose
user_id=user_id,
media_data=media_data,
wait_for_completion=wait_for_completion,
verbose=verbose,
).unwrap_or_return()

if response.status_code == 200:
if response.status_code in [200, 201]:
return isSuccess
else:
return Failure(
Expand Down Expand Up @@ -1153,41 +1164,6 @@ def screening_monitor_delete(
)
)

@early_return
def screening_monitor_open_alerts(
self, start_index: int = 0, size: int = 100, verbose: bool = False
) -> Result[bool, Union[OnboardingError, AuthError]]:
"""
Retrieves from the monitoring list the users with open alerts
Parameters
----------
start_index
DB index to start (0-2147483647)
size
Numbers of alerts to return (1-100).
verbose
Used for print service response as well as the time elapsed
Returns
-------
A Result where if the operation is successful it returns a dictionary.
Otherwise, it returns an OnboardingError or AuthError.
"""
verbose = self.verbose or verbose
response = self.onboarding_client.screening_monitor_open_alerts(
start_index=start_index, size=size, verbose=verbose
).unwrap_or_return()

if response.status_code == 200:
return Success(response.json())
else:
return Failure(
OnboardingError.from_response(
operation="screening_monitor_open_alerts", response=response
)
)

@early_return
def identify_user(
self,
Expand Down
Loading

0 comments on commit ad5f300

Please sign in to comment.