Acebots-AI / my-tiny-service

0 stars 1 forks source link

Add new endpoint for exponentiation #60

Closed artmoskvin closed 3 months ago

artmoskvin commented 3 months ago

The endpoint should take 2 numbers A and B and return A^B.

acebots-ai-local[bot] commented 3 months ago

To add a new endpoint for exponentiation in the project, we will follow these steps:

  1. Update my_tiny_service/api/routers/maths.py:

    • Add a new endpoint function for exponentiation.
    • Define the endpoint with @router.post decorator, similar to the existing mathematical operations.
    • Use the existing MathsIn input model for the two numbers A and B.
    • Create a new function exponentiation(maths_input: MathsIn) -> MathsResult that calculates A^B.
    • Update the responses dictionary if necessary, and ensure proper error handling.
  2. Update tests/test_api.py:

    • Add a new test function test_exponentiation to ensure the new endpoint correctly calculates the exponentiation of two numbers.
    • This test should cover various cases, including positive, negative, and zero exponents.
  3. Ensure proper error handling:

    • In the exponentiation function, handle cases like zero raised to a negative exponent, which should return an error response.
  4. Documentation and Comments:

    • Add comments and documentation for the new endpoint in maths.py.
    • Ensure the OpenAPI documentation is updated with the new endpoint by checking the FastAPI automatic documentation generation.

Code Snippets for Implementation:

In my_tiny_service/api/routers/maths.py:

@router.post(
    "/exponentiation",
    summary="Calculate the exponentiation of two numbers",
    response_model=MathsResult,
)
def exponentiation(maths_input: MathsIn) -> MathsResult:
    """Calculates the exponentiation of two whole numbers."""
    result = maths_input.number1 ** maths_input.number2
    return MathsResult(result=result)

In tests/test_api.py:

def test_exponentiation(client: starlette.testclient.TestClient) -> None:
    """Test that the exponentiation endpoint correctly calculates A^B."""
    response = client.post("/maths/exponentiation", json={"number1": 2, "number2": 3})
    assert response.status_code == 200
    assert response.json() == {"result": 8}

This plan outlines the necessary steps and code changes to implement the new exponentiation endpoint. If there are any questions or additional requirements, please let me know.

artmoskvin commented 3 months ago

@acedev-dev-ai excellent! proceed with implementation