rcpch / rcpchgrowth-python

A python package to produce calculations for all growth issues
GNU Affero General Public License v3.0
5 stars 4 forks source link

date/time stamps and reference requested in response #37

Open eatyourpeas opened 4 days ago

eatyourpeas commented 4 days ago

Now that multiple references are offered, there is currently no information in the response object either that the request was made or the reference that the return data corresponds to. I would be ideal to put in the body of the response with all the results but this would represent a breaking change for users, so the other option is to put it in the response header. Users can discard this, but also they can use this if they need.

FastAPI has this feature to add custom headers:

from fastapi import FastAPI, HTTPException, Response
from typing import List
from datetime import datetime
import constants

app = FastAPI()

@uk_who.post(
    "/fictional-child-data", tags=["uk-who"], response_model=List[MeasurementObject]
)
def fictional_child_data(fictional_child_request: FictionalChildRequest, response: Response):
    """
    ## UK-WHO Fictional Child Data Endpoint

    * Generates synthetic data for demonstration or testing purposes
    """
    try:
        life_course_fictional_child_data = generate_fictional_child_data(
            measurement_method=fictional_child_request.measurement_method,
            sex=fictional_child_request.sex,
            start_chronological_age=fictional_child_request.start_chronological_age,
            end_age=fictional_child_request.end_age,
            gestation_weeks=fictional_child_request.gestation_weeks,
            gestation_days=fictional_child_request.gestation_days,
            measurement_interval_type=fictional_child_request.measurement_interval_type,
            measurement_interval_number=fictional_child_request.measurement_interval_number,
            start_sds=fictional_child_request.start_sds,
            drift=fictional_child_request.drift,
            drift_range=fictional_child_request.drift_range,
            noise=fictional_child_request.noise,
            noise_range=fictional_child_request.noise_range,
            reference=constants.UK_WHO,
        )

        # Set response headers
        response.headers["X-Date-Time"] = datetime.utcnow().isoformat()
        response.headers["X-Reference"] = constants.UK_WHO

        return life_course_fictional_child_data
    except Exception as e:
        raise HTTPException(
            status_code=422,
            detail=f"Not possible to create UK-WHO fictional child data. Error: {str(e)}",
        )
mbarton commented 4 days ago

I would be ideal to put in the body of the response with all the results but this would represent a breaking change for users

Could we add new top level fields with this so it's not a breaking change?

eatyourpeas commented 5 hours ago

We have a typescript interface that matches the API specification of the Measurement class. If we add a new top level object might that not break it for people using older versions of the chart component?