Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor kwargs handling in Bot methods #1924

Merged
merged 9 commits into from
Jun 30, 2020
Merged

Refactor kwargs handling in Bot methods #1924

merged 9 commits into from
Jun 30, 2020

Conversation

Bibo-Joshi
Copy link
Member

@Bibo-Joshi Bibo-Joshi commented Apr 27, 2020

Closes #1918

  • Passes kwargs specified as api_kwargs to API
  • Adds a warning about api_kwargs not being guaranteed to work. I put this on top of Bot rather than on every function as the latter seemed a waste of space to me.
  • reduces code duplication:
    • base_url is added in Bot._post instead of every function
    • data.update(api_kwargs) called in Bot._post instead of every function
  • minor doc fix for set_sticker_set_thumb on the fly

Note: I removed a part of Bot.set_webhook that was intended for backwards compatibility, as it used **kwargs. However, git blame tells me that it's 4 years old and from API 2.3.1 (?). So I thinks it's fair to say that it's not needed anymore.

Copy link
Member

@Poolitzer Poolitzer left a comment

Choose a reason for hiding this comment

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

LGTM.

I dont like that we support this, but if the majority thinks this is good, I am okay with it

@n5y
Copy link
Contributor

n5y commented May 2, 2020

It's perfectly possible to pass parameters with GET methods, it's one of 4 supported ways to pass parameters https://core.telegram.org/bots/api#making-requests . Alternatively, there's no need for those methods to use GET instead of POST.

Having a separate api_kwargs or similar is better than having **kwargs with warning.

  1. An IDE can warn the user about misspelled kwargs before the code is run. Testing that code runs without raising an exception is more straightforward than testing that code runs without logging warnings.
  2. A user that does actually want to pass additional arguments to Bot API is not forced to deal with the warning.

@n5y
Copy link
Contributor

n5y commented May 2, 2020

The url = '{0}/{endpointName}'.format(self.base_url) logic can probably be moved into _post/_get methods. This would also make it easier to call not yet supported api methods. And since there's no need to have both _get and _post they could be replaced with a single Bot._api_call(endpoint, data, timeout) or something like that.

@n5y
Copy link
Contributor

n5y commented May 2, 2020

It seems like there's already bot api specific code in Request.post (e.g. elif key == 'media':) so perhaps instead of spreading the logic across a whole new level of Bot._post / Bot._api_call it should be moved into Request.post. Since Request is already specialized for bot api it would make sense for it to have base_url attribute. Or maybe api_call can be made a Request method and bot api specific logic moved into it out of both Request.post and Bot._post.

The recent bug with reply_markup.to_json vs reply_markup.to_dict when uploading a file could be avoided if Request.post handled the conversion of TelegramObject(or perhaps just ReplyMarkup) like it already does for InputFile.

@Bibo-Joshi
Copy link
Member Author

Bibo-Joshi commented May 2, 2020

It seems like there's already bot api specific code in Request.post (e.g. elif key == 'media':) so perhaps instead of spreading the logic across a whole new level of Bot._post / Bot._api_call it should be moved into Request.post. Since Request is already specialized for bot api it would make sense for it to have base_url attribute. Or maybe api_call can be made a Request method and bot api specific logic moved into it out of both Request.post and Bot._post.

The recent bug with reply_markup.to_json vs reply_markup.to_dict when uploading a file could be avoided if Request.post handled the conversion of TelegramObject(or perhaps just ReplyMarkup) like it already does for InputFile.

As I understood from the discussion with Noam about that recent bug, Requests only task should be the network stuff and mingling the "responsibilities" of Bot and Request is not advisable. The InputFile related stuff in Request is just there to handle uploading files, which is network stuff.

Unifying the construction of the URL, seems a good idea to me, too. Putting it in Requests not, for above reasoning.

@n5y
Copy link
Contributor

n5y commented May 2, 2020

Where does network stuff end and bot stuff begin?
If Request can call .to_json() on value of Bot API specific "media" key it can probably do that for all TelegramObject values. It's about properly passing nested objects as multipart/form-data and there's already an if branch about properly passing ints and floats as multipart/form-data.

In context of dealing with Bot API, setting base url is about as close to network stuff as setting a proxy is. Request is not some abstract network wrapper at this point, it's made specifically for requests to Bot API, trying to generalize it might needlessly increase code complexity.

Copy link
Member

@Eldinnie Eldinnie left a comment

Choose a reason for hiding this comment

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

I;m starting to lean towards @n5y 's argument. Not specifically we need to protect users from early mistakes, but that additional kwarg adding should be explicit. That way we can reduce the warnings.

The option to add api_kwargs to every bot method which will be added to the data_dict silently. and removing **kwargs seems good to me.

@Bibo-Joshi Bibo-Joshi changed the title Unify kwargs handling in Bot methods Refactor kwargs handling in Bot methods May 11, 2020
@Bibo-Joshi
Copy link
Member Author

Bibo-Joshi commented May 13, 2020

Just noticed that the doc strings of the file types get_file should also be updated. Will do soonish.

Edit: Done

# Conflicts:
#	telegram/bot.py
#	telegram/utils/request.py
Copy link
Member

@Poolitzer Poolitzer left a comment

Choose a reason for hiding this comment

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

Great change! I love this solution and I thank you for putting this much effort into it. It looks way better now, great stuff.

