Skip to content

Expose rate limit headers into instance variables#202

Merged
smaeda-ks merged 10 commits intoxdevplatform:masterfrom
smaeda-ks:smaeda-ks/develop/20190717
Aug 2, 2019
Merged

Expose rate limit headers into instance variables#202
smaeda-ks merged 10 commits intoxdevplatform:masterfrom
smaeda-ks:smaeda-ks/develop/20190717

Conversation

@smaeda-ks
Copy link
Copy Markdown
Contributor

@smaeda-ks smaeda-ks commented Jul 18, 2019

re: APE-537

Adding a few lines in def __from_response() (cursor.py) and def from_response() (resource.py) to set rate limit instance varialbes.

With this change, a caller who invokes Cursor class will be able to access rate limit headers through its instance variables. A caller who invokes from_response() from Resource class will be able to access rate limit headers by passing an additional argument (optional).

@smaeda-ks smaeda-ks requested a review from tushdante July 18, 2019 08:09
@smaeda-ks smaeda-ks self-assigned this Jul 18, 2019
Comment thread twitter_ads/resource.py
return self._account

def from_response(self, response):
def from_response(self, response, headers=None):
Copy link
Copy Markdown
Contributor Author

@smaeda-ks smaeda-ks Jul 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a new argument headers where caller can pass response.headers as an optional argument.

Currently, caller passes only response body:

return self.from_response(response.body['data'])

so adding response headers in order to process rate limit headers:

return self.from_response(response.body['data'], response.headers)

see tests/test_rate_limit.py for this point.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just reminder to follow up about debugging level headers and exposing those as well (X- headers)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, we can generalize the extract_rate_limit() method in the future to handle more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to John's point. I believe there are plans to include rate limit headers to the async stats create endpoint. Given the current setup, this should be fairly trivial to add. Good stuff!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I just changed the func name to extract_response_headers() so this can be consistent for future use such as async one @tushdante mentioned.

Comment thread twitter_ads/http.py
self._raw_body = kwargs.get('raw_body', None)

if headers.get('content-type') == 'application/gzip':
# hack because Twitter TON API doesn't return headers as it should
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is not really correct I'm correcting this. re: PAEX-2015

Comment thread twitter_ads/http.py
if 'x-rate-limit-reset' in headers:
self._rate_limit = int(headers['x-rate-limit-limit'])
self._rate_limit_remaining = int(headers['x-rate-limit-remaining'])
self._rate_limit_reset = datetime.fromtimestamp(int(headers['x-rate-limit-reset']))
Copy link
Copy Markdown
Contributor Author

@smaeda-ks smaeda-ks Jul 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with not converting raw header values to a specific format like this. We can just leave it as is so that users can process as they want to. Plus, this doesn't necessarily be a breaking change as most of the users weren't able to access these values anyway unless they do a manual request approach.

remove duplicate code
@smaeda-ks
Copy link
Copy Markdown
Contributor Author

@tushdante It would be great to get your insights too if possible before I merge this, thanks!

@tushdante
Copy link
Copy Markdown
Contributor

@tushdante It would be great to get your insights too if possible before I merge this, thanks!

Yep! I'll take a look at this today!

@smaeda-ks
Copy link
Copy Markdown
Contributor Author

ping

Copy link
Copy Markdown
Contributor

@tushdante tushdante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, LGTM! Just wanted to get your thoughts on the case where headers aren't returned.

Comment thread twitter_ads/resource.py
return self._account

def from_response(self, response):
def from_response(self, response, headers=None):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to John's point. I believe there are plans to include rate limit headers to the async stats create endpoint. Given the current setup, this should be fairly trivial to add. Good stuff!

Comment thread twitter_ads/resource.py
This helper handles all necessary type coercions as it assigns
attribute values.
"""
if headers is not None:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens for the case where headers is None and the calling function i.e., return self.from_response(response.body['data'], response.headers) has the response.headers set? Ideally this should have something like cursor.account_rate_limit set to None as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tushdante Thanks! That's a good point... I removed explicit "if" conditions from the extract_response_headers() and changed to use dict.get() method so they get either actual value from response or None in case if it's not present in response, and always accessible through instance variable.

@smaeda-ks smaeda-ks merged commit b24465f into xdevplatform:master Aug 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants