Skip to content

Commit

Permalink
add customers get by email
Browse files Browse the repository at this point in the history
  • Loading branch information
saleweaver committed Nov 4, 2024
1 parent d683492 commit f1055fc
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 1 deletion.
34 changes: 34 additions & 0 deletions code_generator/4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion moloni/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.3.12'
__version__ = '0.3.14'
2 changes: 2 additions & 0 deletions moloni/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -435,6 +436,7 @@
"CustomersCountModifiedSinceModel",
"CustomersDeleteModel",
"CustomersGetAllModel",
"CustomersGetByEmailModel",
"CustomersGetByNameModel",
"CustomersGetByNumberModel",
"CustomersGetBySearchModel",
Expand Down
73 changes: 73 additions & 0 deletions moloni/api/customers_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("/<version>/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("/<version>/customers/getByNumber/", method="post")
def get_by_number(self, data: Union[CustomersGetByNumberModel, dict], **kwargs):
"""
Expand Down
74 changes: 74 additions & 0 deletions tests/customers_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
CustomersDeleteModel,
CustomersGetAllModel,
CustomersGetByNameModel,
CustomersGetByEmailModel,
CustomersGetByNumberModel,
CustomersGetBySearchModel,
CustomersGetByVatModel,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f1055fc

Please sign in to comment.