#[non_exhaustive]pub struct Response<R, D>where
R: Request,
D: DeserializeOwned + PartialEq,{
pub data: D,
pub pagination: Option<Cursor>,
pub request: Option<R>,
pub total: Option<i64>,
pub other: Option<Map<String, Value>>,
}
Available on crate feature
helix
only.Expand description
Response retrieved from endpoint. Data is the type in Request::Response
Fields (Non-exhaustive)
This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.data: D
Twitch’s response field for data
.
pagination: Option<Cursor>
A cursor value, to be used in a subsequent request to specify the starting point of the next set of results.
request: Option<R>
The request that was sent, used for pagination.
total: Option<i64>
Response would return this many results if fully paginated. Sometimes this is not emmitted or correct for this purpose, in those cases, this value will be None
.
other: Option<Map<String, Value>>
Fields which are not part of the data response, but are returned by the endpoint.
See for example Get Broadcaster Subscriptions which returns this.
Implementations
sourceimpl Response<GetBroadcasterSubscriptionsRequest<'_>, Vec<BroadcasterSubscription>>
impl Response<GetBroadcasterSubscriptionsRequest<'_>, Vec<BroadcasterSubscription>>
sourcepub fn points(&self) -> Result<i64, BroadcasterSubscriptionPointsError>
pub fn points(&self) -> Result<i64, BroadcasterSubscriptionPointsError>
The current number of subscriber points earned by this broadcaster.
sourceimpl<R, D> Response<R, D>where
R: Request,
D: DeserializeOwned + PartialEq,
impl<R, D> Response<R, D>where
R: Request,
D: DeserializeOwned + PartialEq,
sourceimpl<R, D, T> Response<R, D>where
R: Request,
D: IntoIterator<Item = T> + PartialEq + DeserializeOwned,
impl<R, D, T> Response<R, D>where
R: Request,
D: IntoIterator<Item = T> + PartialEq + DeserializeOwned,
sourcepub fn first(self) -> Option<T>
pub fn first(self) -> Option<T>
Get first result of this response.
Examples found in repository?
src/helix/client/client_ext.rs (line 28)
≺ ≻
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
pub async fn get_user_from_login<T>(
&'a self,
login: impl Into<&types::UserNameRef>,
token: &T,
) -> Result<Option<helix::users::User>, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
self.req_get(
helix::users::GetUsersRequest::logins(&[login.into()][..]),
token,
)
.await
.map(|response| response.first())
}
/// Get [User](helix::users::User) from user id
pub async fn get_user_from_id<T>(
&'a self,
id: impl Into<&types::UserIdRef>,
token: &T,
) -> Result<Option<helix::users::User>, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
self.req_get(helix::users::GetUsersRequest::ids(&[id.into()][..]), token)
.await
.map(|response| response.first())
}
/// Get [ChannelInformation](helix::channels::ChannelInformation) from a broadcasters login
pub async fn get_channel_from_login<T>(
&'a self,
login: impl Into<&types::UserNameRef>,
token: &T,
) -> Result<Option<helix::channels::ChannelInformation>, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
if let Some(user) = self.get_user_from_login(login.into(), token).await? {
self.get_channel_from_id(&user.id, token).await
} else {
Ok(None)
}
}
/// Get [ChannelInformation](helix::channels::ChannelInformation) from a broadcasters id
pub async fn get_channel_from_id<'b, T>(
&'a self,
id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &T,
) -> Result<Option<helix::channels::ChannelInformation>, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
self.req_get(
helix::channels::GetChannelInformationRequest::broadcaster_id(id),
token,
)
.await
.map(|response| response.first())
}
/// Get chatters in a stream [Chatter][helix::chat::Chatter]
///
/// `batch_size` sets the amount of chatters to retrieve per api call, max 1000, defaults to 100.
///
/// # Examples
///
/// ```rust, no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # let client: helix::HelixClient<'static, twitch_api::client::DummyHttpClient> = helix::HelixClient::default();
/// # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
/// # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
/// use twitch_api::helix;
/// use futures::TryStreamExt;
///
/// let chatters: Vec<helix::chat::Chatter> = client.get_chatters("1234", "4321", 1000, &token).try_collect().await?;
///
/// # Ok(()) }
/// ```
pub fn get_chatters<T>(
&'a self,
broadcaster_id: impl Into<&'a types::UserIdRef>,
moderator_id: impl Into<&'a types::UserIdRef>,
batch_size: impl Into<Option<usize>>,
token: &'a T,
) -> std::pin::Pin<
Box<dyn futures::Stream<Item = Result<helix::chat::Chatter, ClientError<'a, C>>> + 'a>,
>
where
T: TwitchToken + Send + Sync + ?Sized,
{
let req = helix::chat::GetChattersRequest {
first: batch_size.into(),
..helix::chat::GetChattersRequest::new(broadcaster_id.into(), moderator_id.into())
};
make_stream(req, token, self, std::collections::VecDeque::from)
}
/// Search [Categories](helix::search::Category)
///
/// # Examples
///
/// ```rust, no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # let client: helix::HelixClient<'static, twitch_api::client::DummyHttpClient> = helix::HelixClient::default();
/// # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
/// # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
/// use twitch_api::helix;
/// use futures::TryStreamExt;
///
/// let categories: Vec<helix::search::Category> = client.search_categories("Fortnite", &token).try_collect().await?;
///
/// # Ok(()) }
/// ```
pub fn search_categories<T>(
&'a self,
query: impl Into<&'a str>,
token: &'a T,
) -> std::pin::Pin<
Box<dyn futures::Stream<Item = Result<helix::search::Category, ClientError<'a, C>>> + 'a>,
>
where
T: TwitchToken + Send + Sync + ?Sized,
{
let req = helix::search::SearchCategoriesRequest::query(query.into()).first(100);
make_stream(req, token, self, std::collections::VecDeque::from)
}
/// Search [Channels](helix::search::Channel) via channel name or description
///
/// # Examples
///
/// ```rust, no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # let client: helix::HelixClient<'static, twitch_api::client::DummyHttpClient> = helix::HelixClient::default();
/// # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
/// # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
/// use twitch_api::helix;
/// use futures::TryStreamExt;
///
/// let channel: Vec<helix::search::Channel> = client.search_channels("twitchdev", false, &token).try_collect().await?;
///
/// # Ok(()) }
/// ```
pub fn search_channels<T>(
&'a self,
query: impl Into<&'a str>,
live_only: bool,
token: &'a T,
) -> std::pin::Pin<
Box<dyn futures::Stream<Item = Result<helix::search::Channel, ClientError<'a, C>>> + 'a>,
>
where
T: TwitchToken + Send + Sync + ?Sized,
{
let req = helix::search::SearchChannelsRequest::query(query.into()).live_only(live_only);
make_stream(req, token, self, std::collections::VecDeque::from)
}
/// Get information on a [follow relationship](helix::users::FollowRelationship)
///
/// Can be used to see if X follows Y
///
/// # Examples
///
/// ```rust, no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # let client: helix::HelixClient<'static, twitch_api::client::DummyHttpClient> = helix::HelixClient::default();
/// # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
/// # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
/// use twitch_api::{types, helix};
/// use futures::TryStreamExt;
///
/// // Get the followers of channel "1234"
/// let followers: Vec<helix::users::FollowRelationship> = client.get_follow_relationships(Some("1234".into()), None, &token).try_collect().await?;
///
/// # Ok(()) }
/// ```
pub fn get_follow_relationships<T>(
&'a self,
to_id: impl Into<Option<&'a types::UserIdRef>>,
from_id: impl Into<Option<&'a types::UserIdRef>>,
token: &'a T,
) -> std::pin::Pin<
Box<
dyn futures::Stream<Item = Result<helix::users::FollowRelationship, ClientError<'a, C>>>
+ Send
+ 'a,
>,
>
where
T: TwitchToken + Send + Sync + ?Sized,
{
let mut req = helix::users::GetUsersFollowsRequest::empty();
req.to_id = to_id.into().map(Cow::Borrowed);
req.from_id = from_id.into().map(Cow::Borrowed);
make_stream(req, token, self, |s| {
std::collections::VecDeque::from(s.follow_relationships)
})
}
/// Get authenticated users' followed [streams](helix::streams::Stream)
///
/// Requires token with scope [`user:read:follows`](twitch_oauth2::Scope::UserReadFollows).
///
/// # Examples
///
/// ```rust, no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # let client: helix::HelixClient<'static, twitch_api::client::DummyHttpClient> = helix::HelixClient::default();
/// # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
/// # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
/// use twitch_api::helix;
/// use futures::TryStreamExt;
///
/// let channels: Vec<helix::streams::Stream> = client.get_followed_streams(&token).try_collect().await?;
///
/// # Ok(()) }
/// ```
pub fn get_followed_streams<T>(
&'a self,
token: &'a T,
) -> std::pin::Pin<
Box<dyn futures::Stream<Item = Result<helix::streams::Stream, ClientError<'a, C>>> + 'a>,
>
where
T: TwitchToken + Send + Sync + ?Sized,
{
use futures::StreamExt;
let user_id = match token
.user_id()
.ok_or_else(|| ClientRequestError::Custom("no user_id found on token".into()))
{
Ok(t) => t,
Err(e) => return futures::stream::once(async { Err(e) }).boxed(),
};
let req = helix::streams::GetFollowedStreamsRequest::user_id(user_id);
make_stream(req, token, self, std::collections::VecDeque::from)
}
/// Get authenticated broadcasters' [subscribers](helix::subscriptions::BroadcasterSubscription)
///
/// # Examples
///
/// ```rust, no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # let client: helix::HelixClient<'static, twitch_api::client::DummyHttpClient> = helix::HelixClient::default();
/// # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
/// # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
/// use twitch_api::helix;
/// use futures::TryStreamExt;
///
/// let subs: Vec<helix::subscriptions::BroadcasterSubscription> = client.get_broadcaster_subscriptions(&token).try_collect().await?;
///
/// # Ok(()) }
/// ```
pub fn get_broadcaster_subscriptions<T>(
&'a self,
token: &'a T,
) -> std::pin::Pin<
Box<
dyn futures::Stream<
Item = Result<
helix::subscriptions::BroadcasterSubscription,
ClientError<'a, C>,
>,
> + 'a,
>,
>
where
T: TwitchToken + Send + Sync + ?Sized,
{
use futures::StreamExt;
let user_id = match token
.user_id()
.ok_or_else(|| ClientRequestError::Custom("no user_id found on token".into()))
{
Ok(t) => t,
Err(e) => return futures::stream::once(async { Err(e) }).boxed(),
};
let req = helix::subscriptions::GetBroadcasterSubscriptionsRequest::broadcaster_id(user_id);
make_stream(req, token, self, std::collections::VecDeque::from)
}
/// Get all moderators in a channel [Get Moderators](helix::moderation::GetModeratorsRequest)
///
/// # Examples
///
/// ```rust, no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # let client: helix::HelixClient<'static, twitch_api::client::DummyHttpClient> = helix::HelixClient::default();
/// # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
/// # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
/// use twitch_api::helix;
/// use futures::TryStreamExt;
///
/// let moderators: Vec<helix::moderation::Moderator> = client.get_moderators_in_channel_from_id("twitchdev", &token).try_collect().await?;
///
/// # Ok(()) }
/// ```
pub fn get_moderators_in_channel_from_id<'b: 'a, T>(
&'a self,
broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &'a T,
) -> std::pin::Pin<
Box<
dyn futures::Stream<Item = Result<helix::moderation::Moderator, ClientError<'a, C>>>
+ 'a,
>,
>
where
T: TwitchToken + Send + Sync + ?Sized,
{
let req = helix::moderation::GetModeratorsRequest::broadcaster_id(broadcaster_id);
make_stream(req, token, self, std::collections::VecDeque::from)
}
/// Get all banned users in a channel [Get Banned Users](helix::moderation::GetBannedUsersRequest)
///
/// # Examples
///
/// ```rust, no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # let client: helix::HelixClient<'static, twitch_api::client::DummyHttpClient> = helix::HelixClient::default();
/// # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
/// # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
/// use twitch_api::helix;
/// use futures::TryStreamExt;
///
/// let moderators: Vec<helix::moderation::BannedUser> = client.get_banned_users_in_channel_from_id("twitchdev", &token).try_collect().await?;
///
/// # Ok(()) }
/// ```
pub fn get_banned_users_in_channel_from_id<'b: 'a, T>(
&'a self,
broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &'a T,
) -> std::pin::Pin<
Box<
dyn futures::Stream<Item = Result<helix::moderation::BannedUser, ClientError<'a, C>>>
+ 'a,
>,
>
where
T: TwitchToken + Send + Sync + ?Sized,
{
let req = helix::moderation::GetBannedUsersRequest::broadcaster_id(broadcaster_id);
make_stream(req, token, self, std::collections::VecDeque::from)
}
/// Get a users, with login, follow count
pub async fn get_total_followers_from_login<'b, T>(
&'a self,
login: impl types::IntoCow<'b, types::UserNameRef> + 'b,
token: &T,
) -> Result<Option<i64>, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
if let Some(user) = self.get_user_from_login(&*login.to_cow(), token).await? {
self.get_total_followers_from_id(&user.id, token)
.await
.map(Some)
} else {
Ok(None)
}
}
/// Get a users, with id, follow count
///
/// # Notes
///
/// This returns zero if the user doesn't exist
pub async fn get_total_followers_from_id<'b, T>(
&'a self,
to_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &T,
) -> Result<i64, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
let resp = self
.req_get(
helix::users::GetUsersFollowsRequest::followers(to_id),
token,
)
.await?;
Ok(resp.data.total)
}
/// Get games by ID. Can only be at max 100 ids.
pub async fn get_games_by_id<T>(
&'a self,
ids: impl AsRef<[&'a types::CategoryIdRef]>,
token: &T,
) -> Result<std::collections::HashMap<types::CategoryId, helix::games::Game>, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
let ids = ids.as_ref();
if ids.len() > 100 {
return Err(ClientRequestError::Custom("too many IDs, max 100".into()));
}
let resp = self
.req_get(helix::games::GetGamesRequest::ids(ids), token)
.await?;
Ok(resp
.data
.into_iter()
.map(|g: helix::games::Game| (g.id.clone(), g))
.collect())
}
/// Block a user
pub async fn block_user<'b, T>(
&'a self,
target_user_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &T,
) -> Result<helix::users::BlockUser, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
Ok(self
.req_put(
helix::users::BlockUserRequest::block_user(target_user_id),
helix::EmptyBody,
token,
)
.await?
.data)
}
/// Unblock a user
pub async fn unblock_user<'b, T>(
&'a self,
target_user_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &T,
) -> Result<helix::users::UnblockUser, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
Ok(self
.req_delete(
helix::users::UnblockUserRequest::unblock_user(target_user_id),
token,
)
.await?
.data)
}
/// Ban a user
pub async fn ban_user<'b, T>(
&'a self,
target_user_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
reason: impl Into<&'b str>,
duration: impl Into<Option<u32>>,
broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
moderator_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &T,
) -> Result<helix::moderation::BanUser, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
Ok(self
.req_post(
helix::moderation::BanUserRequest::new(broadcaster_id, moderator_id),
helix::moderation::BanUserBody::new(target_user_id, reason.into(), duration),
token,
)
.await?
.data)
}
/// Unban a user
pub async fn unban_user<'b, T>(
&'a self,
target_user_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
moderator_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &T,
) -> Result<helix::moderation::UnbanUserResponse, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
Ok(self
.req_delete(
helix::moderation::UnbanUserRequest::new(
broadcaster_id,
moderator_id,
target_user_id,
),
token,
)
.await?
.data)
}
// FIXME: Example should use https://github.com/twitch-rs/twitch_api/issues/162
/// Get all scheduled streams in a channel.
///
/// # Notes
///
/// Make sure to limit the data here using [`try_take_while`](futures::stream::TryStreamExt::try_take_while), otherwise this will never end on recurring scheduled streams.
///
///
/// # Examples
///
/// ```rust, no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # let client: helix::HelixClient<'static, twitch_api::client::DummyHttpClient> = helix::HelixClient::default();
/// # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
/// # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
/// use twitch_api::helix;
/// use futures::TryStreamExt;
///
/// let schedule: Vec<helix::schedule::Segment> = client
/// .get_channel_schedule("twitchdev", &token)
/// .try_take_while(|s| {
/// futures::future::ready(Ok(!s.start_time.as_str().starts_with("2021-10")))
/// })
/// .try_collect()
/// .await?;
///
/// # Ok(()) }
/// ```
pub fn get_channel_schedule<'b: 'a, T>(
&'a self,
broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &'a T,
) -> std::pin::Pin<
Box<dyn futures::Stream<Item = Result<helix::schedule::Segment, ClientError<'a, C>>> + 'a>,
>
where
T: TwitchToken + Send + Sync + ?Sized,
{
let req = helix::schedule::GetChannelStreamScheduleRequest::broadcaster_id(broadcaster_id);
make_stream(req, token, self, |broadcasts| broadcasts.segments.into())
}
/// Get all global emotes
pub async fn get_global_emotes<T>(
&'a self,
token: &T,
) -> Result<Vec<helix::chat::GlobalEmote>, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
let req = helix::chat::GetGlobalEmotesRequest::new();
Ok(self.req_get(req, token).await?.data)
}
/// Get channel emotes in channel with user id
pub async fn get_channel_emotes_from_id<'b, T>(
&'a self,
user_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &T,
) -> Result<Vec<helix::chat::ChannelEmote>, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
let req = helix::chat::GetChannelEmotesRequest::broadcaster_id(user_id);
Ok(self.req_get(req, token).await?.data)
}
/// Get channel emotes in channel with user login
pub async fn get_channel_emotes_from_login<T>(
&'a self,
login: impl types::IntoCow<'a, types::UserNameRef> + 'a,
token: &T,
) -> Result<Option<Vec<helix::chat::ChannelEmote>>, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
if let Some(user) = self
.get_user_from_login(login.to_cow().as_ref(), token)
.await?
{
self.get_channel_emotes_from_id(&user.id, token)
.await
.map(Some)
} else {
Ok(None)
}
}
/// Get emotes in emote set
pub async fn get_emote_sets<T>(
&'a self,
emote_sets: impl AsRef<[&'a types::EmoteSetIdRef]>,
token: &T,
) -> Result<Vec<helix::chat::get_emote_sets::Emote>, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
let req = helix::chat::GetEmoteSetsRequest::emote_set_ids(emote_sets.as_ref());
Ok(self.req_get(req, token).await?.data)
}
/// Get a broadcaster's chat settings
pub async fn get_chat_settings<'b, T>(
&'a self,
broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
moderator_id: impl Into<Option<&'b types::UserIdRef>> + 'b,
token: &T,
) -> Result<helix::chat::ChatSettings, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
let mut req = helix::chat::GetChatSettingsRequest::broadcaster_id(broadcaster_id);
if let Some(moderator_id) = moderator_id.into() {
req = req.moderator_id(moderator_id);
}
Ok(self.req_get(req, token).await?.data)
}
/// Send a chat announcement
pub async fn send_chat_announcement<'b, T, E>(
&'a self,
broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
moderator_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
message: impl Into<&'b str>,
color: impl std::convert::TryInto<helix::chat::AnnouncementColor, Error = E>,
token: &T,
) -> Result<helix::chat::SendChatAnnouncementResponse, ClientExtError<'a, C, E>>
where
T: TwitchToken + ?Sized,
{
let req = helix::chat::SendChatAnnouncementRequest::new(broadcaster_id, moderator_id);
let body = helix::chat::SendChatAnnouncementBody::new(message.into(), color)?;
Ok(self
.req_post(req, body, token)
.await
.map_err(ClientExtError::ClientError)?
.data)
}
/// Delete a specific chat message
pub async fn delete_chat_message<'b, T>(
&'a self,
broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
moderator_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
message_id: impl types::IntoCow<'b, types::MsgIdRef> + 'b,
token: &T,
) -> Result<helix::moderation::DeleteChatMessagesResponse, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
let req = helix::moderation::DeleteChatMessagesRequest::new(broadcaster_id, moderator_id)
.message_id(message_id);
Ok(self.req_delete(req, token).await?.data)
}
/// Delete all chat messages in a broadcasters chat room
pub async fn delete_all_chat_message<'b, T>(
&'a self,
broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
moderator_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &T,
) -> Result<helix::moderation::DeleteChatMessagesResponse, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
let req = helix::moderation::DeleteChatMessagesRequest::new(broadcaster_id, moderator_id);
Ok(self.req_delete(req, token).await?.data)
}
/// Start a raid
pub async fn start_a_raid<'b, T>(
&'a self,
from_broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
to_broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &T,
) -> Result<helix::raids::StartARaidResponse, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
let req = helix::raids::StartARaidRequest::new(from_broadcaster_id, to_broadcaster_id);
Ok(self.req_post(req, helix::EmptyBody, token).await?.data)
}
/// Cancel a raid
pub async fn cancel_a_raid<'b, T>(
&'a self,
broadcaster_id: impl types::IntoCow<'b, types::UserIdRef> + 'b,
token: &T,
) -> Result<helix::raids::CancelARaidResponse, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
let req = helix::raids::CancelARaidRequest::broadcaster_id(broadcaster_id);
Ok(self.req_delete(req, token).await?.data)
}
/// Get a users chat color
pub async fn get_user_chat_color<T>(
&'a self,
user_id: impl Into<&types::UserIdRef>,
token: &T,
) -> Result<Option<helix::chat::UserChatColor>, ClientError<'a, C>>
where
T: TwitchToken + ?Sized,
{
Ok(self
.req_get(
helix::chat::GetUserChatColorRequest::user_ids(&[user_id.into()][..]),
token,
)
.await?
.first())
}
sourceimpl<R, D> Response<R, D>where
R: Request<Response = D> + Clone + Paginated + RequestGet + Debug,
D: DeserializeOwned + Debug + PartialEq,
impl<R, D> Response<R, D>where
R: Request<Response = D> + Clone + Paginated + RequestGet + Debug,
D: DeserializeOwned + Debug + PartialEq,
sourcepub async fn get_next<'a, C: HttpClient<'a>>(
self,
client: &'a HelixClient<'a, C>,
token: &impl TwitchToken + ?Sized
) -> Result<Option<Response<R, D>>, ClientRequestError<<C as HttpClient<'a>>::Error>>
Available on crate feature client
only.
pub async fn get_next<'a, C: HttpClient<'a>>(
self,
client: &'a HelixClient<'a, C>,
token: &impl TwitchToken + ?Sized
) -> Result<Option<Response<R, D>>, ClientRequestError<<C as HttpClient<'a>>::Error>>
client
only.Get the next page in the responses.
Examples found in repository?
src/helix/client/client_ext.rs (line 1056)
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
pub fn make_stream<
'a,
C: crate::HttpClient<'a> + Send + Sync,
T: TwitchToken + ?Sized + Send + Sync,
// FIXME: Why does this have to be clone and debug?
Req: super::Request
+ super::RequestGet
+ super::Paginated
+ Clone
+ std::fmt::Debug
+ Send
+ Sync
+ 'a,
// FIXME: this 'a seems suspicious
Item: Send + 'a,
>(
req: Req,
token: &'a T,
client: &'a super::HelixClient<'a, C>,
fun: impl Fn(<Req as super::Request>::Response) -> std::collections::VecDeque<Item>
+ Send
+ Sync
+ Copy
+ 'static,
) -> std::pin::Pin<Box<dyn futures::Stream<Item = Result<Item, ClientError<'a, C>>> + 'a + Send>>
where
// FIXME: This clone is bad. I want to be able to return the data, but not in a way that limits the response to be Default
// I also want to keep allocations low, so std::mem::take is perfect, but that makes get_next not work optimally.
<Req as super::Request>::Response: Send + Sync + std::fmt::Debug + Clone,
{
use futures::StreamExt;
enum StateMode<Req: super::Request + super::RequestGet, Item> {
/// A request needs to be done.
Req(Option<Req>),
/// We have made a request, now working through the data
Cont(
super::Response<Req, <Req as super::Request>::Response>,
std::collections::VecDeque<Item>,
),
Next(Option<super::Response<Req, <Req as super::Request>::Response>>),
/// The operation failed, allowing no further processing
Failed,
}
impl<Req: super::Request + super::RequestGet, Item> StateMode<Req, Item> {
fn take_initial(&mut self) -> Req {
match self {
StateMode::Req(ref mut r) if r.is_some() => std::mem::take(r).expect("oops"),
_ => todo!("hmmm"),
}
}
fn take_next(&mut self) -> super::Response<Req, <Req as super::Request>::Response> {
match self {
StateMode::Next(ref mut r) if r.is_some() => std::mem::take(r).expect("oops"),
_ => todo!("hmmm"),
}
}
}
struct State<
'a,
C: crate::HttpClient<'a>,
T: TwitchToken + ?Sized,
Req: super::Request + super::RequestGet,
Item,
> {
mode: StateMode<Req, Item>,
client: &'a HelixClient<'a, C>,
token: &'a T,
}
impl<
'a,
C: crate::HttpClient<'a>,
T: TwitchToken + ?Sized,
Req: super::Request + super::RequestGet + super::Paginated,
Item,
> State<'a, C, T, Req, Item>
{
/// Process a request, with a given deq
fn process(
mut self,
r: super::Response<Req, <Req as super::Request>::Response>,
d: std::collections::VecDeque<Item>,
) -> Self {
self.mode = StateMode::Cont(r, d);
self
}
fn failed(mut self) -> Self {
self.mode = StateMode::Failed;
self
}
/// get the next
fn get_next(mut self) -> Self {
match self.mode {
StateMode::Cont(r, d) => {
assert!(d.is_empty());
self.mode = StateMode::Next(Some(r));
self
}
_ => panic!("oops"),
}
}
}
let statemode = StateMode::Req(Some(req));
let state = State {
mode: statemode,
client,
token,
};
futures::stream::unfold(state, move |mut state: State<_, _, _, _>| async move {
match state.mode {
StateMode::Req(Some(_)) => {
let req = state.mode.take_initial();
let f = state.client.req_get(req, state.token);
let resp = match f.await {
Ok(resp) => resp,
Err(e) => return Some((Err(e), state.failed())),
};
let mut deq = fun(resp.data.clone());
deq.pop_front().map(|d| (Ok(d), state.process(resp, deq)))
}
StateMode::Cont(_, ref mut deq) => {
if let Some(d) = deq.pop_front() {
if deq.is_empty() {
Some((Ok(d), state.get_next()))
} else {
Some((Ok(d), state))
}
} else {
// New request returned empty.
None
}
}
StateMode::Next(Some(_)) => {
let resp = state.mode.take_next();
let f = resp.get_next(state.client, state.token);
let resp = match f.await {
Ok(Some(resp)) => resp,
Ok(None) => return None,
Err(e) => return Some((Err(e), state.failed())),
};
let mut deq = fun(resp.data.clone());
deq.pop_front().map(|d| (Ok(d), state.process(resp, deq)))
}
_ => todo!("failed to process request"),
}
})
.boxed()
}
Trait Implementations
sourceimpl<R: Debug, D: Debug> Debug for Response<R, D>where
R: Request,
D: DeserializeOwned + PartialEq,
impl<R: Debug, D: Debug> Debug for Response<R, D>where
R: Request,
D: DeserializeOwned + PartialEq,
sourceimpl<R: PartialEq, D: PartialEq> PartialEq<Response<R, D>> for Response<R, D>where
R: Request,
D: DeserializeOwned + PartialEq,
impl<R: PartialEq, D: PartialEq> PartialEq<Response<R, D>> for Response<R, D>where
R: Request,
D: DeserializeOwned + PartialEq,
impl<R: Eq, D: Eq> Eq for Response<R, D>where
R: Request,
D: DeserializeOwned + PartialEq,
impl<R, D> StructuralEq for Response<R, D>where
R: Request,
D: DeserializeOwned + PartialEq,
impl<R, D> StructuralPartialEq for Response<R, D>where
R: Request,
D: DeserializeOwned + PartialEq,
Auto Trait Implementations
impl<R, D> RefUnwindSafe for Response<R, D>where
D: RefUnwindSafe,
R: RefUnwindSafe,
impl<R, D> Send for Response<R, D>where
D: Send,
R: Send,
impl<R, D> Sync for Response<R, D>where
D: Sync,
R: Sync,
impl<R, D> Unpin for Response<R, D>where
D: Unpin,
R: Unpin,
impl<R, D> UnwindSafe for Response<R, D>where
D: UnwindSafe,
R: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.