Skip to content
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
1 change: 1 addition & 0 deletions e2e_config.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"accounts.seller.id": "SEL-7310-3075",
"catalog.product.parameter_group.id": "PGR-7255-3950-0001",
"catalog.product.parameter.id": "PAR-7255-3950-0016",
"catalog.product.document.id": "PDC-7255-3950-0001",
"accounts.account.id": "ACC-9042-0088",
"accounts.buyer.account.id": "ACC-1086-6867",
"accounts.buyer.id": "BUY-1591-2112",
Expand Down
1 change: 1 addition & 0 deletions mpt_api_client/http/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(
headers=base_headers,
timeout=timeout,
transport=AsyncHTTPTransport(retries=retries),
follow_redirects=True,
)

async def request( # noqa: WPS211
Expand Down
8 changes: 7 additions & 1 deletion mpt_api_client/http/async_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from urllib.parse import urljoin

from mpt_api_client.constants import APPLICATION_JSON
from mpt_api_client.http.async_client import AsyncHTTPClient
from mpt_api_client.http.base_service import ServiceBase
from mpt_api_client.http.types import QueryParam, Response
Expand Down Expand Up @@ -69,6 +70,11 @@ async def _resource_action(
query_params: Additional query parameters.
"""
response = await self._resource_do_request(
resource_id, method, action, json=json, query_params=query_params
resource_id,
method,
action,
json=json,
query_params=query_params,
headers={"Accept": APPLICATION_JSON},
)
return self._model_class.from_response(response)
1 change: 1 addition & 0 deletions mpt_api_client/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(
headers=base_headers,
timeout=timeout,
transport=HTTPTransport(retries=retries),
follow_redirects=True,
)

def request( # noqa: WPS211
Expand Down
83 changes: 53 additions & 30 deletions mpt_api_client/http/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from urllib.parse import urljoin

from mpt_api_client.constants import APPLICATION_JSON
from mpt_api_client.exceptions import MPTError
from mpt_api_client.http.query_state import QueryState
from mpt_api_client.http.types import FileTypes, Response
from mpt_api_client.models import Collection, FileModel, ResourceData
Expand Down Expand Up @@ -60,7 +61,32 @@ def update(self, resource_id: str, resource_data: ResourceData) -> Model:
return self._resource_action(resource_id, "PUT", json=resource_data) # type: ignore[attr-defined, no-any-return]


class FileOperationsMixin[Model]:
class DownloadFileMixin[Model]:
"""Download file mixin."""

def download(self, resource_id: str, accept: str | None = None) -> FileModel:
"""Download the file for the given resource ID.

Args:
resource_id: Resource ID.
accept: The content type expected for the file.
If not provided, the content type will be fetched from the resource.

Returns:
File model containing the downloaded file.
"""
if not accept:
resource: Model = self._resource_action(resource_id, method="GET") # type: ignore[attr-defined]
accept = resource.content_type # type: ignore[attr-defined]
if not accept:
raise MPTError("Unable to download file. Content type not found in resource")
response: Response = self._resource_do_request( # type: ignore[attr-defined]
resource_id, method="GET", headers={"Accept": accept}
)
return FileModel(response)


class FilesOperationsMixin[Model](DownloadFileMixin[Model]):
"""Mixin that provides create and download methods for file-based resources."""

def create(
Expand Down Expand Up @@ -91,20 +117,6 @@ def create(

return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return]

def download(self, resource_id: str) -> FileModel:
"""Download the file for the given resource ID.

Args:
resource_id: Resource ID.

Returns:
File model containing the downloaded file.
"""
response: Response = self._resource_do_request( # type: ignore[attr-defined]
resource_id, method="GET", headers={"Accept": "*"}
)
return FileModel(response)


class CreateWithIconMixin[Model]:
"""Create resource with icon mixin."""
Expand Down Expand Up @@ -221,7 +233,32 @@ async def update(self, resource_id: str, resource_data: ResourceData) -> Model:
return await self._resource_action(resource_id, "PUT", json=resource_data) # type: ignore[attr-defined, no-any-return]


class AsyncFileOperationsMixin[Model]:
class AsyncDownloadFileMixin[Model]:
"""Download file mixin."""

async def download(self, resource_id: str, accept: str | None = None) -> FileModel:
"""Download the file for the given resource ID.

Args:
resource_id: Resource ID.
accept: The content type expected for the file.
If not provided, the content type will be fetched from the resource.

Returns:
File model containing the downloaded file.
"""
if not accept:
resource: Model = await self._resource_action(resource_id, method="GET") # type: ignore[attr-defined]
accept = resource.content_type # type: ignore[attr-defined]
if not accept:
raise MPTError("Unable to download file. Content type not found in resource")
response = await self._resource_do_request( # type: ignore[attr-defined]
resource_id, method="GET", headers={"Accept": accept}
)
return FileModel(response)


class AsyncFilesOperationsMixin[Model](AsyncDownloadFileMixin[Model]):
"""Async mixin that provides create and download methods for file-based resources."""

async def create(
Expand Down Expand Up @@ -253,20 +290,6 @@ async def create(

return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return]

async def download(self, resource_id: str) -> FileModel:
"""Download the file for the given resource ID.

Args:
resource_id: Resource ID.

Returns:
File model containing the downloaded file.
"""
response = await self._resource_do_request( # type: ignore[attr-defined]
resource_id, method="GET", headers={"Accept": "*"}
)
return FileModel(response)


class AsyncCreateWithIconMixin[Model]:
"""Create resource with icon mixin."""
Expand Down
3 changes: 2 additions & 1 deletion mpt_api_client/http/service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from urllib.parse import urljoin

from mpt_api_client.constants import APPLICATION_JSON
from mpt_api_client.http.base_service import ServiceBase
from mpt_api_client.http.client import HTTPClient
from mpt_api_client.http.types import QueryParam, Response
Expand Down Expand Up @@ -74,6 +75,6 @@ def _resource_action(
action,
json=json,
query_params=query_params,
headers={"Accept": "application/json"},
headers={"Accept": APPLICATION_JSON},
)
return self._model_class.from_response(response)
8 changes: 4 additions & 4 deletions mpt_api_client/resources/billing/credit_memo_attachments.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from mpt_api_client.http import AsyncService, Service
from mpt_api_client.http.mixins import (
AsyncCollectionMixin,
AsyncFileOperationsMixin,
AsyncFilesOperationsMixin,
AsyncModifiableResourceMixin,
CollectionMixin,
FileOperationsMixin,
FilesOperationsMixin,
ModifiableResourceMixin,
)
from mpt_api_client.models import Model
Expand All @@ -23,7 +23,7 @@ class CreditMemoAttachmentsServiceConfig:


class CreditMemoAttachmentsService(
FileOperationsMixin[CreditMemoAttachment],
FilesOperationsMixin[CreditMemoAttachment],
ModifiableResourceMixin[CreditMemoAttachment],
CollectionMixin[CreditMemoAttachment],
Service[CreditMemoAttachment],
Expand All @@ -33,7 +33,7 @@ class CreditMemoAttachmentsService(


class AsyncCreditMemoAttachmentsService(
AsyncFileOperationsMixin[CreditMemoAttachment],
AsyncFilesOperationsMixin[CreditMemoAttachment],
AsyncModifiableResourceMixin[CreditMemoAttachment],
AsyncCollectionMixin[CreditMemoAttachment],
AsyncService[CreditMemoAttachment],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from mpt_api_client.http import AsyncService, Service
from mpt_api_client.http.mixins import (
AsyncCollectionMixin,
AsyncFileOperationsMixin,
AsyncFilesOperationsMixin,
AsyncModifiableResourceMixin,
CollectionMixin,
FileOperationsMixin,
FilesOperationsMixin,
ModifiableResourceMixin,
)
from mpt_api_client.models import Model
Expand All @@ -23,7 +23,7 @@ class CustomLedgerAttachmentsServiceConfig:


class CustomLedgerAttachmentsService(
FileOperationsMixin[CustomLedgerAttachment],
FilesOperationsMixin[CustomLedgerAttachment],
ModifiableResourceMixin[CustomLedgerAttachment],
CollectionMixin[CustomLedgerAttachment],
Service[CustomLedgerAttachment],
Expand All @@ -33,7 +33,7 @@ class CustomLedgerAttachmentsService(


class AsyncCustomLedgerAttachmentsService(
AsyncFileOperationsMixin[CustomLedgerAttachment],
AsyncFilesOperationsMixin[CustomLedgerAttachment],
AsyncModifiableResourceMixin[CustomLedgerAttachment],
AsyncCollectionMixin[CustomLedgerAttachment],
AsyncService[CustomLedgerAttachment],
Expand Down
6 changes: 3 additions & 3 deletions mpt_api_client/resources/billing/custom_ledger_upload.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from mpt_api_client.http import AsyncService, Service
from mpt_api_client.http.mixins import AsyncFileOperationsMixin, FileOperationsMixin
from mpt_api_client.http.mixins import AsyncFilesOperationsMixin, FilesOperationsMixin
from mpt_api_client.models import Model


Expand All @@ -16,15 +16,15 @@ class CustomLedgerUploadServiceConfig:


class CustomLedgerUploadService(
FileOperationsMixin[CustomLedgerUpload],
FilesOperationsMixin[CustomLedgerUpload],
Service[CustomLedgerUpload],
CustomLedgerUploadServiceConfig,
):
"""Custom Ledger Upload service."""


class AsyncCustomLedgerUploadService(
AsyncFileOperationsMixin[CustomLedgerUpload],
AsyncFilesOperationsMixin[CustomLedgerUpload],
AsyncService[CustomLedgerUpload],
CustomLedgerUploadServiceConfig,
):
Expand Down
8 changes: 4 additions & 4 deletions mpt_api_client/resources/billing/invoice_attachments.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from mpt_api_client.http import AsyncService, Service
from mpt_api_client.http.mixins import (
AsyncCollectionMixin,
AsyncFileOperationsMixin,
AsyncFilesOperationsMixin,
AsyncModifiableResourceMixin,
CollectionMixin,
FileOperationsMixin,
FilesOperationsMixin,
ModifiableResourceMixin,
)
from mpt_api_client.models import Model
Expand All @@ -23,7 +23,7 @@ class InvoiceAttachmentsServiceConfig:


class InvoiceAttachmentsService(
FileOperationsMixin[InvoiceAttachment],
FilesOperationsMixin[InvoiceAttachment],
ModifiableResourceMixin[InvoiceAttachment],
CollectionMixin[InvoiceAttachment],
Service[InvoiceAttachment],
Expand All @@ -33,7 +33,7 @@ class InvoiceAttachmentsService(


class AsyncInvoiceAttachmentsService(
AsyncFileOperationsMixin[InvoiceAttachment],
AsyncFilesOperationsMixin[InvoiceAttachment],
AsyncModifiableResourceMixin[InvoiceAttachment],
AsyncCollectionMixin[InvoiceAttachment],
AsyncService[InvoiceAttachment],
Expand Down
8 changes: 4 additions & 4 deletions mpt_api_client/resources/billing/journal_attachments.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from mpt_api_client.http import AsyncService, Service
from mpt_api_client.http.mixins import (
AsyncCollectionMixin,
AsyncFileOperationsMixin,
AsyncFilesOperationsMixin,
AsyncModifiableResourceMixin,
CollectionMixin,
FileOperationsMixin,
FilesOperationsMixin,
ModifiableResourceMixin,
)
from mpt_api_client.models import Model
Expand All @@ -23,7 +23,7 @@ class JournalAttachmentsServiceConfig:


class JournalAttachmentsService(
FileOperationsMixin[JournalAttachment],
FilesOperationsMixin[JournalAttachment],
ModifiableResourceMixin[JournalAttachment],
CollectionMixin[JournalAttachment],
Service[JournalAttachment],
Expand All @@ -33,7 +33,7 @@ class JournalAttachmentsService(


class AsyncJournalAttachmentsService(
AsyncFileOperationsMixin[JournalAttachment],
AsyncFilesOperationsMixin[JournalAttachment],
AsyncModifiableResourceMixin[JournalAttachment],
AsyncCollectionMixin[JournalAttachment],
AsyncService[JournalAttachment],
Expand Down
8 changes: 4 additions & 4 deletions mpt_api_client/resources/billing/journal_upload.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from mpt_api_client.http import AsyncService, Service
from mpt_api_client.http.mixins import (
AsyncCollectionMixin,
AsyncFileOperationsMixin,
AsyncFilesOperationsMixin,
CollectionMixin,
FileOperationsMixin,
FilesOperationsMixin,
)
from mpt_api_client.models import Model

Expand All @@ -21,7 +21,7 @@ class JournalUploadServiceConfig:


class JournalUploadService(
FileOperationsMixin[JournalUpload],
FilesOperationsMixin[JournalUpload],
CollectionMixin[JournalUpload],
Service[JournalUpload],
JournalUploadServiceConfig,
Expand All @@ -30,7 +30,7 @@ class JournalUploadService(


class AsyncJournalUploadService(
AsyncFileOperationsMixin[JournalUpload],
AsyncFilesOperationsMixin[JournalUpload],
AsyncCollectionMixin[JournalUpload],
AsyncService[JournalUpload],
JournalUploadServiceConfig,
Expand Down
8 changes: 4 additions & 4 deletions mpt_api_client/resources/billing/ledger_attachments.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from mpt_api_client.http import AsyncService, Service
from mpt_api_client.http.mixins import (
AsyncCollectionMixin,
AsyncFileOperationsMixin,
AsyncFilesOperationsMixin,
AsyncModifiableResourceMixin,
CollectionMixin,
FileOperationsMixin,
FilesOperationsMixin,
ModifiableResourceMixin,
)
from mpt_api_client.models import Model
Expand All @@ -23,7 +23,7 @@ class LedgerAttachmentsServiceConfig:


class LedgerAttachmentsService(
FileOperationsMixin[LedgerAttachment],
FilesOperationsMixin[LedgerAttachment],
ModifiableResourceMixin[LedgerAttachment],
CollectionMixin[LedgerAttachment],
Service[LedgerAttachment],
Expand All @@ -33,7 +33,7 @@ class LedgerAttachmentsService(


class AsyncLedgerAttachmentsService(
AsyncFileOperationsMixin[LedgerAttachment],
AsyncFilesOperationsMixin[LedgerAttachment],
AsyncModifiableResourceMixin[LedgerAttachment],
AsyncCollectionMixin[LedgerAttachment],
AsyncService[LedgerAttachment],
Expand Down
Loading