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"
"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 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.
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 ( )
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
}