Skip to content

Commit 5228a45

Browse files
bors[bot]NerixyzEmilgardis
authored
Merge #280
280: refactor: Use (mostly) References in Requests r=Emilgardis a=Nerixyz Since #194 only implemented a few references and a lot of changes happened in the meantime, I went ahead and tried to refactor (most) request fields to use references. "_most_" in this case means that (1) `after`/`cursor`s remain owned (since I couldn't get pagination to work) and (2) eventsub requests remain owned. I think having (mostly) non-owned requests is already a big step. * Some fields are `&str` or `&types::TypeRef` but should be `Cow<'_, str>` for the `Deserialize` impl (since they might be encoded as escaped characters in JSON). These are mainly user-inputs. user-ids, user-logins, UUIDs and similar can remain, since they aren't escaped in JSON. I think it's fair to make sure `Deserialize` works for JSON, but it shouldn't need to work for _all_ encodings. * The use of `Cow<'_, [&types::TypeRef]>` makes passing (static) slices a bit ugly, because `&["foo"]` is `&[&str; 1]` and `&["foo"][..]` is `&[&str]` and `Cow`'s `Into` only accepts the latter. Co-authored-by: Nerixyz <[email protected]> Co-authored-by: Emil Gardström <[email protected]>
2 parents 4232145 + c71717f commit 5228a45

File tree

116 files changed

+1962
-1957
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+1962
-1957
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
### Breaking changes
1010

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

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ crypto_hmac = { package = "hmac", version = "0.12.1", optional = true }
5757
aliri_braid = "0.2.4"
5858
futures = { version = "0.3.25", optional = true }
5959
hyper = { version = "0.14.20", optional = true }
60-
twitch_types = { version = "0.3.5", path = "./twitch_types" }
60+
twitch_types = { version = "0.3.6", path = "./twitch_types" }
6161

6262
[features]
6363
default = []

examples/automod_check.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
3535

3636
let req =
3737
twitch_api::helix::moderation::CheckAutoModStatusRequest::broadcaster_id(broadcaster_id);
38-
let data =
39-
twitch_api::helix::moderation::CheckAutoModStatusBody::new("123", args.collect::<String>());
38+
let text = args.collect::<String>();
39+
let data = twitch_api::helix::moderation::CheckAutoModStatusBody::new("123", &text);
4040
println!("data: {:?}", data);
41-
let response = client.req_post(req, vec![data], &token).await?;
42-
println!("{:?}", response.data);
41+
let response = client.req_post(req, &[&data], &token).await?.data;
42+
println!("{response:?}");
4343

4444
Ok(())
4545
}

examples/channel_information.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
2626
let token = UserToken::from_existing(&client, token, None, None).await?;
2727

2828
let user = client
29-
.get_user_from_login(args.next().unwrap(), &token)
29+
.get_user_from_login(&*args.next().unwrap(), &token)
3030
.await?
3131
.expect("no user found");
3232

3333
let channel = client
34-
.get_channel_from_id(user.id.clone(), &token)
34+
.get_channel_from_id(&user.id, &token)
3535
.await?
3636
.expect("no channel found");
3737

examples/channel_information_custom.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
2424
.map(AccessToken::new)
2525
.expect("Please set env: TWITCH_TOKEN or pass token as first argument");
2626
let token = UserToken::from_existing(&client, token, None, None).await?;
27-
let id = token.user_id.clone();
2827

2928
let resp = client
3029
.req_get_custom(
31-
helix::channels::GetChannelInformationRequest::broadcaster_id(id),
30+
helix::channels::GetChannelInformationRequest::broadcaster_id(&token.user_id),
3231
&token,
3332
)
3433
.await

examples/client.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use twitch_api::{helix::streams::GetStreamsRequest, TwitchClient};
1+
use twitch_api::{helix::streams::GetStreamsRequest, types, TwitchClient};
22
use twitch_oauth2::{AccessToken, UserToken};
33
fn main() {
44
use std::error::Error;
@@ -36,9 +36,20 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
3636
)
3737
.await?;
3838

