Add Search endpoints to API

This commit is contained in:
Evan Fiordeliso 2024-03-03 16:47:48 -05:00
parent 4063c82e6a
commit f068cfdd73
4 changed files with 203 additions and 0 deletions

View File

@ -25,6 +25,7 @@ import (
"go.fifitido.net/twitch/api/predictions"
"go.fifitido.net/twitch/api/raids"
"go.fifitido.net/twitch/api/schedule"
"go.fifitido.net/twitch/api/search"
)
const HelixBaseUrl = "https://api.twitch.tv/helix"
@ -54,6 +55,7 @@ type API struct {
Predictions *predictions.Predictions
Raids *raids.Raids
Schedule *schedule.Schedule
Search *search.Search
}
func NewWithClient(client *http.Client) *API {
@ -84,6 +86,7 @@ func NewWithClient(client *http.Client) *API {
Predictions: predictions.New(client, baseUrl),
Raids: raids.New(client, baseUrl),
Schedule: schedule.New(client, baseUrl),
Search: search.New(client, baseUrl),
}
}

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

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

View File

@ -0,0 +1,74 @@
package search
import (
"context"
"encoding/json"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/types"
)
type SearchCategoriesParams struct {
// The URI-encoded search string. For example, encode #archery as %23archery and search strings like angel of death as angel%20of%20death.
Query string `url:"query,omitempty"`
// 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 items per page.
// 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 cursors value.
// Read More: https://dev.twitch.tv/docs/api/guide#pagination
After string `url:"after,omitempty"`
}
type SearchCategoriesResponse struct {
// The list of games or categories that match the query. The list is empty if there are no matches.
Data []struct {
// A URL to an image of the games box art or streaming category.
BoxArtURL string `json:"box_art_url"`
// The name of the game or category.
Name string `json:"name"`
// An ID that uniquely identifies the game or category.
ID string `json:"id"`
} `json:"data"`
// A pagination object that contains the cursor to use to get the next page 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"`
}
// Gets the games or categories that match the specified query.
//
// To match, the categorys name must contain all parts of the query string.
// For example, if the query string is 42, the response includes any category name that contains 42 in the title.
// If the query string is a phrase like love computer, the response includes any category name that contains the words love and computer anywhere in the name.
// The comparison is case insensitive.
//
// Requires an app access token or user access token.
func (c *Search) SearchCategories(ctx context.Context, params *SearchCategoriesParams) (*SearchCategoriesResponse, error) {
v, _ := query.Values(params)
endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "search/categories", RawQuery: v.Encode()})
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 SearchCategoriesResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -0,0 +1,108 @@
package search
import (
"context"
"encoding/json"
"net/http"
"net/url"
"time"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/types"
)
type SearchChannelsParams struct {
// The URI-encoded search string. For example, encode #archery as %23archery and search strings like angel of death as angel%20of%20death.
Query string `url:"query,omitempty"`
// A Boolean value that determines whether the response includes only channels that are currently streaming live.
// Set to true to get only channels that are streaming live; otherwise, false to get live and offline channels.
// The default is false.
LiveOnly *bool `url:"live_only,omitempty"`
// 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 items per page.
// 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 cursors value.
// Read More: https://dev.twitch.tv/docs/api/guide#pagination
After *types.Cursor `url:"after,omitempty"`
}
type SearchChannelsResponse struct {
// The list of channels that match the query. The list is empty if there are no matches.
Data []struct {
// The ISO 639-1 two-letter language code of the language used by the broadcaster.
// For example, en for English. If the broadcaster uses a language not in the list of supported stream languages, the value is other.
BroadcasterLanguage string `json:"broadcaster_language"`
// The broadcasters login name.
BroadcasterLogin string `json:"broadcaster_login"`
// The broadcasters display name.
DisplayName string `json:"display_name"`
// The ID of the game that the broadcaster is playing or last played.
GameID string `json:"game_id"`
// The name of the game that the broadcaster is playing or last played.
GameName string `json:"game_name"`
// An ID that uniquely identifies the channel (this is the broadcasters ID).
ID string `json:"id"`
// A Boolean value that determines whether the broadcaster is streaming live. Is true if the broadcaster is streaming live; otherwise, false.
IsLive bool `json:"is_live"`
// The tags applied to the channel.
Tags []string `json:"tags"`
// A URL to a thumbnail of the broadcasters profile image.
ThumbnailURL string `json:"thumbnail_url"`
// The streams title. Is an empty string if the broadcaster didnt set it.
Title string `json:"title"`
// The UTC date and time (in RFC3339 format) of when the broadcaster started streaming.
// The string is empty if the broadcaster is not streaming live.
StartedAt *time.Time `json:"started_at"`
} `json:"data"`
}
// Gets the channels that match the specified query and have streamed content within the past 6 months.
//
// The fields that the API uses for comparison depends on the value that the live_only query parameter is set to.
// If live_only is false, the API matches on the broadcasters login name.
// However, if live_only is true, the API matches on the broadcasters name and category name.
//
// To match, the beginning of the broadcasters name or category must match the query string. The comparison is case insensitive.
// If the query string is angel_of_death, it matches all names that begin with angel_of_death.
// However, if the query string is a phrase like angel of death, it matches to names starting with angelofdeath or names starting with angel_of_death.
//
// By default, the results include both live and offline channels. To get only live channels set the live_only query parameter to true.
//
// Requires an app access token or user access token.
func (c *Search) SearchChannels(ctx context.Context, params *SearchChannelsParams) (*SearchChannelsResponse, error) {
v, _ := query.Values(params)
endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "search/channels", RawQuery: v.Encode()})
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 SearchChannelsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}