use super::*;
use helix::RequestGet;
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[non_exhaustive]
pub struct GetStreamKeyRequest<'a> {
#[cfg_attr(feature = "typed-builder", builder(setter(into)))]
#[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
pub broadcaster_id: Cow<'a, types::UserIdRef>,
}
impl<'a> GetStreamKeyRequest<'a> {
pub fn broadcaster_id(broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a) -> Self {
Self {
broadcaster_id: broadcaster_id.into_cow(),
}
}
}
#[derive(PartialEq, Eq, Deserialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct GetStreamKeyResponse {
pub stream_key: types::StreamKey,
}
impl Request for GetStreamKeyRequest<'_> {
type Response = GetStreamKeyResponse;
const PATH: &'static str = "streams/key";
#[cfg(feature = "twitch_oauth2")]
const SCOPE: twitch_oauth2::Validator =
twitch_oauth2::validator![twitch_oauth2::Scope::ChannelReadStreamKey];
}
impl RequestGet for GetStreamKeyRequest<'_> {
fn parse_inner_response(
request: Option<Self>,
uri: &http::Uri,
response: &str,
status: http::StatusCode,
) -> Result<helix::Response<Self, <Self as Request>::Response>, helix::HelixRequestGetError>
where
Self: Sized,
{
helix::parse_single_return(request, uri, response, status)
}
}
#[cfg(test)]
#[test]
fn test_request() {
use helix::*;
let req = GetStreamKeyRequest::broadcaster_id("198704263");
let data = br#"
{
"data": [
{
"stream_key": "live_44322889_a34ub37c8ajv98a0"
}
]
}
"#
.to_vec();
let http_response = http::Response::builder().body(data).unwrap();
let uri = req.get_uri().unwrap();
assert_eq!(
uri.to_string(),
"https://api.twitch.tv/helix/streams/key?broadcaster_id=198704263"
);
let res = GetStreamKeyRequest::parse_response(Some(req), &uri, http_response)
.unwrap()
.data;
assert_eq!(res.stream_key.as_str(), "live_44322889_a34ub37c8ajv98a0");
}