go-twitch/api/search/search_categories.go

80 lines
2.7 KiB
Go
Raw Permalink Normal View History

2024-03-03 16:47:48 -05:00
package search
import (
"context"
"encoding/json"
"fmt"
2024-03-03 16:47:48 -05:00
"net/http"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
2024-03-03 16:47:48 -05:00
"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 (s *Search) SearchCategories(ctx context.Context, params *SearchCategoriesParams) (*SearchCategoriesResponse, error) {
2024-03-03 16:47:48 -05:00
v, _ := query.Values(params)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(s.baseUrl, "search/categories", v), nil)
2024-03-03 16:47:48 -05:00
if err != nil {
return nil, err
}
res, err := s.client.Do(req)
2024-03-03 16:47:48 -05:00
if err != nil {
return nil, err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("search categories request failed: %s", res.Status)
}
2024-03-03 16:47:48 -05:00
var data SearchCategoriesResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}