python-opencage-geocoder icon indicating copy to clipboard operation
python-opencage-geocoder copied to clipboard

Added ratelimit headers as class properties

Open riquedev opened this issue 5 months ago • 1 comments

I added three new properties to the OpenCageGeocode class:

  • ratelimit_limit
  • ratelimit_remaining
  • ratelimit_reset

These properties are initialized as None and are populated based on the API response headers. To prevent the code from breaking if the headers are missing or renamed, I implemented the following logic:

headers_keys = {
    'ratelimit_limit': {
        'header': 'x-ratelimit-limit',
        'conversor': lambda v: int(v) if v else self.ratelimit_limit
    },
    'ratelimit_remaining': {
        'header': 'x-ratelimit-remaining',
        'conversor': lambda v: int(v) if v else self.ratelimit_remaining
    },
    'ratelimit_reset': {
        'header': 'x-ratelimit-reset',
        'conversor': lambda v: datetime.fromtimestamp(int(v) / 1e3) if v else self.ratelimit_reset
    }
}

for prop_name, prop_data in headers_keys.items():
    header_value = response.headers.get(prop_data['header'])
    conversor = prop_data['conversor']
    setattr(self, prop_name, conversor(header_value))

Motivation:

I needed to capture this information in another project where I use this package. Since I’m currently not on the paid plan, the rate limit data is not included in the response. However, I thought it would be useful to have these properties available for potential future use if this data becomes available.

Tests:

I added simple tests for these new properties in the test/test_ratelimit_properties.py file to ensure that the changes work as expected.

riquedev avatar Sep 14 '24 13:09 riquedev