Skip to content

refactor: Use (mostly) References in Requests #280

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

Merged
merged 8 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

### Breaking changes

- Requests for helix endpoints have been converted to take `Cow`s.
This change means the `builder()` methods are harder to use, consider using the new methods on
each request which provide the same functionality but with better ergonomics.
See the top-level documentation for a endpoint for more examples.
- Crate name changed: `twitch_api2` -> `twitch_api`, also changed to new org `twitch-rs`
- All (most) types are now living in their own crate `twitch_types`
- Features for clients are now named after the client, e.g feature `reqwest_client` is now simply `reqwest`
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ crypto_hmac = { package = "hmac", version = "0.12.1", optional = true }
aliri_braid = "0.2.4"
futures = { version = "0.3.25", optional = true }
hyper = { version = "0.14.20", optional = true }
twitch_types = { version = "0.3.5", path = "./twitch_types" }
twitch_types = { version = "0.3.6", path = "./twitch_types" }

[features]
default = []
Expand Down
8 changes: 4 additions & 4 deletions examples/automod_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>

let req =
twitch_api::helix::moderation::CheckAutoModStatusRequest::broadcaster_id(broadcaster_id);
let data =
twitch_api::helix::moderation::CheckAutoModStatusBody::new("123", args.collect::<String>());
let text = args.collect::<String>();
let data = twitch_api::helix::moderation::CheckAutoModStatusBody::new("123", &text);
println!("data: {:?}", data);
let response = client.req_post(req, vec![data], &token).await?;
println!("{:?}", response.data);
let response = client.req_post(req, &[&data], &token).await?.data;
println!("{response:?}");

Ok(())
}
4 changes: 2 additions & 2 deletions examples/channel_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
let token = UserToken::from_existing(&client, token, None, None).await?;

let user = client
.get_user_from_login(args.next().unwrap(), &token)
.get_user_from_login(&*args.next().unwrap(), &token)
.await?
.expect("no user found");

let channel = client
.get_channel_from_id(user.id.clone(), &token)
.get_channel_from_id(&user.id, &token)
.await?
.expect("no channel found");

Expand Down
3 changes: 1 addition & 2 deletions examples/channel_information_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
.map(AccessToken::new)
.expect("Please set env: TWITCH_TOKEN or pass token as first argument");
let token = UserToken::from_existing(&client, token, None, None).await?;
let id = token.user_id.clone();

let resp = client
.req_get_custom(
helix::channels::GetChannelInformationRequest::broadcaster_id(id),
helix::channels::GetChannelInformationRequest::broadcaster_id(&token.user_id),
&token,
)
.await
Expand Down
19 changes: 15 additions & 4 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use twitch_api::{helix::streams::GetStreamsRequest, TwitchClient};
use twitch_api::{helix::streams::GetStreamsRequest, types, TwitchClient};
use twitch_oauth2::{AccessToken, UserToken};
fn main() {
use std::error::Error;
Expand Down Expand Up @@ -36,9 +36,20 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
)
.await?;

let req = GetStreamsRequest::user_login(args.next().expect("please provide an username"));
foo_client.client.helix.clone_client();
let response = foo_client.client.helix.req_get(req, &token).await?;
println!("{:?}", response);
let response = foo_client
.client
.helix
.req_get(
GetStreamsRequest::user_logins(
&[types::UserNameRef::from_str(
&args.next().expect("please provide an username"),
)][..],
),
&token,
)
.await?
.data;
println!("{response:?}");
Ok(())
}
2 changes: 1 addition & 1 deletion examples/eventsub/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub async fn run(opts: &Opts) -> eyre::Result<()> {
.await?;

let broadcaster = client
.get_user_from_login(&*opts.broadcaster_login, &token)
.get_user_from_login(&opts.broadcaster_login, &token)
.await?
.ok_or_else(|| eyre::eyre!("broadcaster not found"))?;

Expand Down
4 changes: 3 additions & 1 deletion examples/eventsub/src/twitch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ pub async fn is_live<'a>(
tracing::info!("checking if live");
if let Some(stream) = client
.req_get(
helix::streams::get_streams::GetStreamsRequest::user_id(config.broadcaster.id.clone()),
helix::streams::get_streams::GetStreamsRequest::user_ids(
&[config.broadcaster.id.as_ref()][..],
),
token,
)
.await
Expand Down
8 changes: 7 additions & 1 deletion examples/followed_streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
.try_collect::<Vec<_>>()
.await?;
let games = client
.get_games_by_id(streams.iter().map(|s| s.game_id.clone()), &token)
.get_games_by_id(
&streams
.iter()
.map(|s| s.game_id.as_ref())
.collect::<Vec<_>>(),
&token,
)
.await?;

println!(
Expand Down
17 changes: 12 additions & 5 deletions examples/get_channel_status.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use twitch_api::{helix::streams::GetStreamsRequest, HelixClient};
use twitch_api::{helix::streams::GetStreamsRequest, types, HelixClient};
use twitch_oauth2::{AccessToken, UserToken};

fn main() {
Expand Down Expand Up @@ -31,10 +31,17 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
.await
.unwrap();

let req = GetStreamsRequest::user_login(args.next().unwrap());
let response = client
.req_get(
GetStreamsRequest::user_logins(
&[types::UserNameRef::from_str(&args.next().unwrap())][..],
),
&token,
)
.await
.unwrap()
.data;

let response = client.req_get(req, &token).await.unwrap();

println!("Stream information:\n\t{:?}", response.data);
println!("Stream information:\n\t{response:?}");
Ok(())
}
8 changes: 4 additions & 4 deletions examples/mock_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
.await?;

let user = client
.get_user_from_id(&*user_id, &token)
.get_user_from_id(&user_id, &token)
.await?
.expect("no user found");

let _channel = client
.get_channel_from_id(&*user_id, &token)
.get_channel_from_id(&user_id, &token)
.await?
.expect("no channel found");
let _channel = client
.get_channel_from_id(user.id.clone(), &token)
.get_channel_from_id(&user.id, &token)
.await?
.expect("no channel found");

Expand All @@ -86,7 +86,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
.await?;
dbg!(search.get(0));
let _total = client
.get_total_followers_from_id(search.get(0).unwrap().id.clone(), &token)
.get_total_followers_from_id(&search.get(0).unwrap().id, &token)
.await?;
dbg!(_total);
let streams: Vec<_> = client.get_followed_streams(&token).try_collect().await?;
Expand Down
8 changes: 4 additions & 4 deletions examples/modify_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
let broadcaster_id = token.validate_token(&client).await?.user_id.unwrap();

let req = twitch_api::helix::channels::ModifyChannelInformationRequest::broadcaster_id(
broadcaster_id,
&broadcaster_id,
);

let mut data = twitch_api::helix::channels::ModifyChannelInformationBody::new();
data.title("Hello World!");
let mut body = twitch_api::helix::channels::ModifyChannelInformationBody::new();
body.title("Hello World!");

println!("scopes: {:?}", token.scopes());
let response = client.req_patch(req, data, &token).await?;
let response = client.req_patch(req, body, &token).await?;
println!("{:?}", response);

Ok(())
Expand Down
Loading