Add Goal endpoints to API

This commit is contained in:
Evan Fiordeliso 2024-03-02 23:04:11 -05:00
parent 10cf0e7456
commit 10a15645b7
3 changed files with 125 additions and 0 deletions

View File

@ -17,6 +17,7 @@ import (
"go.fifitido.net/twitch/api/eventsub"
"go.fifitido.net/twitch/api/extensions"
"go.fifitido.net/twitch/api/games"
"go.fifitido.net/twitch/api/goals"
)
const HelixBaseUrl = "https://api.twitch.tv/helix"
@ -38,6 +39,7 @@ type API struct {
Extensions *extensions.Extensions
EventSub *eventsub.EventSub
Games *games.Games
Goals *goals.Goals
}
func New() *API {
@ -61,5 +63,6 @@ func New() *API {
Extensions: extensions.New(client, baseUrl),
EventSub: eventsub.New(client, baseUrl),
Games: games.New(client, baseUrl),
Goals: goals.New(client, baseUrl),
}
}

View File

@ -0,0 +1,104 @@
package goals
import (
"context"
"encoding/json"
"net/http"
"net/url"
"time"
)
type GetCreatorGoalsResponse struct {
Data []Goal `json:"data"`
}
type Goal struct {
// An ID that identifies this goal.
Id string `json:"id"`
// An ID that identifies the broadcaster that created the goal.
BroadcasterId string `json:"broadcaster_id"`
// The broadcasters display name.
BroadcasterName string `json:"broadcaster_name"`
// The broadcasters login name.
BroadcasterLogin string `json:"broadcaster_login"`
// The type of goal. Possible values are:
//
// follower — The goal is to increase followers.
//
// subscription — The goal is to increase subscriptions.
// This type shows the net increase or decrease in tier points associated with the subscriptions.
//
// subscription_count — The goal is to increase subscriptions.
// This type shows the net increase or decrease in the number of subscriptions.
//
// new_subscription — The goal is to increase subscriptions.
// This type shows only the net increase in tier points associated with the subscriptions
// (it does not account for users that unsubscribed since the goal started).
//
// new_subscription_count — The goal is to increase subscriptions.
// This type shows only the net increase in the number of subscriptions
// (it does not account for users that unsubscribed since the goal started).
Type string `json:"type"`
// A description of the goal. Is an empty string if not specified.
Description string `json:"description"`
// The goals current value.
//
// The goals type determines how this value is increased or decreased.
//
// If type is follower, this field is set to the broadcasters current number of followers.
// This number increases with new followers and decreases when users unfollow the broadcaster.
//
// If type is subscription, this field is increased and decreased by the points value associated with the subscription tier.
// For example, if a tier-two subscription is worth 2 points, this field is increased or decreased by 2, not 1.
//
// If type is subscription_count, this field is increased by 1 for each new subscription and decreased by 1 for each user that unsubscribes.
//
// If type is new_subscription, this field is increased by the points value associated with the subscription tier.
// For example, if a tier-two subscription is worth 2 points, this field is increased by 2, not 1.
//
// If type is new_subscription_count, this field is increased by 1 for each new subscription.
CurrentAmount int `json:"current_amount"`
// The goals target value.
//
// For example, if the broadcaster has 200 followers before creating the goal,
// and their goal is to double that number, this field is set to 400.
TargetAmount int `json:"target_amount"`
// The UTC date and time (in RFC3339 format) that the broadcaster created the goal.
CreatedAt time.Time `json:"created_at"`
}
// Gets the broadcasters list of active goals. Use this endpoint to get the current progress of each goal.
//
// Instead of polling for the progress of a goal, consider subscribing to receive notifications when a goal makes progress
// using the channel.goal.progress subscription type. Read More: https://dev.twitch.tv/docs/api/goals#requesting-event-notifications
//
// Requires a user access token that includes the channel:read:goals scope.
func (c *Goals) GetCreatorGoals(ctx context.Context, broadcasterID string) (*GetCreatorGoalsResponse, error) {
endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "goals", RawQuery: "broadcaster_id=" + broadcasterID})
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var data GetCreatorGoalsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}

18
api/goals/goals.go Normal file
View File

@ -0,0 +1,18 @@
package goals
import (
"net/http"
"net/url"
)
type Goals struct {
client *http.Client
baseUrl *url.URL
}
func New(client *http.Client, baseUrl *url.URL) *Goals {
return &Goals{
client: client,
baseUrl: baseUrl,
}
}