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

Document how to use dotted parameters with Twitter API v2 #34

Open
boringhexi opened this issue Oct 1, 2020 · 1 comment
Open

Document how to use dotted parameters with Twitter API v2 #34

boringhexi opened this issue Oct 1, 2020 · 1 comment

Comments

@boringhexi
Copy link

A number of endpoints in Twitter API v2 take optional parameters that contain a dot/period. Example: On GET /2/tweets/:id (lookup by single ID) under the section Query Parameters, you can see optional parameters with names like media.fields and user.fields.

In peony-twitter, parameters are passed as keyword arguments, whose names cannot contain a period. The workaround I found (and maybe this is the intended way) is to make a dict of dotted parameters and pass them into the method call as **kwargs. I searched through the documentation and didn't find anything about this. Should this be documented?

Example code that uses user.fields, both the wrong way and right way:

import asyncio, peony

twclient_v2user = peony.PeonyClient(
    consumer_key=TWITTER_CONSUMER_KEY,
    consumer_secret=TWITTER_CONSUMER_SECRET,
    access_token=TWITTER_ACCESS_TOKEN,
    access_token_secret=TWITTER_ACCESS_TOKEN_SECRET,
    api_version="2",
    suffix="",
)

# Goal: get a tweet that includes the author's description/bio
# This will not work
tweet_v2_task = twclient_v2user.api.tweets["1292589384265601029"].get(
    expansions="author_id",
    user.fields="description"  # "user.fields" is invalid python syntax, but Twitter expects that name
)
# However, this will work
dotted_parameters = {"user.fields" : "description"}
tweet_v2_task = twclient_v2user.api.tweets["1292589384265601029"].get(
    expansions="author_id",
    **dotted_parameters
)

loop = asyncio.get_event_loop()
tweet_v2 = loop.run_until_complete(tweet_v2_task)
loop.close()

print(tweet_v2)
@odrling
Copy link
Owner

odrling commented Oct 1, 2020

I haven't tested the API v2 with Peony yet and I wanted to test it before the next release.
I did see these parameters and I was wondering how I should set them.

So it should be documented when I've made up my mind on how it should be used.
I'm thinking about adding a parameter to the client that would set the default parameters for every request.
You would still be able to set it for each request of course.

Dotted parameters obviously won't work the same way as the other parameters. I still want to be able to use them directly as parameters (not by unwrapping a dict, though that would still work). Probably it will be a Parameter class, that would make it look like this:

tweet_v2_task = twclient_v2user.api.tweets["1292589384265601029"].get(
    Parameter("user.fields", "description"),   # it has to be before the keyword arguments
    expansions="author_id"
)```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants