Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion matrix_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ def login(self, login_type, **kwargs):
"type": login_type
}
for key in kwargs:
content[key] = kwargs[key]
if kwargs[key]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm confused by this. We're already iterating through all keys in kwargs. Under what circumstances would kwargs[key] be falsey?

All of this is fine, but in case you aren't aware (as I wasn't until a few months ago), content.update(kwargs) will do this on a single line with no explicit iteration required.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Now reading further on, I see why since we're not validating input in the client method. In light of that, this is fine IMO.

content[key] = kwargs[key]

return self._send("POST", "/login", content)

Expand Down
60 changes: 44 additions & 16 deletions matrix_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,10 @@ def _post_registration(self, response):
self._sync()
return self.token

# TODO: combine login methods into single method controlled by kwargs
def login_with_password_no_sync(self, username, password):
""" Login to the homeserver.
"""Deprecated. Use ``login`` with ``sync=False`` instead.

Login to the homeserver.

Args:
username (str): Account username
Expand All @@ -222,17 +223,15 @@ def login_with_password_no_sync(self, username, password):
Raises:
MatrixRequestError
"""
response = self.api.login(
"m.login.password", user=username, password=password
)
self.user_id = response["user_id"]
self.token = response["access_token"]
self.hs = response["home_server"]
self.api.token = self.token
return self.token
warn("login_with_password_no_sync is deprecated. Use login with sync=False"
"instead.",
DeprecationWarning)
return self.login(username, password, sync=False)

def login_with_password(self, username, password, limit=10):
""" Login to the homeserver.
"""Deprecated. Use ``login`` with ``sync=True`` instead.

Login to the homeserver.

Args:
username (str): Account username
Expand All @@ -246,12 +245,41 @@ def login_with_password(self, username, password, limit=10):
Raises:
MatrixRequestError
"""
token = self.login_with_password_no_sync(username, password)
warn("login_with_password is deprecated. Use login with sync=True instead.",
DeprecationWarning)
return self.login(username, password, limit, sync=True)

""" Limit Filter """
self.sync_filter = '{ "room": { "timeline" : { "limit" : %i } } }' % limit
self._sync()
return token
def login(self, username, password, limit=10, sync=True, device_id=None):
"""Login to the homeserver.

Args:
username (str): Account username
password (str): Account password
limit (int): Deprecated. How many messages to return when syncing.
This will be replaced by a filter API in a later release.
sync (bool): Optional. Whether to initiate a /sync request after logging in.
device_id (str): Optional. ID of the client device. The server will
auto-generate a device_id if this is not specified.

Returns:
str: Access token

Raises:
MatrixRequestError
"""
response = self.api.login(
"m.login.password", user=username, password=password, device_id=device_id
)
self.user_id = response["user_id"]
self.token = response["access_token"]
self.hs = response["home_server"]
self.api.token = self.token

if sync:
""" Limit Filter """
self.sync_filter = '{ "room": { "timeline" : { "limit" : %i } } }' % limit
self._sync()
return self.token

def logout(self):
""" Logout from the homeserver.
Expand Down