From f1055fc83238bb860e0e6473e1611117a8ec5ec7 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 4 Nov 2024 09:58:41 +0000 Subject: [PATCH] add customers get by email --- code_generator/4.yaml | 34 ++++++++++++++++ moloni/__version__.py | 2 +- moloni/api/__init__.py | 2 + moloni/api/customers_client.py | 73 +++++++++++++++++++++++++++++++++ tests/customers_client_test.py | 74 ++++++++++++++++++++++++++++++++++ 5 files changed, 184 insertions(+), 1 deletion(-) diff --git a/code_generator/4.yaml b/code_generator/4.yaml index 3ff824a..c415e15 100644 --- a/code_generator/4.yaml +++ b/code_generator/4.yaml @@ -507,6 +507,40 @@ paths: '500': description: Internal server error summary: "+\n getByName" + /customers/getByEmail/: + post: + requestBody: + content: + application/json: + schema: + properties: + company_id: + type: string + email: + type: string + offset: + type: string + qty: + type: string + required: + - email + - company_id + type: object + responses: + '200': + content: + application/json: + schema: + type: object + description: Successful response + '400': + description: Bad request + '401': + description: Unauthorized + '500': + description: Internal server error + summary: "+\n getByEmail" + /customers/getByNumber/: post: requestBody: diff --git a/moloni/__version__.py b/moloni/__version__.py index abda494..1ec0501 100644 --- a/moloni/__version__.py +++ b/moloni/__version__.py @@ -1 +1 @@ -__version__ = '0.3.12' +__version__ = '0.3.14' diff --git a/moloni/api/__init__.py b/moloni/api/__init__.py index 11d87d1..34490b3 100644 --- a/moloni/api/__init__.py +++ b/moloni/api/__init__.py @@ -78,6 +78,7 @@ from .customers_client import CustomersCountModifiedSinceModel from .customers_client import CustomersDeleteModel from .customers_client import CustomersGetAllModel +from .customers_client import CustomersGetByEmailModel from .customers_client import CustomersGetByNameModel from .customers_client import CustomersGetByNumberModel from .customers_client import CustomersGetBySearchModel @@ -435,6 +436,7 @@ "CustomersCountModifiedSinceModel", "CustomersDeleteModel", "CustomersGetAllModel", + "CustomersGetByEmailModel", "CustomersGetByNameModel", "CustomersGetByNumberModel", "CustomersGetBySearchModel", diff --git a/moloni/api/customers_client.py b/moloni/api/customers_client.py index a1bfabb..b2fdc7d 100644 --- a/moloni/api/customers_client.py +++ b/moloni/api/customers_client.py @@ -400,6 +400,50 @@ def request(self) -> ApiResponse: raise ValueError("Client not initialized. Use the 'connect' method.") +class CustomersGetByEmailModel(ApiRequestModel): + company_id: Union[str, int] + email: Optional[str] = None + offset: Optional[Union[str, int]] = 0 + qty: Optional[Union[str, int]] = 25 + + def request(self) -> ApiResponse: + """ + request(self) -> ApiResponse + + Make an API request using the initialized client. + + This method checks if the `_api_client` attribute is set (i.e., if the client has been initialized via the `connect` method). + If the client is initialized, it will make an API request using the provided method name and the model's data, + excluding the `_api_client` attribute itself from the request payload. If the client is not initialized, it will raise a `ValueError`. + + Returns: + The response from the API. + + Raises: + ValueError: If the client is not initialized via the `connect` method. + + Example: + + # Assuming you have a model instance `request_model` and an API client `api_client` + + ..code-block:: python + + with request_model.connect(auth_config=auth_config) as api: + response = api.request() + + # The above example assumes that the `connect` method has been used to initialize the client. + # The request method then sends the model's data to the API and returns the API's response. + + """ + if hasattr(self, "_api_client"): + response = self._api_client.get_by_email( + self.model_dump(exclude={"_api_client"}, exclude_unset=True) + ) + return response + else: + raise ValueError("Client not initialized. Use the 'connect' method.") + + class CustomersGetByNumberModel(ApiRequestModel): company_id: Union[str, int] number: Optional[str] = None @@ -1025,6 +1069,35 @@ def get_by_name(self, data: Union[CustomersGetByNameModel, dict], **kwargs): fill_query_params(kwargs.pop("path"), self.version), data={**data, **kwargs} ) + @endpoint("//customers/getByEmail/", method="post") + def get_by_email(self, data: Union[CustomersGetByEmailModel, dict], **kwargs): + """ + get_by_email(self, data: Union[CustomersGetByEmailModel, dict], **kwargs) + + Args: + + data (Union[CustomersGetByEmailModel, dict]): A model instance or dictionary containing the following fields: + + - company_id (Union[str, int]): company_id of the CustomersGetByEmailModel. + + - email (str): email of the CustomersGetByEmailModel. + + - offset (str): offset of the CustomersGetByEmailModel. + + - qty (str): qty of the CustomersGetByEmailModel. + + + + Returns: + ApiResponse: The response from the API. + """ + + data = validate_data(data, self.validate, CustomersGetByEmailModel) + + return self._request( + fill_query_params(kwargs.pop("path"), self.version), data={**data, **kwargs} + ) + @endpoint("//customers/getByNumber/", method="post") def get_by_number(self, data: Union[CustomersGetByNumberModel, dict], **kwargs): """ diff --git a/tests/customers_client_test.py b/tests/customers_client_test.py index 2497efa..4be469a 100644 --- a/tests/customers_client_test.py +++ b/tests/customers_client_test.py @@ -12,6 +12,7 @@ CustomersDeleteModel, CustomersGetAllModel, CustomersGetByNameModel, + CustomersGetByEmailModel, CustomersGetByNumberModel, CustomersGetBySearchModel, CustomersGetByVatModel, @@ -653,6 +654,79 @@ def test_get_by_name_from_model(self, mock_request): except NoMoreRecords: pass + @patch.object(CustomersClient, "_request") + def test_get_by_email(self, mock_request): + # Mock the Response object + mock_response = Mock(spec=Response) + mock_response.json.return_value = {"some_key": "some_value"} + mock_response.status_code = 200 + mock_response.headers = {"Content-Type": "application/json"} + + # Create the ApiResponse object with the mocked Response + mock_request.return_value = ApiResponse( + response=mock_response, request_data={"qty": 10, "offset": 0} + ) + + model_data = CustomersGetByEmailModel( + company_id="sample_value", + email="email", + offset="offset", + qty="qty", + ) + response = self.client.get_by_email(data=model_data) + + # Assertions + self.assertIsInstance(response, ApiResponse) + self.assertEqual(response.payload, {"some_key": "some_value"}) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.headers["Content-Type"], "application/json") + mock_request.assert_called_once() + + # Test pagination functionality + try: + next_params = response.next(qty=5) + self.assertEqual(next_params["offset"], 10) + self.assertEqual(next_params["qty"], 5) + except NoMoreRecords: + pass + + @patch.object(CustomersClient, "_request") + def test_get_by_email_from_model(self, mock_request): + # Mock the Response object + mock_response = Mock(spec=Response) + mock_response.json.return_value = {"some_key": "some_value"} + mock_response.status_code = 200 + mock_response.headers = {"Content-Type": "application/json"} + + # Create the ApiResponse object with the mocked Response + mock_request.return_value = ApiResponse( + response=mock_response, request_data={"qty": 10, "offset": 0} + ) + + model_data = CustomersGetByEmailModel( + company_id="sample_value", + email="email", + offset="offset", + qty="qty", + ) + with model_data.connect() as api: + response = api.request() + + # Assertions + self.assertIsInstance(response, ApiResponse) + self.assertEqual(response.payload, {"some_key": "some_value"}) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.headers["Content-Type"], "application/json") + mock_request.assert_called_once() + + # Test pagination functionality + try: + next_params = response.next(qty=5) + self.assertEqual(next_params["offset"], 10) + self.assertEqual(next_params["qty"], 5) + except NoMoreRecords: + pass + @patch.object(CustomersClient, "_request") def test_get_by_number(self, mock_request): # Mock the Response object