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

Avoid error on empty requests #223

Open
geekingfrog opened this issue Oct 16, 2021 · 4 comments
Open

Avoid error on empty requests #223

geekingfrog opened this issue Oct 16, 2021 · 4 comments
Assignees

Comments

@geekingfrog
Copy link

While using the crate, I noticed some requests will be rejected by twitch because empty. For example:

get_users::GetUsersRequest::builder()
    .id(vec![])
    .login(vec![])
    .build();

will be rejected. But in this case, the response will be empty. Would it be desirable to bypass the entire call and immediately returns a response in this case?
Or, looking at the RequestGet trait, it seems maybe it would be better to detect this error when parsing the response and recover?
What do you think?

@Emilgardis
Copy link
Member

the url we call for

get_users::GetUsersRequest::builder()
    .id(vec![])
    .login(vec![])
    .build();

would be https://api.twitch.tv/helix/users? (check with request.get_uri())

calling this is special-cased by twitch, where it returns the current user. For me for example

image

I'm not sure I understand the issue here. Where is the error?

@geekingfrog
Copy link
Author

This is interesting, it seems the behavior is different in the browser. Probably because you have some form of cookie?
Here's an example exhibiting the problem:

use twitch_api2::{twitch_oauth2::AppAccessToken, TwitchClient};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client_id = std::env::var("TWITCH_CLIENT_ID").unwrap().into();
    let client_secret = std::env::var("TWITCH_CLIENT_SECRET").unwrap().into();
    let client: TwitchClient<reqwest::Client> = TwitchClient::default();
    let token = AppAccessToken::get_app_access_token(&client, client_id, client_secret, vec![])
        .await
        .unwrap();

    let req = twitch_api2::helix::users::get_users::GetUsersRequest::builder()
        .id(vec![])
        .login(vec![])
        .build();

    let resp = client.helix.req_get(req, &token).await;
    dbg!(&resp);
    resp.expect("valid response");

    Ok(())
}

This gives:

[src/testtwitch.rs:18] &resp = Err(
    HelixRequestGetError(
        Error {
            error: "Bad Request",
            status: 400,
            message: "Must provide an ID, Login or OAuth Token.",
            uri: https://api.twitch.tv/helix/users?,
        },
    ),
)
thread 'main' panicked at 'valid response: HelixRequestGetError(Error { error: "Bad Request", status: 400, message: "Must provide an ID, Login or OAuth Token.", uri: https://api.twitch.tv/helix/users? })', src/testtwitch.rs:19:10

I'm using tokio version "1.0.1" and twitch_api version 0.6.0-rc.3

@Emilgardis
Copy link
Member

Aha, no default user associated with apptokens so twitch throws an error on empty query. There are no differences with using a browser, I jus have an addon setting the correct headers, using an user token.

I dont think it's possible or usable for twitch_api2 to catch this kind of error without speaking to twitch.

@geekingfrog
Copy link
Author

Oh right. Might be worth mentioning this in the doc of get_users, or/and GetUsersRequest.

@Emilgardis Emilgardis self-assigned this Nov 6, 2021
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