Avoid error on empty requests
geekingfrog opened this issue · 4 comments
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?
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
I'm not sure I understand the issue here. Where is the error?
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
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.
Oh right. Might be worth mentioning this in the doc of get_users
, or/and GetUsersRequest
.