Skip to content

Commit f1055fc

Browse files
committed
add customers get by email
1 parent d683492 commit f1055fc

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

code_generator/4.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,40 @@ paths:
507507
'500':
508508
description: Internal server error
509509
summary: "+\n getByName"
510+
/customers/getByEmail/:
511+
post:
512+
requestBody:
513+
content:
514+
application/json:
515+
schema:
516+
properties:
517+
company_id:
518+
type: string
519+
email:
520+
type: string
521+
offset:
522+
type: string
523+
qty:
524+
type: string
525+
required:
526+
- email
527+
- company_id
528+
type: object
529+
responses:
530+
'200':
531+
content:
532+
application/json:
533+
schema:
534+
type: object
535+
description: Successful response
536+
'400':
537+
description: Bad request
538+
'401':
539+
description: Unauthorized
540+
'500':
541+
description: Internal server error
542+
summary: "+\n getByEmail"
543+
510544
/customers/getByNumber/:
511545
post:
512546
requestBody:

moloni/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.3.12'
1+
__version__ = '0.3.14'

moloni/api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
from .customers_client import CustomersCountModifiedSinceModel
7979
from .customers_client import CustomersDeleteModel
8080
from .customers_client import CustomersGetAllModel
81+
from .customers_client import CustomersGetByEmailModel
8182
from .customers_client import CustomersGetByNameModel
8283
from .customers_client import CustomersGetByNumberModel
8384
from .customers_client import CustomersGetBySearchModel
@@ -435,6 +436,7 @@
435436
"CustomersCountModifiedSinceModel",
436437
"CustomersDeleteModel",
437438
"CustomersGetAllModel",
439+
"CustomersGetByEmailModel",
438440
"CustomersGetByNameModel",
439441
"CustomersGetByNumberModel",
440442
"CustomersGetBySearchModel",

moloni/api/customers_client.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,50 @@ def request(self) -> ApiResponse:
400400
raise ValueError("Client not initialized. Use the 'connect' method.")
401401

402402

403+
class CustomersGetByEmailModel(ApiRequestModel):
404+
company_id: Union[str, int]
405+
email: Optional[str] = None
406+
offset: Optional[Union[str, int]] = 0
407+
qty: Optional[Union[str, int]] = 25
408+
409+
def request(self) -> ApiResponse:
410+
"""
411+
request(self) -> ApiResponse
412+
413+
Make an API request using the initialized client.
414+
415+
This method checks if the `_api_client` attribute is set (i.e., if the client has been initialized via the `connect` method).
416+
If the client is initialized, it will make an API request using the provided method name and the model's data,
417+
excluding the `_api_client` attribute itself from the request payload. If the client is not initialized, it will raise a `ValueError`.
418+
419+
Returns:
420+
The response from the API.
421+
422+
Raises:
423+
ValueError: If the client is not initialized via the `connect` method.
424+
425+
Example:
426+
427+
# Assuming you have a model instance `request_model` and an API client `api_client`
428+
429+
..code-block:: python
430+
431+
with request_model.connect(auth_config=auth_config) as api:
432+
response = api.request()
433+
434+
# The above example assumes that the `connect` method has been used to initialize the client.
435+
# The request method then sends the model's data to the API and returns the API's response.
436+
437+
"""
438+
if hasattr(self, "_api_client"):
439+
response = self._api_client.get_by_email(
440+
self.model_dump(exclude={"_api_client"}, exclude_unset=True)
441+
)
442+
return response
443+
else:
444+
raise ValueError("Client not initialized. Use the 'connect' method.")
445+
446+
403447
class CustomersGetByNumberModel(ApiRequestModel):
404448
company_id: Union[str, int]
405449
number: Optional[str] = None
@@ -1025,6 +1069,35 @@ def get_by_name(self, data: Union[CustomersGetByNameModel, dict], **kwargs):
10251069
fill_query_params(kwargs.pop("path"), self.version), data={**data, **kwargs}
10261070
)
10271071

