ubidots / ubidots-python

A python API client for Ubidots
ubidots.com/docs/libraries/python.html
MIT License
30 stars 13 forks source link

Variable.get_values(1) always requests all values from the API #20

Closed filmkorn closed 1 year ago

filmkorn commented 3 years ago

Hello,

I've been testing ubidots for a small project and ran into the following issue:

When I just need the latest value of a Variable, the ServerBridge always gets all values of the Variable. For variables with increasing number of 'dots', this increases the bandwidth needed considerably.

If I read correctly, ServerBridge.get() should pass params={"page_size: <number of values>"} in here.

filmkorn commented 3 years ago

As a workaround I changed a few lines in these inherited Variable and ApiClient classes:

class MyVariable(Variable):
    """A variable that performs an optimized request."""

    def get_values(self, numofvals="ALL"):
        endpoint = 'variables/' + self.id + '/values'
        kwargs = {}
        if isinstance(numofvals, int):
            kwargs["params"] = {"page_size": str(numofvals)}
        response = self.bridge.get(endpoint, **kwargs).json()
        pag = Paginator(self.bridge, response, self.get_transform_function(), endpoint)
        return InfoList(pag, numofvals)

class MyApiClient(ApiClient):
    """ApiClient that uses MyVariable."""

    def get_variable(self, var_id, **kwargs):
        raw_variable = self.bridge.get('variables/' + str(var_id), **kwargs).json()
        return MyVariable(raw_variable, self.bridge)

In my test this appears to reduce the result payload to requested numofvals.

filmkorn commented 1 year ago

Closed in #21