twitch-rs / twitch_api

Rust library for talking with the Twitch API aka. "Helix", TMI and more! Use Twitch endpoints fearlessly!
Apache License 2.0
150 stars 33 forks source link

How do you actually use this library #433

Open RasAllhague opened 1 month ago

RasAllhague commented 1 month ago

If you use any example in a blank fresh project all i get is errors. Yes you can run the example but only when you are in the project. Try creating a new blank rust projekt, then add for example the code you provide under https://docs.rs/twitch_api/0.7.0-rc.7/twitch_api/ in the first example Get a channel:

code:

use twitch_api::helix::HelixClient;
use twitch_api::twitch_oauth2::{AccessToken, UserToken};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
    // Create the HelixClient, which is used to make requests to the Twitch API
    let client: HelixClient<reqwest::Client> = HelixClient::default();
    // Create a UserToken, which is used to authenticate requests.
    let token =
        UserToken::from_token(&client, AccessToken::from("mytoken"))
            .await?;

    println!(
        "Channel: {:?}",
        client.get_channel_from_login("twitchdev", &token).await?
    );

    Ok(())
}

dependencies (features all is because you cant be bothered to find the right dependency for a simple example):

[dependencies]
twitch_api = { version = "0.7.0-rc.7", features = ["all"] }
reqwest = { version = "0.12.8", features = ["json"] }
serde = "1"
serde_json = { version = "1" }
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }

What ends up happening is the code does not compile and only complains about reqwest not implementing HttpClient.

grafik

error[E0277]: the trait bound `reqwest::Client: HttpClient` is not satisfied
 --> src/main.rs:7:48
  |
7 |     let client: HelixClient<reqwest::Client> = HelixClient::default();
  |                                                ^^^^^^^^^^^ the trait `HttpClient` is not implemented for `reqwest::Client`, which is required by `HelixClient<'_, _>: Default`
  |
  = help: the following other types implement trait `HttpClient`:
            Arc<C>
            Box<C>
            DummyClient
            DummyHttpClient
  = note: required for `HelixClient<'_, reqwest::Client>` to implement `Default`

error[E0277]: the trait bound `reqwest::Client: ClientDefault<'static>` is not satisfied
 --> src/main.rs:7:48
  |
7 |     let client: HelixClient<reqwest::Client> = HelixClient::default();
  |                                                ^^^^^^^^^^^ the trait `ClientDefault<'static>` is not implemented for `reqwest::Client`, which is required by `HelixClient<'_, _>: Default`
  |
  = help: the trait `ClientDefault<'static>` is implemented for `DummyHttpClient`
  = note: required for `HelixClient<'_, reqwest::Client>` to implement `Default`

error[E0277]: the trait bound `reqwest::Client: HttpClient` is not satisfied
   --> src/main.rs:10:31
    |
10  |         UserToken::from_token(&client, AccessToken::from("mytoken"))
    |         --------------------- ^^^^^^^ the trait `HttpClient` is not implemented for `reqwest::Client`, which is required by `HelixClient<'_, reqwest::Client>: twitch_api::twitch_oauth2::client::Client`
    |         |
    |         required by a bound introduced by this call
    |
    = help: the following other types implement trait `HttpClient`:
              Arc<C>
              Box<C>
              DummyClient
              DummyHttpClient
    = note: required for `HelixClient<'_, reqwest::Client>` to implement `twitch_api::twitch_oauth2::client::Client`
note: required by a bound in `twitch_api::twitch_oauth2::UserToken::from_token`
   --> C:\Users\user\.cargo\registry\src\index.crates.io-6f17d22bba15001f\twitch_oauth2-0.12.9\src\tokens\user_token.rs:106:12      
    |
