2024-03-03 16:47:48 -05:00
package search
import (
"context"
"encoding/json"
2024-03-07 19:41:05 -05:00
"fmt"
2024-03-03 16:47:48 -05:00
"net/http"
"github.com/google/go-querystring/query"
2024-03-07 20:52:42 -05:00
"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 cursor’ s 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 game’ s 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 category’ s 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.
2024-03-07 20:52:42 -05:00
func ( s * Search ) SearchCategories ( ctx context . Context , params * SearchCategoriesParams ) ( * SearchCategoriesResponse , error ) {
2024-03-03 16:47:48 -05:00
v , _ := query . Values ( params )
2024-03-07 20:52:42 -05:00
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
}
2024-03-07 20:52:42 -05:00
res , err := s . client . Do ( req )
2024-03-03 16:47:48 -05:00
if err != nil {
return nil , err
}
defer res . Body . Close ( )
2024-03-07 19:41:05 -05:00
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
}