-
Notifications
You must be signed in to change notification settings - Fork 0
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
Group Attributes of Class and Object elements of Google Wallet, to make it more maintainable. #6
Open
loechel
wants to merge
38
commits into
main
Choose a base branch
from
group_attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 18 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
5c23cd4
Group Attributes of Class and Object elements of Google Wallet, to ma…
loechel 0f80398
move attributes around
loechel 1e7671a
move attributes around
loechel 9b76185
rename file notification.py to message.py as it only contains message…
loechel f4d137b
fix mypy issues with __call_ order on multiple inheritance
loechel 246a3e4
move attributes around
loechel 89bb64c
start implementing transit tickets
loechel a8feb00
make credential_file depended on a path
loechel 7a327e5
make credential_file depended on a path
loechel 0c3e063
move attributes around
loechel ed7ac8e
more Classes and Objects, Types and Enums added and cleaned-up
loechel f18c7a0
fix spelling in tox.ini
loechel b55ee19
more Classes and Objects, Types and Enums added and cleaned-up
loechel 10f99e3
rename some classes to reflect Mixin character of those classes
loechel 8f73896
More documentation and revert change from model_dump to model_dump_js…
loechel 6802b85
cleanup models
loechel a9d3047
fix some classes
loechel 1c96007
add debug statement
loechel 63864c7
Update src/edutap/wallet_google/api.py
loechel dcaacbf
Update src/edutap/wallet_google/session.py
loechel 74d977c
add pydantic-settings and refactor session to use this.
loechel 7e6a7db
refactore settings
loechel 5e36e14
extend Settings
loechel 174135e
test fixtures
loechel 950d60b
test fixtures
loechel ec7222d
Merge branch 'main' into group_attributes
loechel 100b961
tox env handling
loechel 8b6b094
tox env handling, changed to default env vars
loechel 752dd79
fix enum equals method
loechel 773d230
env var check
loechel 665773f
try to fix tests
a2c80dc
try to fix settings for CI
loechel 1942abf
fix all tox tests
loechel 32f77dd
add kind attribute for retail classes
loechel 5bd87d3
fix classes
loechel ae617dd
switch requests param to json, as Content-Type-Header is not set
loechel c600f08
switch requests param to json, as Content-Type-Header is not set, rem…
loechel 6bf4ec9
switch requests param back to data, and explicitly set Content-Type-H…
loechel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
from .modelbase import GoogleWalletModel | ||
from .modelbase import GoogleWalletObjectReference | ||
from .modelbase import GoogleWalletObjectWithClassReferenceMixin | ||
from .modelcore import GoogleWalletModel | ||
from .models.primitives import Pagination | ||
from .models.primitives.enums import State | ||
from .models.primitives.notification import AddMessageRequest | ||
from .models.primitives.notification import Message | ||
from .models.primitives.message import AddMessageRequest | ||
from .models.primitives.message import Message | ||
from .registry import lookup_metadata | ||
from .registry import lookup_model | ||
from .registry import lookup_model_by_plural_name | ||
|
@@ -170,6 +170,8 @@ def update( | |
url=session_manager.url(name, f"/{resource_id}"), | ||
data=verified_json.encode("utf-8"), | ||
) | ||
logger.debug(verified_json.encode("utf-8")) | ||
# print(verified_json.encode("utf-8")) | ||
if response.status_code == 404: | ||
raise LookupError( | ||
f"Error 404, {name} {getattr(data, 'id', 'No ID')} not found: - {response.text}" | ||
|
@@ -374,15 +376,31 @@ def save_link( | |
for obj in objs: | ||
# first look if this is an object reference as dict | ||
if isinstance(obj, dict) and "id" in obj and len(obj.keys()) <= 2: | ||
obj = GoogleWalletObjectReference.model_validate(obj) | ||
if isinstance(obj, GoogleWalletObjectReference): | ||
payload[name].append(obj.model_dump(exclude_none=True, mode="json")) | ||
obj = GoogleWalletObjectWithClassReferenceMixin.model_validate(obj) | ||
if isinstance(obj, GoogleWalletObjectWithClassReferenceMixin): | ||
payload[name].append( | ||
obj.model_dump( | ||
# explicitly set to model_dump(mode="json") instead of model_dump_json due to problems | ||
# reported by jensens | ||
mode="json", | ||
exclude_none=True, | ||
exclude_unset=True, | ||
exclude_defaults=True, | ||
) | ||
) | ||
continue | ||
|
||
# otherwise it must be a registered model | ||
model = lookup_model_by_plural_name(name) | ||
obj = _validate_data(model, obj) | ||
obj_json = obj.model_dump(exclude_none=True, mode="json") | ||
obj_json = obj.model_dump( | ||
# explicitly set to model_dump(mode="json") instead of model_dump_json due to problems | ||
# reported by jensens | ||
mode="json", | ||
exclude_none=True, | ||
exclude_unset=True, | ||
exclude_defaults=True, | ||
) | ||
payload[name].append(obj_json) | ||
claims = { | ||
"iat": "", | ||
|
@@ -394,9 +412,9 @@ def save_link( | |
} | ||
signer = crypt.RSASigner.from_service_account_file(session_manager.credentials_file) | ||
jwt_string = jwt.encode(signer, claims).decode("utf-8") | ||
logger.debug( | ||
"JWT-Length: %d, is less than recommenden 1800: %s", | ||
logger.warning( | ||
"JWT-Length: %d, is larger than recommenden 1800: %s", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if this is worth a warning. It works anyway if it is longer. |
||
len(jwt_string), | ||
loechel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
len(jwt_string) <= 1800, | ||
len(jwt_string) >= 1800, | ||
) | ||
return f"{session_manager.save_url}/{jwt_string}" |
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 |
---|---|---|
@@ -1,43 +1,151 @@ | ||
from pydantic import BaseModel | ||
from pydantic import ConfigDict | ||
from .modelcore import GoogleWalletModel | ||
from .modelcore import GoogleWalletWithIdModel | ||
from .models.primitives import CallbackOptions | ||
from .models.primitives import GroupingInfo | ||
from .models.primitives import Image | ||
from .models.primitives import PassConstraints | ||
from .models.primitives import SecurityAnimation | ||
from .models.primitives.barcode import Barcode | ||
from .models.primitives.barcode import RotatingBarcode | ||
from .models.primitives.class_template_info import ClassTemplateInfo | ||
from .models.primitives.data import ImageModuleData | ||
from .models.primitives.data import InfoModuleData | ||
from .models.primitives.data import LinksModuleData | ||
from .models.primitives.data import TextModuleData | ||
from .models.primitives.enums import MultipleDevicesAndHoldersAllowedStatus | ||
from .models.primitives.enums import ViewUnlockRequirement | ||
from .models.primitives.message import Message | ||
from pydantic import Field | ||
|
||
|
||
class GoogleWalletModel(BaseModel): | ||
class GoogleWalletClassModel(GoogleWalletWithIdModel): | ||
""" | ||
Base model for all Google Wallet models. | ||
BaseModel for all Google Wallet Class Models. | ||
""" | ||
|
||
model_config = ConfigDict( | ||
extra="forbid", | ||
use_enum_values=True, | ||
# Templating and Visual Data | ||
jensens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
classTemplateInfo: ClassTemplateInfo | None = None | ||
imageModulesData: list[ImageModuleData] | None = None | ||
textModulesData: list[TextModuleData] | None = None | ||
linksModuleData: LinksModuleData | None = None | ||
infoModuleData: InfoModuleData | None = Field( | ||
description="deprecated", | ||
deprecated=True, | ||
exclude=True, | ||
default=None, | ||
) | ||
|
||
# Callback Options | ||
callbackOptions: CallbackOptions | None = None | ||
|
||
# Smarttap Options | ||
enableSmartTap: bool | None = None | ||
redemptionIssuers: list[str] | None = None # string (int64 format) | ||
|
||
# Barcode Options | ||
securityAnimation: SecurityAnimation | None = None | ||
|
||
# Security Options | ||
viewUnlockRequirement: ViewUnlockRequirement = ( | ||
ViewUnlockRequirement.VIEW_UNLOCK_REQUIREMENT_UNSPECIFIED | ||
) | ||
allowMultipleUsersPerObject: bool | None = Field( | ||
description="deprecated", | ||
deprecated=True, | ||
exclude=True, | ||
default=None, | ||
) | ||
multipleDevicesAndHoldersAllowedStatus: MultipleDevicesAndHoldersAllowedStatus = ( | ||
MultipleDevicesAndHoldersAllowedStatus.STATUS_UNSPECIFIED | ||
) | ||
|
||
# Design Options | ||
# hexBackgroundColor: str | None = None | ||
# logo: Image | None = None | ||
# wideLogo: Image | None = None | ||
# heroImage: Image | None = None | ||
wordMark: Image | None = Field( | ||
description="deprecated", | ||
deprecated=True, | ||
exclude=True, | ||
default=None, | ||
) | ||
|
||
|
||
class GoogleWalletWithIdModel(GoogleWalletModel): | ||
class GoogleWalletObjectModel(GoogleWalletWithIdModel): | ||
""" | ||
Base model for Google Wallet models with an identifier. | ||
Base model for all Google Wallet Object models. | ||
""" | ||
|
||
id: str | ||
classId: str | ||
version: str | None = Field( | ||
description="deprecated", deprecated=True, exclude=True, default=None | ||
) | ||
|
||
groupingInfo: GroupingInfo | None = None | ||
|
||
class GoogleWalletClassModel(GoogleWalletWithIdModel): | ||
# Templating and Visual Data | ||
imageModulesData: list[ImageModuleData] | None = None | ||
textModulesData: list[TextModuleData] | None = None | ||
linksModuleData: LinksModuleData | None = None | ||
infoModuleData: InfoModuleData | None = Field( | ||
description="deprecated", | ||
deprecated=True, | ||
exclude=True, | ||
default=None, | ||
) | ||
|
||
# Security Options | ||
passConstraints: PassConstraints | None = None | ||
|
||
# Smart Tap Option | ||
smartTapRedemptionValue: str | None = None | ||
|
||
# Barcode Options | ||
barcode: Barcode | None = None | ||
rotatingBarcode: RotatingBarcode | None = None | ||
|
||
|
||
class GoogleWalletObjectWithClassReferenceMixin( | ||
GoogleWalletModel, GoogleWalletWithIdModel | ||
): | ||
""" | ||
Base model for all Google Wallet Class models. | ||
Mixin for all Google Wallet Object with a classReferences attribute, that reflects the whole class data. | ||
""" | ||
|
||
classReference: GoogleWalletClassModel | None = None | ||
|
||
class GoogleWalletObjectModel(GoogleWalletWithIdModel): | ||
|
||
class GoogleWalletMessageableMixin: | ||
""" | ||
Base model for all Google Wallet Object models. | ||
Mixin for Google Wallet Classes or Objects that can retrieve Messages | ||
""" | ||
|
||
classId: str | ||
messages: list[Message] | None = None | ||
|
||
|
||
class GoogleWalletObjectReference(GoogleWalletWithIdModel): | ||
class GoogleWalletStyleableClassMixin: | ||
""" | ||
Mixin for Google Wallet Classes that can be styled. | ||
""" | ||
Model for all Google Wallet Object references. | ||
|
||
# Design Options | ||
hexBackgroundColor: str | None = None | ||
logo: Image | None = None | ||
wideLogo: Image | None = None | ||
heroImage: Image | None = None | ||
|
||
|
||
class GoogleWalletStyleableObjectMixin: | ||
""" | ||
Mixin for Google Wallet Objects that can be styled. | ||
|
||
TODO: Check if really all Objects that can be styled have all four attributes and also with this name. | ||
Potential Override necessary for programLogo, ... | ||
""" | ||
|
||
classId: str | None = None | ||
# Design Options | ||
hexBackgroundColor: str | None = None | ||
logo: Image | None = None | ||
wideLogo: Image | None = None | ||
heroImage: Image | None = None |
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,40 @@ | ||
from pydantic import BaseModel | ||
from pydantic import ConfigDict | ||
from pydantic import Field | ||
|
||
|
||
class GoogleWalletModel(BaseModel): | ||
""" | ||
Base Model for all Google Wallet Models. | ||
|
||
Sets a model_config for all Google Wallet Models that enforce that all attributes must be explicitly modeled, and trying to set an unknown attribute would raise an Exception. | ||
This Follows the Zen of Python (PEP 20) --> Explicit is better than implicit. | ||
""" | ||
|
||
model_config = ConfigDict( | ||
extra="forbid", | ||
# extra="ignore", | ||
# use_enum_values=True, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am pretty sure we want to use_enum_values to be true on serialization |
||
) | ||
|
||
|
||
class GoogleWalletWithKindMixin(BaseModel): | ||
""" | ||
Mixin Class for Google Wallet Models with an deprecated kind identifier. | ||
Explicit kind value should be provided by the inheriting concret class. | ||
""" | ||
|
||
kind: str | None = Field( | ||
description="deprecated", | ||
deprecated=True, | ||
exclude=True, | ||
default=None, | ||
) | ||
|
||
|
||
class GoogleWalletWithIdModel(BaseModel): | ||
""" | ||
Model for Google Wallet models with an identifier. | ||
""" | ||
|
||
id: str |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commented print should go before merge.