Acebots-AI / my-tiny-service

0 stars 1 forks source link

Add new endpoint for exponentiation #50

Closed artmoskvin closed 4 months ago

artmoskvin commented 4 months ago

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

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

To add a new endpoint for exponentiation in the project, here's a plan for implementation:

  1. 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.

  2. 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:

    • Defining a new route with @router.post decorator.
    • Creating a function that takes two numbers (base and exponent) and returns the result of the exponentiation.
    • Adding the new route to the responses dictionary if necessary.
  3. 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.

  4. 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.

  5. 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.

artmoskvin commented 4 months ago

@acedev-dev-ai looks perfect, please proceed

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

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.