@Bibo-Joshi Bibo-Joshi changed the base branch from master to v13 June 30, 2020 19:43
@Bibo-Joshi Bibo-Joshi merged commit 20ee6e1 into v13 Jun 30, 2020
@Bibo-Joshi Bibo-Joshi deleted the fix-1918 branch June 30, 2020 20:07
Bibo-Joshi added a commit that referenced this pull request Jul 10, 2020
* Unify kwargs handling in Bot methods

* Remove Request.get, make api_kwargs an explicit argument, move note to head of Bot class

* Fix test_official

* Update get_file methods
Bibo-Joshi added a commit that referenced this pull request Jul 14, 2020
* Unify kwargs handling in Bot methods

* Remove Request.get, make api_kwargs an explicit argument, move note to head of Bot class

* Fix test_official

* Update get_file methods
Bibo-Joshi added a commit that referenced this pull request Jul 19, 2020
* Unify kwargs handling in Bot methods

* Remove Request.get, make api_kwargs an explicit argument, move note to head of Bot class

* Fix test_official

* Update get_file methods
Bibo-Joshi added a commit that referenced this pull request Jul 19, 2020
* Refactor handling of `default_quote`

* Make it a breaking change

* Pickle a bots defaults

* Temporarily enable tests for the v13 branch

* Temporarily enable tests for the v13 branch

* Refactor handling of kwargs in Bot methods (#1924)

* Unify kwargs handling in Bot methods

* Remove Request.get, make api_kwargs an explicit argument, move note to head of Bot class

* Fix test_official

* Update get_file methods

* Refactor JobQueue (#1981)

* First go on refactoring JobQueue

* Temporarily enable tests for the v13 branch

* Work on tests

* Temporarily enable tests for the v13 branch

* Increase coverage

* Remove JobQueue.tick()

# Was intended for interal use anyways
# Fixes tests

* Address review

* Temporarily enable tests for the v13 branch

* Address review

* Dispatch errors

* Fix handling of job_kwargs

* Remove possibility to pass a Bot to JobQueue

* Refactor persistence of Bot instances (#1994)

* Refactor persistence of bots

* User BP.set_bot in Dispatcher

* Temporarily enable tests for the v13 branch

* Add documentation

* Add warning to Updater for passing both defaults and bot

* Address review

* Fix test
Bibo-Joshi added a commit that referenced this pull request Aug 13, 2020
* Unify kwargs handling in Bot methods

* Remove Request.get, make api_kwargs an explicit argument, move note to head of Bot class

* Fix test_official

* Update get_file methods
Bibo-Joshi added a commit that referenced this pull request Aug 13, 2020
* Refactor handling of `default_quote`

* Make it a breaking change

* Pickle a bots defaults

* Temporarily enable tests for the v13 branch

* Temporarily enable tests for the v13 branch

* Refactor handling of kwargs in Bot methods (#1924)

* Unify kwargs handling in Bot methods

* Remove Request.get, make api_kwargs an explicit argument, move note to head of Bot class

* Fix test_official

* Update get_file methods

* Refactor JobQueue (#1981)

* First go on refactoring JobQueue

* Temporarily enable tests for the v13 branch

* Work on tests

* Temporarily enable tests for the v13 branch

* Increase coverage

* Remove JobQueue.tick()

# Was intended for interal use anyways
# Fixes tests

* Address review

* Temporarily enable tests for the v13 branch

* Address review

* Dispatch errors

* Fix handling of job_kwargs

* Remove possibility to pass a Bot to JobQueue

* Refactor persistence of Bot instances (#1994)

* Refactor persistence of bots

* User BP.set_bot in Dispatcher

* Temporarily enable tests for the v13 branch

* Add documentation

* Add warning to Updater for passing both defaults and bot

* Address review

* Fix test
Bibo-Joshi added a commit that referenced this pull request Aug 16, 2020
* Unify kwargs handling in Bot methods

* Remove Request.get, make api_kwargs an explicit argument, move note to head of Bot class

* Fix test_official

* Update get_file methods
Bibo-Joshi added a commit that referenced this pull request Aug 16, 2020
* Refactor handling of `default_quote`

* Make it a breaking change

* Pickle a bots defaults

* Temporarily enable tests for the v13 branch

* Temporarily enable tests for the v13 branch

* Refactor handling of kwargs in Bot methods (#1924)

* Unify kwargs handling in Bot methods

* Remove Request.get, make api_kwargs an explicit argument, move note to head of Bot class

* Fix test_official

* Update get_file methods

* Refactor JobQueue (#1981)

* First go on refactoring JobQueue

* Temporarily enable tests for the v13 branch

* Work on tests

* Temporarily enable tests for the v13 branch

* Increase coverage

* Remove JobQueue.tick()

# Was intended for interal use anyways
# Fixes tests

* Address review

* Temporarily enable tests for the v13 branch

* Address review

* Dispatch errors

* Fix handling of job_kwargs

* Remove possibility to pass a Bot to JobQueue

* Refactor persistence of Bot instances (#1994)

* Refactor persistence of bots

* User BP.set_bot in Dispatcher

* Temporarily enable tests for the v13 branch

* Add documentation

* Add warning to Updater for passing both defaults and bot

* Address review

* Fix test
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Behaviour of kwargs in bot methods
4 participants