101 |     pub async fn from_token<C>(
    |                  ---------- required by a bound in this associated function
...
106 |         C: Client,
    |            ^^^^^^ required by this bound in `UserToken::from_token`

error[E0277]: the trait bound `reqwest::Client: HttpClient` is not satisfied
   --> src/main.rs:10:9
    |
10  |         UserToken::from_token(&client, AccessToken::from("mytoken"))
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HttpClient` is not implemented for `reqwest::Client`, which is required by `HelixClient<'_, reqwest::Client>: twitch_api::twitch_oauth2::client::Client`
    |
    = help: the following other types implement trait `HttpClient`:
              Arc<C>
              Box<C>
              DummyClient
              DummyHttpClient
    = note: required for `HelixClient<'_, reqwest::Client>` to implement `twitch_api::twitch_oauth2::client::Client`
note: required by a bound in `twitch_api::twitch_oauth2::UserToken::from_token`
   --> C:\Users\user\.cargo\registry\src\index.crates.io-6f17d22bba15001f\twitch_oauth2-0.12.9\src\tokens\user_token.rs:106:12      
    |
101 |     pub async fn from_token<C>(
    |                  ---------- required by a bound in this associated function
...
106 |         C: Client,
    |            ^^^^^^ required by this bound in `UserToken::from_token`

error[E0277]: the trait bound `reqwest::Client: HttpClient` is not satisfied
   --> src/main.rs:11:14
    |
11  |             .await?;
    |              ^^^^^ the trait `HttpClient` is not implemented for `reqwest::Client`, which is required by `HelixClient<'_, reqwest::Client>: twitch_api::twitch_oauth2::client::Client`
    |
    = help: the following other types implement trait `HttpClient`:
              Arc<C>
              Box<C>
              DummyClient
              DummyHttpClient
    = note: required for `HelixClient<'_, reqwest::Client>` to implement `twitch_api::twitch_oauth2::client::Client`
note: required by a bound in `twitch_api::twitch_oauth2::UserToken::from_token`
   --> C:\Users\user\.cargo\registry\src\index.crates.io-6f17d22bba15001f\twitch_oauth2-0.12.9\src\tokens\user_token.rs:106:12      
    |
101 |     pub async fn from_token<C>(
    |                  ---------- required by a bound in this associated function
...
106 |         C: Client,
    |            ^^^^^^ required by this bound in `UserToken::from_token`

error[E0599]: the method `get_channel_from_login` exists for struct `HelixClient<'_, Client>`, but its trait bounds were not satisfied
  --> src/main.rs:15:16
   |
15 |         client.get_channel_from_login("twitchdev", &token).await?
   |                ^^^^^^^^^^^^^^^^^^^^^^ method cannot be called on `HelixClient<'_, Client>` due to unsatisfied trait bounds    
   |
  ::: C:\Users\user\.cargo\registry\src\index.crates.io-6f17d22bba15001f\reqwest-0.12.8\src\async_impl\client.rs:71:1
   |
71 | pub struct Client {
   | ----------------- doesn't satisfy `reqwest::Client: HttpClient`
   |
   = note: the following trait bounds were not satisfied:
           `reqwest::Client: HttpClient`

If you try to use the code from the eventsub_websocket for example it gets even worse.

Nerixyz commented 1 month ago
  1. You're missing the reqwest feature for twitch_api
  2. You're running into dependency mismatches. You want request 0.12, but twitch_api depends on 0.11. If you change your dependency from reqwest 0.12 to 0.11, your code compiles.

If you want to use a newer version of reqwest, you need to depend on twitch_api as a Git dependency:

[dependencies]
reqwest = "0.12"
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
twitch_api = { git = "https://github.com/twitch-rs/twitch_api/", features = [
    "all",
    "reqwest",
] }

# workaround for https://github.com/twitch-rs/twitch_api/issues/256
[patch.crates-io.twitch_types]
git = "https://github.com/twitch-rs/twitch_api"

CC @Emilgardis I think we should release a new version on crates.io. The last one (0.7.0-rc.7) is over one year old and a lot of features have been added since then. Maybe we can get a few PRs in before, but it's probably better if we prioritize releasing soon.

RasAllhague commented 1 month ago

Thanks for the quick response. Tested it and it works with the workaround.

This is something you should mention somewhere close to the examples as there is currently no way for a new user to find this.

Emilgardis commented 1 month ago

yes, let's release just a rc, do some prs then 0.7, we've been on this rc for way to long :3 @Nerixyz