-
Notifications
You must be signed in to change notification settings - Fork 0
MPT-16326 Refactor seeding accounts #158
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,31 @@ | ||
| import logging | ||
| import os | ||
|
|
||
| from mpt_api_client.resources.accounts.account import Account | ||
| from seed.accounts.api_tokens import seed_api_token | ||
| from seed.accounts.buyer import seed_buyer | ||
| from seed.accounts.licensee import seed_licensee | ||
| from seed.accounts.module import seed_module | ||
| from seed.accounts.seller import seed_seller | ||
| from seed.accounts.user_group import seed_user_group | ||
| from seed.helper import init_resource | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def seed_accounts() -> None: # noqa: WPS217 | ||
| async def get_account() -> Account: # noqa: RUF029 async for compatibility purposes with init_resource | ||
| """Get account ID from environment variable.""" | ||
| account_id = os.getenv("CLIENT_ACCOUNT_ID") | ||
| if not account_id: | ||
| raise ValueError("CLIENT_ACCOUNT_ID environment variable is required") | ||
| return Account({"id": account_id}) | ||
|
Comment on lines
+16
to
+21
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. Fix Ruff findings: remove unused +class MissingClientAccountIdError(ValueError):
+ """CLIENT_ACCOUNT_ID environment variable is required."""
+
async def get_account() -> Account: # noqa: RUF029 async for compatibility purposes with init_resource
"""Get account ID from environment variable."""
account_id = os.getenv("CLIENT_ACCOUNT_ID")
if not account_id:
- raise ValueError("CLIENT_ACCOUNT_ID environment variable is required")
+ raise MissingClientAccountIdError
return Account({"id": account_id})…and then remove the now-unneeded
🧰 Tools🪛 Ruff (0.14.8)16-16: Unused Remove unused (RUF100) 20-20: Avoid specifying long messages outside the exception class (TRY003) 🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| async def seed_accounts() -> None: # noqa: WPS213 WPS217 | ||
| """Seed accounts data including account.""" | ||
| logger.debug("Seeding accounts ...") | ||
| await init_resource("accounts.account.id", get_account) | ||
|
|
||
| await seed_seller() | ||
| await seed_buyer() | ||
| await seed_module() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,27 @@ | ||
| import logging | ||
| import os | ||
|
|
||
| from dependency_injector.wiring import Provide, inject | ||
|
|
||
| from mpt_api_client import AsyncMPTClient | ||
| from mpt_api_client.resources.accounts.licensees import Licensee | ||
| from seed.container import Container | ||
| from seed.context import Context | ||
| from seed.helper import init_resource, require_context_id | ||
| from seed.static.static import ICON | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @inject | ||
| async def get_licensee( | ||
| context: Context = Provide[Container.context], | ||
| mpt_client: AsyncMPTClient = Provide[Container.mpt_client], | ||
| ) -> Licensee | None: | ||
| """Get licensee from context or fetch from API.""" | ||
| licensee_id = context.get_string("accounts.licensee.id") | ||
| if not licensee_id: | ||
| return None | ||
| try: | ||
| licensee = context.get_resource("accounts.licensee", licensee_id) | ||
| except ValueError: | ||
| licensee = None | ||
| if not isinstance(licensee, Licensee): | ||
| licensee = await mpt_client.accounts.licensees.get(licensee_id) | ||
| context.set_resource("accounts.licensee", licensee) | ||
| context["accounts.licensee.id"] = licensee.id | ||
| return licensee | ||
| return licensee | ||
|
|
||
|
|
||
| @inject | ||
| def build_licensee_data( # noqa: WPS238 | ||
| context: Context = Provide[Container.context], | ||
| ) -> dict[str, object]: | ||
| """Get licensee data dictionary for creation.""" | ||
| account_id = os.getenv("CLIENT_ACCOUNT_ID") | ||
| if not account_id: | ||
| raise ValueError("CLIENT_ACCOUNT_ID environment variable is required") | ||
| seller_id = context.get_string("accounts.seller.id") | ||
| if not seller_id: | ||
| raise ValueError("Seller ID is required in context") | ||
| buyer_id = context.get_string("accounts.buyer.id") | ||
| if not buyer_id: | ||
| raise ValueError("Buyer ID is required in context") | ||
| group = context.get_resource("accounts.user_group") | ||
| if group is None: | ||
| raise ValueError("User group is required in context") | ||
| account_id = require_context_id(context, "accounts.account.id", "creating licensee") | ||
| seller_id = require_context_id(context, "accounts.seller.id", "create licensee") | ||
| buyer_id = require_context_id(context, "accounts.buyer.id", "create licensee") | ||
| user_group_id = require_context_id(context, "accounts.user_group.id", "create licensee") | ||
|
Comment on lines
+20
to
+23
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. Unify 🤖 Prompt for AI Agents |
||
|
|
||
| licensee_type = "Client" | ||
| return { | ||
| "name": "E2E Seeded Licensee", | ||
|
|
@@ -65,39 +37,23 @@ def build_licensee_data( # noqa: WPS238 | |
| "buyer": {"id": buyer_id}, | ||
| "account": {"id": account_id}, | ||
| "eligibility": {"client": True, "partner": False}, | ||
| "groups": [{"id": group.id}], | ||
| "groups": [{"id": user_group_id}], | ||
| "type": licensee_type, | ||
| "status": "Enabled", | ||
| "defaultLanguage": "en-US", | ||
| } | ||
|
|
||
|
|
||
| @inject | ||
| async def init_licensee( | ||
| context: Context = Provide[Container.context], | ||
| mpt_client: AsyncMPTClient = Provide[Container.mpt_client], | ||
| ) -> Licensee: | ||
| """Get or create licensee.""" | ||
| licensee = await get_licensee(context=context, mpt_client=mpt_client) | ||
| if licensee is None: | ||
| licensee_data = build_licensee_data(context=context) | ||
| logger.debug("Creating licensee ...") | ||
| with ICON.open("rb") as icon_file: | ||
| created = await mpt_client.accounts.licensees.create(licensee_data, file=icon_file) | ||
| if isinstance(created, Licensee): | ||
| context.set_resource("accounts.licensee", created) | ||
| context["accounts.licensee.id"] = created.id | ||
| logger.info("Licensee created: %s", created.id) | ||
| return created | ||
| logger.warning("Licensee creation failed") # type: ignore[unreachable] | ||
| raise ValueError("Licensee creation failed") | ||
| logger.info("Licensee found: %s", licensee.id) | ||
| return licensee | ||
| async def create_licensee(mpt_client: AsyncMPTClient = Provide[Container.mpt_client]) -> Licensee: | ||
| """Create licensee.""" | ||
| licensee_data = build_licensee_data() | ||
| with ICON.open("rb") as icon_fd: | ||
| return await mpt_client.accounts.licensees.create(licensee_data, file=icon_fd) | ||
|
|
||
|
|
||
| @inject | ||
| async def seed_licensee() -> None: | ||
| """Seed licensee.""" | ||
| logger.debug("Seeding licensee ...") | ||
| await init_licensee() | ||
| await init_resource("accounts.licensee.id", create_licensee) | ||
| logger.info("Seeding licensee completed.") | ||
Uh oh!
There was an error while loading. Please reload this page.