Skip to content

Commit 17cf8a0

Browse files
author
Robert Segal
committed
Updated mixins for create and update in account, added an update mixin in product
1 parent 8994da6 commit 17cf8a0

28 files changed

+1076
-929
lines changed

mpt_api_client/http/mixins.py

Lines changed: 0 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
from collections.abc import AsyncIterator, Iterator
32
from typing import Self
43
from urllib.parse import urljoin
@@ -113,77 +112,6 @@ def create(
113112
return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return]
114113

115114

116-
class CreateWithIconMixin[Model]:
117-
"""Create resource with icon mixin."""
118-
119-
def create(
120-
self,
121-
resource_data: ResourceData,
122-
icon: FileTypes,
123-
data_key: str,
124-
icon_key: str,
125-
) -> Model:
126-
"""Create resource with icon.
127-
128-
Args:
129-
resource_data: Resource data.
130-
data_key: Key for the resource data.
131-
icon: Icon image in jpg, png, GIF, etc.
132-
icon_key: Key for the icon.
133-
134-
Returns:
135-
Created resource.
136-
"""
137-
files: dict[str, FileTypes] = {}
138-
files[data_key] = (
139-
None,
140-
json.dumps(resource_data),
141-
APPLICATION_JSON,
142-
)
143-
files[icon_key] = icon
144-
response = self.http_client.request("post", self.path, files=files) # type: ignore[attr-defined]
145-
146-
return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return]
147-
148-
149-
class UpdateWithIconMixin[Model]:
150-
"""Update resource with icon mixin."""
151-
152-
def update(
153-
self,
154-
resource_id: str,
155-
resource_data: ResourceData,
156-
icon: FileTypes,
157-
data_key: str,
158-
icon_key: str,
159-
) -> Model:
160-
"""Update resource with icon.
161-
162-
Args:
163-
resource_id: Resource ID.
164-
resource_data: Resource data.
165-
data_key: Key for the resource data.
166-
icon: Icon image in jpg, png, GIF, etc.
167-
icon_key: Key for the icon.
168-
169-
Returns:
170-
Updated resource.
171-
"""
172-
files: dict[str, FileTypes] = {}
173-
files[data_key] = (
174-
None,
175-
json.dumps(resource_data),
176-
APPLICATION_JSON,
177-
)
178-
files[icon_key] = icon
179-
180-
url = urljoin(f"{self.path}/", resource_id) # type: ignore[attr-defined]
181-
182-
response = self.http_client.request("put", url, files=files) # type: ignore[attr-defined]
183-
184-
return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return]
185-
186-
187115
class AsyncCreateMixin[Model]:
188116
"""Create resource mixin."""
189117

@@ -286,77 +214,6 @@ async def create(
286214
return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return]
287215

288216

289-
class AsyncCreateWithIconMixin[Model]:
290-
"""Create resource with icon mixin."""
291-
292-
async def create(
293-
self,
294-
resource_data: ResourceData,
295-
icon: FileTypes,
296-
data_key: str,
297-
icon_key: str,
298-
) -> Model:
299-
"""Create resource with icon.
300-
301-
Args:
302-
resource_data: Resource data.
303-
data_key: Key for the resource data.
304-
icon: Icon image in jpg, png, GIF, etc.
305-
icon_key: Key for the icon.
306-
307-
Returns:
308-
Created resource.
309-
"""
310-
files: dict[str, FileTypes] = {}
311-
files[data_key] = (
312-
None,
313-
json.dumps(resource_data),
314-
APPLICATION_JSON,
315-
)
316-
files[icon_key] = icon
317-
response = await self.http_client.request("post", self.path, files=files) # type: ignore[attr-defined]
318-
319-
return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return]
320-
321-
322-
class AsyncUpdateWithIconMixin[Model]:
323-
"""Update resource with icon mixin."""
324-
325-
async def update(
326-
self,
327-
resource_id: str,
328-
resource_data: ResourceData,
329-
icon: FileTypes,
330-
data_key: str,
331-
icon_key: str,
332-
) -> Model:
333-
"""Update resource with icon.
334-
335-
Args:
336-
resource_id: Resource ID.
337-
resource_data: Resource data.
338-
data_key: Key for the resource data.
339-
icon: Icon image in jpg, png, GIF, etc.
340-
icon_key: Key for the icon.
341-
342-
Returns:
343-
Updated resource.
344-
"""
345-
files: dict[str, FileTypes] = {}
346-
files[data_key] = (
347-
None,
348-
json.dumps(resource_data),
349-
APPLICATION_JSON,
350-
)
351-
files[icon_key] = icon
352-
353-
url = urljoin(f"{self.path}/", resource_id) # type: ignore[attr-defined]
354-
355-
response = await self.http_client.request("put", url, files=files) # type: ignore[attr-defined]
356-
357-
return self._model_class.from_response(response) # type: ignore[attr-defined, no-any-return]
358-
359-
360217
class GetMixin[Model]:
361218
"""Get resource mixin."""
362219

mpt_api_client/resources/accounts/account.py

