feat: add missing endpoitn implementation
This commit is contained in:
44
client/types.go
Normal file
44
client/types.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/kratisto/tcgdex-golang/models"
|
||||
)
|
||||
|
||||
// Types retrieves all types.
|
||||
func (s *tcgDexClient) Types(ctx context.Context) ([]string, error) {
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, s.baseURL+"/types", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return doRequestSlice[string](s.httpClient, request)
|
||||
}
|
||||
|
||||
// GetCardsByType retrieves all cards for a given type.
|
||||
func (s *tcgDexClient) GetCardsByType(ctx context.Context, typ string, params *FilterParams) ([]models.Card, error) {
|
||||
endpoint := s.baseURL + "/types/" + typ
|
||||
urlParams := url.Values{}
|
||||
if params != nil {
|
||||
if params.Sort != "" {
|
||||
urlParams.Add("sort", params.Sort)
|
||||
}
|
||||
if params.Page > 0 {
|
||||
urlParams.Add("page", fmt.Sprintf("%d", params.Page))
|
||||
}
|
||||
if params.PageSize > 0 {
|
||||
urlParams.Add("pageSize", fmt.Sprintf("%d", params.PageSize))
|
||||
}
|
||||
}
|
||||
if len(urlParams) > 0 {
|
||||
endpoint += "?" + urlParams.Encode()
|
||||
}
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return doRequestSlice[models.Card](s.httpClient, request)
|
||||
}
|
||||
Reference in New Issue
Block a user