2024-02-27 22:26:57 -05:00
|
|
|
|
package charity
|
|
|
|
|
|
|
|
|
|
import (
|
2024-02-27 23:03:40 -05:00
|
|
|
|
"context"
|
2024-02-27 22:26:57 -05:00
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-02-27 23:03:40 -05:00
|
|
|
|
"net/http"
|
2024-02-27 22:26:57 -05:00
|
|
|
|
|
|
|
|
|
"github.com/google/go-querystring/query"
|
2024-03-07 20:52:42 -05:00
|
|
|
|
"go.fifitido.net/twitch/api/endpoint"
|
2024-02-27 22:26:57 -05:00
|
|
|
|
"go.fifitido.net/twitch/api/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type GetCharityCampaignDonationsParams struct {
|
|
|
|
|
// The ID of the broadcaster that’s currently running a charity campaign. This ID must match the user ID in the access token.
|
|
|
|
|
BroadcasterID string `json:"broadcaster_id"`
|
|
|
|
|
|
|
|
|
|
// The maximum number of items to return per page in the response. The minimum page size is 1 item per page and the maximum is 100. The default is 20.
|
|
|
|
|
First *int `url:"first,omitempty"`
|
|
|
|
|
|
|
|
|
|
// The cursor used to get the next page of results. The Pagination object in the response contains the cursor’s value.
|
|
|
|
|
// Read more: https://dev.twitch.tv/docs/api/guide#pagination
|
|
|
|
|
After *types.Cursor `url:"after,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GetCharityCampaignDonationsResponse struct {
|
|
|
|
|
// A list that contains the donations that users have made to the broadcaster’s charity campaign.
|
|
|
|
|
// The list is empty if the broadcaster is not currently running a charity campaign.
|
|
|
|
|
// The donation information is not available after the campaign ends.
|
|
|
|
|
Data []CharityCampaignDonation `json:"data"`
|
|
|
|
|
|
|
|
|
|
// Contains the information used to page through the list of results. The object is empty if there are no more pages left to page through.
|
|
|
|
|
// Read more: https://dev.twitch.tv/docs/api/guide#pagination
|
|
|
|
|
Pagination types.Pagination `json:"pagination"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type CharityCampaignDonation struct {
|
|
|
|
|
// An ID that identifies the donation. The ID is unique across campaigns
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
|
|
|
|
|
// An ID that identifies the charity campaign that the donation applies to.
|
|
|
|
|
CampaignID string `json:"campaign_id"`
|
|
|
|
|
|
|
|
|
|
// An ID that identifies a user that donated money to the campaign.
|
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
|
|
|
|
|
|
// The user’s login name.
|
|
|
|
|
UserLogin string `json:"user_login"`
|
|
|
|
|
|
|
|
|
|
// The user’s display name.
|
|
|
|
|
UserName string `json:"user_name"`
|
|
|
|
|
|
|
|
|
|
// An object that contains the amount of money that the user donated.
|
|
|
|
|
Amount types.CurrencyAmount `json:"amount"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Gets the list of donations that users have made to the broadcaster’s active charity campaign.
|
|
|
|
|
//
|
|
|
|
|
// To receive events as donations occur, subscribe to the channel.charity_campaign.donate subscription type.
|
|
|
|
|
//
|
|
|
|
|
// Requires a user access token that includes the channel:read:charity scope.
|
2024-02-27 23:03:40 -05:00
|
|
|
|
func (c *Charity) GetCharityCampaignDonations(ctx context.Context, params *GetCharityCampaignDonationsParams) (*GetCharityCampaignDonationsResponse, error) {
|
2024-02-27 22:26:57 -05:00
|
|
|
|
v, _ := query.Values(params)
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(c.baseUrl, "charity/campaigns/donations", v), nil)
|
2024-02-27 23:03:40 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := c.client.Do(req)
|
2024-02-27 22:26:57 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-03-07 19:41:05 -05:00
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
|
|
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
|
|
|
|
if !statusOK {
|
|
|
|
|
return nil, fmt.Errorf("failed to get charity campaign donations (%d)", res.StatusCode)
|
|
|
|
|
}
|
2024-02-27 22:26:57 -05:00
|
|
|
|
|
|
|
|
|
var respBody GetCharityCampaignDonationsResponse
|
2024-02-27 23:03:40 -05:00
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&respBody); err != nil {
|
2024-02-27 22:26:57 -05:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &respBody, nil
|
|
|
|
|
}
|