Lines changed: 6 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
1-
from typing import override
2-
31
from mpt_api_client.http import AsyncService, Service
42
from mpt_api_client.http.mixins import (
53
AsyncCollectionMixin,
6-
AsyncCreateWithIconMixin,
74
AsyncGetMixin,
8-
AsyncUpdateWithIconMixin,
95
CollectionMixin,
10-
CreateWithIconMixin,
116
GetMixin,
12-
UpdateWithIconMixin,
137
)
14-
from mpt_api_client.http.types import FileTypes
158
from mpt_api_client.models import Model
16-
from mpt_api_client.models.model import ResourceData
179
from mpt_api_client.resources.accounts.accounts_users import (
1810
AccountsUsersService,
1911
AsyncAccountsUsersService,
2012
)
2113
from mpt_api_client.resources.accounts.mixins import (
22-
ActivatableMixin,
23-
AsyncActivatableMixin,
24-
AsyncEnablableMixin,
25-
AsyncValidateMixin,
26-
EnablableMixin,
27-
ValidateMixin,
14+
AccountMixin,
15+
AsyncAccountMixin,
2816
)
2917

3018

@@ -38,78 +26,19 @@ class AccountsServiceConfig:
3826
_endpoint = "/public/v1/accounts/accounts"
3927
_model_class = Account
4028
_collection_key = "data"
29+
_upload_file_key = "logo"
30+
_upload_data_key = "account"
4131

4232

4333
class AccountsService(
44-
CreateWithIconMixin[Account],
45-
UpdateWithIconMixin[Account],
46-
ActivatableMixin[Account],
47-
EnablableMixin[Account],
48-
ValidateMixin[Account],
34+
AccountMixin[Account],
4935
GetMixin[Account],
5036
CollectionMixin[Account],
5137
Service[Account],
5238
AccountsServiceConfig,
5339
):
5440
"""Accounts service."""
5541

56-
@override
57-
def create(
58-
self,
59-
resource_data: ResourceData,
60-
logo: FileTypes,
61-
data_key: str = "account",
62-
icon_key: str = "logo",
63-
) -> Account:
64-
"""
65-
Create a new account with logo.
66-
67-
Args:
68-
resource_data (ResourceData): Account data.
69-
logo: Logo image in jpg, png, GIF, etc.
70-
data_key: Key for the account data.
71-
icon_key: Key for the logo.
72-
73-
Returns:
74-
Account: The created account.
75-
"""
76-
return super().create(
77-
resource_data=resource_data,
78-
icon=logo,
79-
data_key=data_key,
80-
icon_key=icon_key,
81-
)
82-
83-
@override
84-
def update(
85-
self,
86-
resource_id: str,
87-
resource_data: ResourceData,
88-
logo: FileTypes,
89-
data_key: str = "account",
90-
icon_key: str = "logo",
91-
) -> Account:
92-
"""
93-
Update an existing account with logo.
94-
95-
Args:
96-
resource_id (str): The ID of the account to update.
97-
resource_data (ResourceData): Account data.
98-
logo: Logo image in jpg, png, GIF, etc.
99-
data_key: Key for the account data.
100-
icon_key: Key for the logo.
101-
102-
Returns:
103-
Account: The updated account.
104-
"""
105-
return super().update(
106-
resource_id=resource_id,
107-
resource_data=resource_data,
108-
icon=logo,
109-
data_key=data_key,
110-
icon_key=icon_key,
111-
)
112-
11342
def users(self, account_id: str) -> AccountsUsersService:
11443
"""Return account users service."""
11544
return AccountsUsersService(
@@ -118,75 +47,14 @@ def users(self, account_id: str) -> AccountsUsersService:
11847

11948

12049
class AsyncAccountsService(
121-
AsyncCreateWithIconMixin[Account],
122-
AsyncUpdateWithIconMixin[Account],
123-
AsyncActivatableMixin[Account],
124-
AsyncEnablableMixin[Account],
125-
AsyncValidateMixin[Account],
50+
AsyncAccountMixin[Account],
12651
AsyncGetMixin[Account],
12752
AsyncCollectionMixin[Account],
12853
AsyncService[Account],
12954
AccountsServiceConfig,
13055
):
13156
"""Async Accounts service."""
13257

133-
@override
134-
async def create(
135-
self,
136-
resource_data: ResourceData,
137-
logo: FileTypes,
138-
data_key: str = "account",
139-
icon_key: str = "logo",
140-
) -> Account:
141-
"""
142-
Create a new account with logo.
143-
144-
Args:
145-
resource_data (ResourceData): Account data.
146-
logo: Logo image in jpg, png, GIF, etc.
147-
data_key: Key for the account data.
148-
icon_key: Key for the logo.
149-
150-
Returns:
151-
Account: The created account.
152-
"""
153-
return await super().create(
154-
resource_data=resource_data,
155-
icon=logo,
156-
data_key=data_key,
157-
icon_key=icon_key,
158-
)
159-
160-
@override
161-
async def update(
162-
self,
163-
resource_id: str,
164-
resource_data: ResourceData,
165-
logo: FileTypes,
166-
data_key: str = "account",
167-
icon_key: str = "logo",
168-
) -> Account:
169-
"""
170-
Update an existing account with logo.
171-
172-
Args:
173-
resource_id (str): The ID of the account to update.
174-
resource_data (ResourceData): Account data.
175-
logo: Logo image in jpg, png, GIF, etc.
176-
data_key: Key for the account data.
177-
icon_key: Key for the logo.
178-
179-
Returns:
180-
Account: The updated account.
181-
"""
182-
return await super().update(
183-
resource_id=resource_id,
184-
resource_data=resource_data,
185-
icon=logo,
186-
data_key=data_key,
187-
icon_key=icon_key,
188-
)
189-
19058
def users(self, account_id: str) -> AsyncAccountsUsersService:
19159
"""Return account users service."""
19260
return AsyncAccountsUsersService(

0 commit comments

Comments
 (0)