Closed artmoskvin closed 3 months ago
To add a new endpoint for exponentiation in the project, here's a plan that outlines the steps and changes needed:
Update maths.py
in the my_tiny_service/api/routers
directory:
MathsIn
class for input and MathsResult
for output.@router.post(
"/exponentiation",
summary="Calculate the exponentiation of a base number to a power",
response_model=MathsResult,
)
def exponentiation(maths_input: MathsIn) -> MathsResult:
"""Calculates the exponentiation of a base number to a power."""
result = maths_input.a ** maths_input.b
return MathsResult(result=result)
Update the Pydantic models if necessary:
MathsIn
and MathsResult
classes in maths.py
meet the requirements for the exponentiation endpoint (which they likely do, as they are designed for mathematical operations), no changes are needed here.Update tests in test_api.py
in the tests
directory:
def test_exponentiation(client: starlette.testclient.TestClient) -> None:
"""Test that the exponentiation endpoint correctly calculates the power of a number."""
response = client.post("/exponentiation", json={"a": 2, "b": 3})
assert response.status_code == 200
assert response.json() == {"result": 8}
Ensure all changes are properly documented:
This plan focuses on adding the new endpoint in a way that is consistent with the existing project structure and coding conventions. The changes are localized to the maths.py
file for the endpoint implementation and the test_api.py
file for testing, minimizing the impact on the rest of the project.
@acedev-dev-ai could you double-check the MathsIn
schema to verify the fields?
After reviewing the MathsIn
schema, it appears that the fields are named number1
and number2
, not a
and b
as initially mentioned. Here's the updated plan with the correct field names:
Update maths.py
in the my_tiny_service/api/routers
directory:
MathsIn
class for input and MathsResult
for output, adjusting for the correct field names.@router.post(
"/exponentiation",
summary="Calculate the exponentiation of a base number to a power",
response_model=MathsResult,
)
def exponentiation(maths_input: MathsIn) -> MathsResult:
"""Calculates the exponentiation of a base number to a power."""
result = maths_input.number1 ** maths_input.number2
return MathsResult(result=result)
No changes needed for Pydantic models:
MathsIn
and MathsResult
classes are suitable for the exponentiation endpoint.Update tests in test_api.py
in the tests
directory:
def test_exponentiation(client: starlette.testclient.TestClient) -> None:
"""Test that the exponentiation endpoint correctly calculates the power of a number."""
response = client.post("/exponentiation", json={"number1": 2, "number2": 3})
assert response.status_code == 200
assert response.json() == {"result": 8}
Ensure all changes are properly documented:
This updated plan corrects the field names in the MathsIn
schema, ensuring the implementation aligns with the existing project structure.
@acedev-dev-ai looks good, proceed with implementation
The implementation for adding a new endpoint for exponentiation has been completed, and a pull request has been created. You can review and merge the changes via the following link: Pull Request #70.
The endpoint should take 2 numbers A and B and return A^B.