Skip to content

Conversation

jatin-encrypted
Copy link

This PR fixes the parsing of Twitch Helix API endpoints that return only a single item in the data array. Instead of parsing responses into an intermediate Vec<T> and then extracting the first element with .into_iter().next(), we now parse directly into [T; 1] (a fixed-size array) or use the parse_single_return helper function where appropriate.

Fixes #504

Updated Files:
src/helix/endpoints/channels/get_ad_schedule.rs
src/helix/endpoints/channels/start_commercial.rs
src/helix/endpoints/charity/get_charity_campaign.rs
src/helix/endpoints/chat/update_chat_settings.rs
src/helix/endpoints/eventsub/create_eventsub_subscription.rs
src/helix/endpoints/moderation/ban_user.rs
src/helix/endpoints/moderation/get_shield_mode_status.rs
src/helix/endpoints/moderation/update_shield_mode_status.rs
src/helix/endpoints/moderation/warn_chat_user.rs
src/helix/endpoints/points/create_custom_rewards.rs
src/helix/endpoints/points/update_custom_reward.rs
src/helix/endpoints/points/update_redemption_status.rs
src/helix/endpoints/polls/create_poll.rs
src/helix/endpoints/polls/end_poll.rs
src/helix/endpoints/predictions/create_prediction.rs
src/helix/endpoints/predictions/end_prediction.rs
src/helix/endpoints/raids/start_a_raid.rs
src/helix/endpoints/subscriptions/check_user_subscription.rs

@jatin-encrypted
Copy link
Author

I am encountering these test failures, the changes that i have done are running fine on local machine but i am not able to understand why is this failing. I would appreciate a lot if anyone could suggest what changes can be done?

Copy link
Contributor

@Nerixyz Nerixyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR, just three small things. The rest is just things I noticed that already existed before your PR.

I'll keep in mind to check for the get-ad-schedule response.

Comment on lines -135 to -142
struct IsWrong {
snooze_count: i32,
snooze_refresh_at: i32,
next_ad_at: i32,
duration: i32,
last_ad_at: i32,
preroll_free_time: i32,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is still returned. We should check that this isn't returned anymore by Twitch.

Copy link
Contributor

@Nerixyz Nerixyz Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Twitch can still return numbers here (https://redirect.github.com/twitchdev/issues/issues/857):

> twitch api get channels/ads -q broadcaster_id=129546453
{
  "data": [
    {
      "duration": 0,
      "last_ad_at": 0,
      "next_ad_at": 0,
      "preroll_free_time": 0,
      "snooze_count": 3,
      "snooze_refresh_at": 0
    }
  ]
}

Undo this change for now.

Comment on lines 198 to 208
let resp: helix::InnerResponse<[ChatSettings; 1]> = parse_json(response, true)
.map_err(|e| {
HelixRequestPatchError::DeserializeError(
response.to_string(),
e,
uri.clone(),
status,
)
})?;
resp.data.into_iter().next().ok_or(
helix::HelixRequestPatchError::InvalidResponse {
reason: "expected at least one element in data",
response: response.to_string(),
status,
uri: uri.clone(),
},
)?
let [data] = resp.data;
data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use parse_single_return too, no? It checks for the status as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I will change it.

@Emilgardis
Copy link
Member

please remove the merge commit :)

@jatin-encrypted
Copy link
Author

I have added a commit with the updated changes, Please review them and give your suggestions, if any!!

@Emilgardis
Copy link
Member

need to fix the tests, they are failing due to a merge you did.

also please remove the merge commits, you should be able to find some guides online or you can ask for more precise help. Specifically what I think you should need to do is

git pull upstream main --rebase -X theirs

and then fix whatever issues arise from that, could also remove -X theirs but most changes here should be that easy

I'd also like it if you squashed the commits afterwards :)

Comment on lines -135 to -142
struct IsWrong {
snooze_count: i32,
snooze_refresh_at: i32,
next_ad_at: i32,
duration: i32,
last_ad_at: i32,
preroll_free_time: i32,
}
Copy link
Contributor

@Nerixyz Nerixyz Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Twitch can still return numbers here (https://redirect.github.com/twitchdev/issues/issues/857):

> twitch api get channels/ads -q broadcaster_id=129546453
{
  "data": [
    {
      "duration": 0,
      "last_ad_at": 0,
      "next_ad_at": 0,
      "preroll_free_time": 0,
      "snooze_count": 3,
      "snooze_refresh_at": 0
    }
  ]
}

Undo this change for now.

@Nerixyz
Copy link
Contributor

Nerixyz commented Oct 15, 2025

🤔 we could also use GitHub's squash merge. There, merges get squashed away

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parse responses with one item using parse_single_return

3 participants