This repository was archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Allow to hook load and save functions to use external session storage… #36
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -175,6 +175,9 @@ def __init__(self, | |
|
|
||
| self.download_queue = Queue() | ||
|
|
||
| self.load_session_hook = None | ||
| self.save_session_hook = None | ||
|
|
||
| def start(self): | ||
| """Use this method to start the Client after creating it. | ||
| Requires no parameters. | ||
|
|
@@ -183,16 +186,18 @@ def start(self): | |
| :class:`pyrogram.Error` | ||
| """ | ||
| self.load_config() | ||
| self.load_session(self.session_name) | ||
|
|
||
| self.session = Session( | ||
| self.dc_id, | ||
| self.test_mode, | ||
| self.proxy, | ||
| self.auth_key, | ||
| self.api_key.api_id, | ||
| client=self | ||
| ) | ||
| if callable(self.load_session_hook): | ||
| self.load_session_hook(self) | ||
| else: | ||
| self.load_session(self.session_name) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if you indented it wrongly, but this piece of code is always needed: self.session = Session(...)Here is where Pyrogram creates a new connection to the server. |
||
| self.session = Session( | ||
| self.dc_id, | ||
| self.test_mode, | ||
| self.proxy, | ||
| self.auth_key, | ||
| self.api_key.api_id, | ||
| client=self | ||
| ) | ||
|
|
||
| self.session.start() | ||
|
|
||
|
|
@@ -202,7 +207,10 @@ def start(self): | |
| else: | ||
| self.authorize_bot() | ||
|
|
||
| self.save_session() | ||
| if callable(self.save_session_hook): | ||
| self.save_session_hook(self) | ||
| else: | ||
| self.save_session() | ||
|
|
||
| if self.token is None: | ||
| self.get_dialogs() | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw your Pyroredis repo, and there is no need to store the proxy settings in a session.
A session should not depend on these information, users should be free to change their proxies regardless of the session used. The same applies for the api key (although I didn't see you using it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proxy settings is not stored into the redis session. Please re-check the code again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought you were only dealing with sessions, and can't understand why you need Proxy (and ApiKey) to be available there. There is a different method that takes care of loading proxy and api key settings (load_config) and has nothing to do with load_session.
Maybe the auth key creation process inside load_session was confusing you a bit, if that's so we should find a nice way to move it from there. Creating a Session (and auth keys) should only be Client's job, and loading a session must happen before that.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I'm thinking is returning False from load_session in case a session does not exist and the Client needs to create an auth key, this way we can move
self.auth_key = Auth(...)outside.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
During the modification I had several objectives:
I think will be better if those two actions (session construction and session start) will be in separate methods, like:
(or even move self.load_session() under create_session()), so it is possible to do something like: