diff --git a/matrix_client/api.py b/matrix_client/api.py index 264861c2..a6634d17 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -152,7 +152,8 @@ def login(self, login_type, **kwargs): "type": login_type } for key in kwargs: - content[key] = kwargs[key] + if kwargs[key]: + content[key] = kwargs[key] return self._send("POST", "/login", content) diff --git a/matrix_client/client.py b/matrix_client/client.py index be1abe74..d9a59d52 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -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 @@ -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 @@ -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.