Best practice to retrieve measurements

Product/components used and version/fix level:

Cumulocity Prod - 1016.0.324

Detailed explanation of the problem:

I am using a custom application with c8y version 10.16. I would like to understand the differences in the way measurements are called.

First approach by using measurements service:

constructor(private measurements: MeasurementService) { }
getLastManualInpDate(
        boilerId: any
    ): Promise<IResultList<IMeasurement>> {
        const filters: object = {
            withTotalElements: true,
            source: boilerId,
            withTotalPages: true,
            dateTo: new Date().toISOString(),
            valueFragmentType: 'user_manualMeasurement',
            revert: true
        };
        return this.measurements.list(filters);
    }

Second Approach using fetch client:

async waterAnalysis_ManualInput(boilerId: any, valueFragmentSeries: string) {
        const dateTo = new Date();
        let payload = {
            withTotalElements: true,
            source: boilerId,
            withTotalPages: true,
            dateTo: new Date().toISOString(),
            valueFragmentType: 'user_manualMeasurement',
            revert: true
        };
        try {
            const res = await this.fetchClient.fetch(`/measurement/measurements`,
                {
                    method: 'GET',
                    params: payload,
                    headers: {
                        'Content-Type': 'application/json'
                    },
                }
            );
            const resp = await res.json();
            return resp;
        } catch (err) {

        }
    }

Both ways allow me to retrieve the data and view the API requests in the network tab. What is the best approach between these two methods ?

Hi Mohan,

I would say that this always comes to your personal preferences.
Using the fetchClient you are potentially more flexible but you also have to take care of more things, that are otherwise handled by the abstraction done in the API specific services.

E.g. in your second approach using the FetchClient you are missing to verify the statuscode of the response. These are things you would have to take care of on your own when using the FetchClient directly.

In your example I do not see any benefit in using the FetchClient and I would stick with the MeasurementService. This also gives you responses that are typed. And why would you want to do something in a more complicated way, when there is also a simple one with no drawbacks?

Regards,
Tristan

So if I use service I don’t want catch the errors ? If I use fetch client I should catch the error’s explicitly ?

Regards
Mohan

If you use measurements.list this will throw an exception when the statuscode of the response did not match the expected value.
You might still want to catch this error but you do not have to throw an error.

If you use fetchClient.fetch you are responsible for checking status codes and you can decide on your own how you want to deal with them. fetchClient.fetch would e.g. not throw an error if the statuscode of the response is 503.

Thanks for the update

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.