1072+
@endpoint("/<version>/customers/getByEmail/", method="post")
1073+
def get_by_email(self, data: Union[CustomersGetByEmailModel, dict], **kwargs):
1074+
"""
1075+
get_by_email(self, data: Union[CustomersGetByEmailModel, dict], **kwargs)
1076+
1077+
Args:
1078+
1079+
data (Union[CustomersGetByEmailModel, dict]): A model instance or dictionary containing the following fields:
1080+
1081+
- company_id (Union[str, int]): company_id of the CustomersGetByEmailModel.
1082+
1083+
- email (str): email of the CustomersGetByEmailModel.
1084+
1085+
- offset (str): offset of the CustomersGetByEmailModel.
1086+
1087+
- qty (str): qty of the CustomersGetByEmailModel.
1088+
1089+
1090+
1091+
Returns:
1092+
ApiResponse: The response from the API.
1093+
"""
1094+
1095+
data = validate_data(data, self.validate, CustomersGetByEmailModel)
1096+
1097+
return self._request(
1098+
fill_query_params(kwargs.pop("path"), self.version), data={**data, **kwargs}
1099+
)
1100+
10281101
@endpoint("/<version>/customers/getByNumber/", method="post")
10291102
def get_by_number(self, data: Union[CustomersGetByNumberModel, dict], **kwargs):
10301103
"""

tests/customers_client_test.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
CustomersDeleteModel,
1313
CustomersGetAllModel,
1414
CustomersGetByNameModel,
15+
CustomersGetByEmailModel,
1516
CustomersGetByNumberModel,
1617
CustomersGetBySearchModel,
1718
CustomersGetByVatModel,
@@ -653,6 +654,79 @@ def test_get_by_name_from_model(self, mock_request):
653654
except NoMoreRecords:
654655
pass
655656

657+
@patch.object(CustomersClient, "_request")
658+
def test_get_by_email(self, mock_request):
659+
# Mock the Response object
660+
mock_response = Mock(spec=Response)
661+
mock_response.json.return_value = {"some_key": "some_value"}
662+
mock_response.status_code = 200
663+
mock_response.headers = {"Content-Type": "application/json"}
664+
665+
# Create the ApiResponse object with the mocked Response
666+
mock_request.return_value = ApiResponse(
667+
response=mock_response, request_data={"qty": 10, "offset": 0}
668+
)
669+
670+
model_data = CustomersGetByEmailModel(
671+
company_id="sample_value",
672+
email="email",
673+
offset="offset",
674+
qty="qty",
675+
)
676+
response = self.client.get_by_email(data=model_data)
677+
678+
# Assertions
679+
self.assertIsInstance(response, ApiResponse)
680+
self.assertEqual(response.payload, {"some_key": "some_value"})
681+
self.assertEqual(response.status_code, 200)
682+
self.assertEqual(response.headers["Content-Type"], "application/json")
683+
mock_request.assert_called_once()
684+
685+
# Test pagination functionality
686+
try:
687+
next_params = response.next(qty=5)
688+
self.assertEqual(next_params["offset"], 10)
689+
self.assertEqual(next_params["qty"], 5)
690+
except NoMoreRecords:
691+
pass
692+
693+
@patch.object(CustomersClient, "_request")
694+
def test_get_by_email_from_model(self, mock_request):
695+
# Mock the Response object
696+
mock_response = Mock(spec=Response)
697+
mock_response.json.return_value = {"some_key": "some_value"}
698+
mock_response.status_code = 200
699+
mock_response.headers = {"Content-Type": "application/json"}
700+
701+
# Create the ApiResponse object with the mocked Response
702+
mock_request.return_value = ApiResponse(
703+
response=mock_response, request_data={"qty": 10, "offset": 0}
704+
)
705+
706+
model_data = CustomersGetByEmailModel(
707+
company_id="sample_value",
708+
email="email",
709+
offset="offset",
710+
qty="qty",
711+
)
712+
with model_data.connect() as api:
713+
response = api.request()
714+
715+
# Assertions
716+
self.assertIsInstance(response, ApiResponse)
717+
self.assertEqual(response.payload, {"some_key": "some_value"})
718+
self.assertEqual(response.status_code, 200)
719+
self.assertEqual(response.headers["Content-Type"], "application/json")
720+
mock_request.assert_called_once()
721+
722+
# Test pagination functionality
723+
try:
724+
next_params = response.next(qty=5)
725+
self.assertEqual(next_params["offset"], 10)
726+
self.assertEqual(next_params["qty"], 5)
727+
except NoMoreRecords:
728+
pass
729+
656730
@patch.object(CustomersClient, "_request")
657731
def test_get_by_number(self, mock_request):
658732
# Mock the Response object

0 commit comments

Comments
 (0)