Skip to content

Conversation

@Cadair
Copy link
Contributor

@Cadair Cadair commented Sep 8, 2017

This now adds **query_params to most methods to allow specification of appservice parameters like ts and user_id.

It also does a bunch of documentation updates and moves from sphinxcontrib.napoleon to sphinx.ext.napoleon.

This introduces a breaking change of timestamp= to ts=.

Signed-off-by: Stuart Mumford stuart@cadair.com

@matrixbot
Copy link
Member

Can one of the admins verify this patch?

@non-Jedi
Copy link
Collaborator

non-Jedi commented Sep 9, 2017

Thanks @Cadair. A couple notes:

  • Please update the docstrings of any methods whose arguments you change to
    reflect the new arguments even if those arguments are strictly optional.
  • The more I think about this, the less I like having user_id as a
    separate kwarg on each method. We should just put a query_params kwarg
    on each method instead. This would also get rid of all the timestamp
    kwargs littered everywhere. This gives at least 2 benefits:
    1. The method signatures more closely match the api (information going in
      the headers is separate from information going in query_params is
      separate from the json payload... mostly).
    2. Any future (optional) extensions to the CS api involving query_params
      are already accounted for.
  • We should include a note in each docstring listing possible contents of
    the query_params kwarg (e.g. "ts" and "user_id").

@Cadair Cadair changed the title Add an explicit user_id kwarg everywhere Add **query_params to appropriate methods Sep 9, 2017
@non-Jedi
Copy link
Collaborator

non-Jedi commented Sep 10, 2017

Can't comment on the sphinx stuff since I'm not familiar with it at all, but I can tell you @Half-Shot will want it in a separate PR. I can also tell you it will almost certainly be very appreciated. ;)

Oh, also, you'll need to sign-off before anything is merged. See Contributing.rst

@Cadair
Copy link
Contributor Author

Cadair commented Sep 10, 2017

@non-Jedi This now only contains changes relevant to the title, the other doc fixes are in #144.

Copy link
Collaborator

@non-Jedi non-Jedi left a comment

Choose a reason for hiding this comment

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

I strongly prefer using an explicit query_params={} rather than **query_params since we already use **kwargs on login.

"""

def __init__(self, base_url, token=None, identity=None):
def __init__(self, base_url, token=None):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd like to keep the ability to use an api object per mxid without having to explicitly pass in the user_id kwarg on every request.

Common values for ``query_params`` are:
| ts: timestamp for event
| user_id: user id for transaction.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is good documentation. Need to add note in parentheses about these values only being useful for application services. Matrix newcomers may use python sdk as reference without reading the spec.

return self._send("GET", "/initialSync", query_params={"limit": limit})
query_params['limit'] = limit
return self._send("GET", "/initialSync",
query_params=query_params)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we're going with the convention of query_params being slurped by
**query_params, we should also consider slurping the query_params for _send
in the same way and splatting the query_params dict here into the _send
call.

Common values for ``query_params`` are:
| ts: timestamp for event
| user_id: user id for transaction.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same note as above for all documentation about params being AS-specific.

request = {
"timeout": timeout_ms
}
request = query_params
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should rename this variable query_params for consistency.


def sync(self, since=None, timeout_ms=30000, filter=None,
full_state=None, set_presence=None):
full_state=None, set_presence=None, **query_params):
Copy link
Collaborator

Choose a reason for hiding this comment

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

For consistency of the api, we should probably deprecate since, timeout_ms, filter, full_state, and set_presence kwargs in favor of slurping those things with **query_params. Unfortunately, I don't think there's any way to raise a deprecation warning for kwargs specified positionally and not raise a warning for the same kwargs specified by identifier. :/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you could do that by using a decorator that does funky things with signature objects. I think it might be Python 3 only though.

**query_params: Extra parameters to be sent in the HTTP request.
Common values for ``query_params`` are:
| ts: timestamp for event
Copy link
Collaborator

Choose a reason for hiding this comment

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

My gut says that ts is not a query_param that makes sense for this api call.

**query_params: Extra parameters to be sent in the HTTP request.
Common values for ``query_params`` are:
| ts: timestamp for event
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again, we need to make sure query_param values make sense for the api call before mentioning them in the documentation. user_id does, but ts probably doesn't.

query_params=query_params
)

def login(self, login_type, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is why I don't like using **query_params. Now we've introduced
inconsistency to our api methods. Sometimes, extra kwargs are treated as extra
json, and sometimes they're params.

Additionally, there might be times when an AS that's e2e-aware would make use of
login/logout api methods. Not sure; would need to double-check this.

"timeout": timeout,
"from": from_token
}
query_params.update(params)
Copy link
Collaborator

Choose a reason for hiding this comment

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

query_params["timeout"] = timeout
query_params["from"] = from_token

is slightly cleaner, but whatever.

@Cadair
Copy link
Contributor Author

Cadair commented Jan 3, 2018

@non-Jedi I have changed from **query_params to query_params=None and addressed all your other relevant comments I think.

@non-Jedi
Copy link
Collaborator

non-Jedi commented Jan 3, 2018

@Cadair thanks. Will take a look when I get a chance (probably after I finally get to the doc updates). Could you fix the failing flake8 test from Travis, please?

@Cadair Cadair closed this Jan 3, 2018
@Cadair Cadair reopened this Jan 3, 2018
@Cadair Cadair changed the title Add **query_params to appropriate methods Add query_params to appropriate methods Jan 3, 2018
@non-Jedi
Copy link
Collaborator

non-Jedi commented Jan 5, 2018

@Cadair turns out query_params={} was terrible advice on my part. Having
mutable default args in python doesn't produce the expected result. For example:

>>> def f(a=[]):
...     a.append(None)
...     return a
... 
>>> f()
[None]
>>> f()
[None, None]
>>> f()
[None, None, None]

Haven't thought about this enough to know what correct solution is, but I just
remembered the thing about mutable default kwargs, and thought I should note it
here. Sorry about that.

@Cadair
Copy link
Contributor Author

Cadair commented Jan 6, 2018

Yeah I thought about that the other day as well. It's not just query params that does that in the api though. I will go through and replace them all with None and add conditionals to change them to dicts where needed.

@non-Jedi
Copy link
Collaborator

Thinking about this again, I think AS support should be pushed entirely into a subclass of MatrixHttpApi that defines a custom _send. Then each public method of MatrixHttpApi would have an extras kwarg which would be forwarded unmodified to _send. For the AS subclass of MatrixHttpApi, _send would look into extras (make it a dict I suppose) for the AS-specific params. Thoughts?

I'm trying to push this sdk towards being as modular/composable as possible, and this seems like a step in the correct direction while also making things unique to different _send backends explicit rather than implicit.

@Cadair
Copy link
Contributor Author

Cadair commented May 14, 2018

I need to give this some thought. off the top of my head I feel like this is a good argument for **kwargs to get passed to _send._

@non-Jedi
Copy link
Collaborator

Could be. Need to look again at api methods that currently slurp kwargs first. If we do go that route, we should call the slurped kwargs **extras or something evocative of their role (but more generic than **query_params) rather than **kwargs. I'm gonna close this one for now and open a new issue since the approach here I think isn't quite right no matter what direction we end up going.

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