import logging from owsclient import OwsClient from pydantic import BaseModel, Field logger = logging.getLogger(__name__) class Vendor(BaseModel): vendor_id: int name: str = Field(alias="account_name") brand: str = Field(alias="company_brand") class Subaccount(BaseModel): vendor_id: int subaccount_id: int name: str = Field(alias="subaccount_name") class VendorLookup(BaseModel): vendor_id: int vendor_uuid: str = Field(alias="uuid") class LookupVendorsDataloaderResponse(BaseModel): vendors: list[VendorLookup | None] class Feature(BaseModel): feature_id: int feature_name: str class GetFeaturesResponse(BaseModel): items: list[Feature] class OwsAccountClient: service_name = "ows-account" def __init__(self, ows_client: OwsClient) -> None: self.ows_client = ows_client def get_vendor(self, vendor_id: int) -> Vendor: response = self.ows_client.get(self.service_name, path=f"/vendor/{vendor_id}") response.raise_for_status() return Vendor.model_validate_json(response.content) def get_subaccount(self, subaccount_id: int) -> Subaccount: response = self.ows_client.get( self.service_name, path=f"/subaccount/{subaccount_id}", ) response.raise_for_status() return Subaccount.model_validate_json(response.content) def lookup_vendors_by_uuids(self, uuids: list[str]) -> list[VendorLookup]: if not uuids: return [] response = self.ows_client.post( service_name=self.service_name, path="/lookup/vendors/uuids/", json={"uuids": uuids}, ) response.raise_for_status() response_obj = LookupVendorsDataloaderResponse.model_validate_json( response.content, ) # Remove all the null values in the Dataloader formatted response return [vendor for vendor in response_obj.vendors if vendor] def lookup_vendors_by_vendor_ids(self, vendor_ids: list[int]) -> list[VendorLookup]: if not vendor_ids: return [] response = self.ows_client.post( service_name=self.service_name, path="/lookup/vendors/vendor-ids/", json={"vendor_ids": vendor_ids}, ) response.raise_for_status() response_obj = LookupVendorsDataloaderResponse.model_validate_json( response.content, ) return [vendor for vendor in response_obj.vendors if vendor] def get_vendor_features(self, vendor_id: int) -> list[Feature]: response = self.ows_client.get( self.service_name, path=f"/vendor/{vendor_id}/features", ) response.raise_for_status() response_obj = GetFeaturesResponse.model_validate_json(response.content) return response_obj.items