39-
let req = GetStreamsRequest::user_login(args.next().expect("please provide an username"));
4039
foo_client.client.helix.clone_client();
41-
let response = foo_client.client.helix.req_get(req, &token).await?;
42-
println!("{:?}", response);
40+
let response = foo_client
41+
.client
42+
.helix
43+
.req_get(
44+
GetStreamsRequest::user_logins(
45+
&[types::UserNameRef::from_str(
46+
&args.next().expect("please provide an username"),
47+
)][..],
48+
),
49+
&token,
50+
)
51+
.await?
52+
.data;
53+
println!("{response:?}");
4354
Ok(())
4455
}

examples/eventsub/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub async fn run(opts: &Opts) -> eyre::Result<()> {
7474
.await?;
7575

7676
let broadcaster = client
77-
.get_user_from_login(&*opts.broadcaster_login, &token)
77+
.get_user_from_login(&opts.broadcaster_login, &token)
7878
.await?
7979
.ok_or_else(|| eyre::eyre!("broadcaster not found"))?;
8080

examples/eventsub/src/twitch.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ pub async fn is_live<'a>(
264264
tracing::info!("checking if live");
265265
if let Some(stream) = client
266266
.req_get(
267-
helix::streams::get_streams::GetStreamsRequest::user_id(config.broadcaster.id.clone()),
267+
helix::streams::get_streams::GetStreamsRequest::user_ids(
268+
&[config.broadcaster.id.as_ref()][..],
269+
),
268270
token,
269271
)
270272
.await

examples/followed_streams.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
3737
.try_collect::<Vec<_>>()
3838
.await?;
3939
let games = client
40-
.get_games_by_id(streams.iter().map(|s| s.game_id.clone()), &token)
40+
.get_games_by_id(
41+
&streams
42+
.iter()
43+
.map(|s| s.game_id.as_ref())
44+
.collect::<Vec<_>>(),
45+
&token,
46+
)
4147
.await?;
4248

4349
println!(

examples/get_channel_status.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use twitch_api::{helix::streams::GetStreamsRequest, HelixClient};
1+
use twitch_api::{helix::streams::GetStreamsRequest, types, HelixClient};
22
use twitch_oauth2::{AccessToken, UserToken};
33

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

34-
let req = GetStreamsRequest::user_login(args.next().unwrap());
34+
let response = client
35+
.req_get(
36+
GetStreamsRequest::user_logins(
37+
&[types::UserNameRef::from_str(&args.next().unwrap())][..],
38+
),
39+
&token,
40+
)
41+
.await
42+
.unwrap()
43+
.data;
3544

36-
let response = client.req_get(req, &token).await.unwrap();
37-
38-
println!("Stream information:\n\t{:?}", response.data);
45+
println!("Stream information:\n\t{response:?}");
3946
Ok(())
4047
}

examples/mock_api.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
5959
.await?;
6060

6161
let user = client
62-
.get_user_from_id(&*user_id, &token)
62+
.get_user_from_id(&user_id, &token)
6363
.await?
6464
.expect("no user found");
6565

6666
let _channel = client
67-
.get_channel_from_id(&*user_id, &token)
67+
.get_channel_from_id(&user_id, &token)
6868
.await?
6969
.expect("no channel found");
7070
let _channel = client
71-
.get_channel_from_id(user.id.clone(), &token)
71+
.get_channel_from_id(&user.id, &token)
7272
.await?
7373
.expect("no channel found");
7474

@@ -86,7 +86,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
8686
.await?;
8787
dbg!(search.get(0));
8888
let _total = client
89-
.get_total_followers_from_id(search.get(0).unwrap().id.clone(), &token)
89+
.get_total_followers_from_id(&search.get(0).unwrap().id, &token)
9090
.await?;
9191
dbg!(_total);
9292
let streams: Vec<_> = client.get_followed_streams(&token).try_collect().await?;

examples/modify_channel.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
3333
let broadcaster_id = token.validate_token(&client).await?.user_id.unwrap();
3434

3535
let req = twitch_api::helix::channels::ModifyChannelInformationRequest::broadcaster_id(
36-
broadcaster_id,
36+
&broadcaster_id,
3737
);
3838

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

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

4646
Ok(())

0 commit comments

Comments
 (0)