Closed artmoskvin closed 4 months ago
To add a new endpoint for exponentiation in the project, here's a plan for implementation:
Create a New Branch: Before making any changes, it's a good practice to create a new branch. This ensures that the main branch remains stable and any new features can be reviewed before being merged.
Update the Maths Router: The file my_tiny_service/api/routers/maths.py
contains the definitions for mathematical operations. We'll add a new endpoint for exponentiation here. This involves:
@router.post
decorator.responses
dictionary if necessary.Implement Exponentiation Logic: Inside the new function, use Python's **
operator to calculate the exponentiation. Ensure that the input and output are correctly formatted according to the existing structure, using MathsIn
for input and MathsResult
for output.
Update Tests: Add new tests in tests/test_api.py
to cover the exponentiation endpoint. This should include tests for positive numbers, negative numbers, and edge cases like raising to the power of 0.
Create a Pull Request: Once the changes are tested locally, push the new branch and create a pull request for review.
Here's a snippet for the new endpoint in my_tiny_service/api/routers/maths.py
:
@router.post(
"/exponentiation",
summary="Calculate the exponentiation of a number",
response_model=MathsResult,
)
def exponentiation(maths_input: MathsIn) -> MathsResult:
"""Calculates the exponentiation of a base number raised to a power."""
result = maths_input.base ** maths_input.exponent
return MathsResult(result=result)
And a test case snippet in tests/test_api.py
:
def test_exponentiation(client: starlette.testclient.TestClient) -> None:
"""Test that the exponentiation endpoint correctly calculates the power."""
response = client.post("/exponentiation", json={"base": 2, "exponent": 3})
assert response.status_code == 200
assert response.json() == {"result": 8}
This plan outlines the steps and code snippets needed to implement the new exponentiation endpoint.
@acedev-dev-ai looks perfect, please proceed
The new endpoint for exponentiation has been implemented, and a pull request has been created. You can review and merge the changes via the following link: Pull Request #51. This pull request includes the implementation of the endpoint in my_tiny_service/api/routers/maths.py
and the corresponding tests in tests/test_api.py
.
The endpoint should take 2 numbers A and B and return A^B.