# Best practice to retrieve measurements

**URL:** <https://community.cumulocity.com/t/best-practice-to-retrieve-measurements/7609>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [September 20, 2023, 10:33am UTC](https://community.cumulocity.com/t/best-practice-to-retrieve-measurements/7609 "2023-09-20T10:33:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohan\_Pathapataa](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/mohan_pathapataa/32/11730_2.png) [@Mohan\_Pathapataa](https://community.cumulocity.com/u/Mohan_Pathapataa)\
**Post date:** [September 20, 2023, 10:33am UTC](https://community.cumulocity.com/t/best-practice-to-retrieve-measurements/7609/1 "2023-09-20T10:33:13Z")

</div>

#### _ **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:**

```auto
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:

```auto
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 ?

---

<div class="post-metadata">

**Author:** ![Tristan\_Bastian](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/tristan_bastian/32/18259_2.png) [@Tristan\_Bastian](https://community.cumulocity.com/u/Tristan_Bastian)\
**Post date:** [September 20, 2023, 10:55am UTC](https://community.cumulocity.com/t/best-practice-to-retrieve-measurements/7609/2 "2023-09-20T10:55:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mohan\_Pathapataa](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/mohan_pathapataa/32/11730_2.png) [@Mohan\_Pathapataa](https://community.cumulocity.com/u/Mohan_Pathapataa)\
**Post date:** [September 20, 2023, 12:58pm UTC](https://community.cumulocity.com/t/best-practice-to-retrieve-measurements/7609/3 "2023-09-20T12:58:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tristan\_Bastian](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/tristan_bastian/32/18259_2.png) [@Tristan\_Bastian](https://community.cumulocity.com/u/Tristan_Bastian)\
**Post date:** [September 20, 2023, 3:11pm UTC](https://community.cumulocity.com/t/best-practice-to-retrieve-measurements/7609/4 "2023-09-20T15:11:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mohan\_Pathapataa](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/mohan_pathapataa/32/11730_2.png) [@Mohan\_Pathapataa](https://community.cumulocity.com/u/Mohan_Pathapataa)\
**Post date:** [September 22, 2023, 12:49pm UTC](https://community.cumulocity.com/t/best-practice-to-retrieve-measurements/7609/5 "2023-09-22T12:49:43Z")

</div>

Thanks for the update

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [March 20, 2024, 12:49pm UTC](https://community.cumulocity.com/t/best-practice-to-retrieve-measurements/7609/6 "2024-03-20T12:49:44Z")

</div>

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