feat: init client
This commit is contained in:
40
client/cards.go
Normal file
40
client/cards.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/kratisto/tcgdex-golang/models"
|
||||
)
|
||||
|
||||
// GetCard retrieves a specific card by its ID.
|
||||
// This function is used to get detailed information about a specific card.
|
||||
func (s *tcgDexClient) GetCard(ctx context.Context, id string) (*models.Card, error) {
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, s.baseURL+"/cards/"+id, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
card, err := doRequest[*models.Card](s.httpClient, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return card, nil
|
||||
}
|
||||
|
||||
// Cards retrieves all cards.
|
||||
// This function returns a list of all cards available in the Pokémon TCG database.
|
||||
func (s *tcgDexClient) Cards(ctx context.Context) ([]*models.Card, error) {
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, s.baseURL+"/cards", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cards, err := doRequest[[]*models.Card](s.httpClient, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cards, nil
|
||||
}
|
||||
95
client/client.go
Normal file
95
client/client.go
Normal file
@@ -0,0 +1,95 @@
|
||||
// Package client provides a client for interacting with the Pokémon TCG database.
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/kratisto/tcgdex-golang/models"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultURL = "https://api.tcgdex.net/v2/fr"
|
||||
defaultLanguage = "en"
|
||||
)
|
||||
|
||||
// TCGDexClient is the interface for interacting with the Pokémon TCG database.
|
||||
type TCGDexClient interface {
|
||||
CardsClient
|
||||
SeriesClient
|
||||
SetsClient
|
||||
}
|
||||
|
||||
// CardsClient is the interface for card-related operations.
|
||||
type CardsClient interface {
|
||||
GetCard(ctx context.Context, id string) (*models.Card, error)
|
||||
Cards(ctx context.Context) ([]*models.Card, error)
|
||||
}
|
||||
|
||||
// SeriesClient is the interface for series-related operations.
|
||||
type SeriesClient interface {
|
||||
Series(ctx context.Context) ([]*models.Serie, error)
|
||||
GetSerie(ctx context.Context, seriesID string) (*models.Serie, error)
|
||||
}
|
||||
|
||||
// SetsClient is the interface for set-related operations.
|
||||
type SetsClient interface {
|
||||
Sets(ctx context.Context) ([]*models.Set, error)
|
||||
GetSet(ctx context.Context, setID string) (*models.Set, error)
|
||||
GetCardOfSet(ctx context.Context, setID string, cardID string) (*models.Card, error)
|
||||
}
|
||||
|
||||
type tcgDexClient struct {
|
||||
CardsClient
|
||||
SeriesClient
|
||||
SetsClient
|
||||
|
||||
baseURL string
|
||||
language string
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
// NewClient creates a new TCGDexClient with the default http client.
|
||||
func NewClient(baseURL string, language string) TCGDexClient {
|
||||
return &tcgDexClient{
|
||||
baseURL: baseURL,
|
||||
language: language,
|
||||
httpClient: http.DefaultClient,
|
||||
}
|
||||
}
|
||||
|
||||
// NewClientWithCustomHTTPClient creates a new TCGDexClient with a custom HTTP client.
|
||||
// This allows for more control over the HTTP requests, such as setting timeouts or custom headers.
|
||||
func NewClientWithCustomHTTPClient(url string, language string, client *http.Client) TCGDexClient {
|
||||
return &tcgDexClient{
|
||||
baseURL: url,
|
||||
language: language,
|
||||
httpClient: client,
|
||||
}
|
||||
}
|
||||
|
||||
func doRequest[T any](client *http.Client, request *http.Request) (T, error) {
|
||||
var result T
|
||||
|
||||
resp, err := client.Do(request)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err := resp.Body.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
jsonDecoder := json.NewDecoder(resp.Body)
|
||||
|
||||
err = jsonDecoder.Decode(&result)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
61
client/client_builder.go
Normal file
61
client/client_builder.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package client
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Builder is a builder for creating a TCGDexClient with customizable options.
|
||||
type Builder struct {
|
||||
language *string
|
||||
baseURL *string
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
// NewBuilder creates a new Builder instance.
|
||||
// This allows for fluent configuration of the TCGDexClient.
|
||||
func NewBuilder() *Builder {
|
||||
return &Builder{}
|
||||
}
|
||||
|
||||
// Language sets the language for the TCGDexClient.
|
||||
func (b *Builder) Language(language string) *Builder {
|
||||
b.language = &language
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
// BaseURL sets the base URL for the TCGDexClient.
|
||||
func (b *Builder) BaseURL(baseURL string) *Builder {
|
||||
b.baseURL = &baseURL
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
// HTTPClient sets the custom HTTP client for the TCGDexClient.
|
||||
func (b *Builder) HTTPClient(client *http.Client) *Builder {
|
||||
b.httpClient = client
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
// Builder constructs the TCGDexClient with the specified options.
|
||||
func (b *Builder) Builder() TCGDexClient {
|
||||
httpClient := b.httpClient
|
||||
if httpClient == nil {
|
||||
httpClient = http.DefaultClient
|
||||
}
|
||||
|
||||
language := b.language
|
||||
if language == nil {
|
||||
language = ptr(defaultLanguage)
|
||||
}
|
||||
|
||||
baseURL := b.baseURL
|
||||
if baseURL == nil {
|
||||
baseURL = ptr(defaultURL)
|
||||
}
|
||||
|
||||
return NewClientWithCustomHTTPClient(*baseURL, *language, httpClient)
|
||||
}
|
||||
|
||||
func ptr[T any](s T) *T {
|
||||
return &s
|
||||
}
|
||||
40
client/series.go
Normal file
40
client/series.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/kratisto/tcgdex-golang/models"
|
||||
)
|
||||
|
||||
// Series retrieves all series.
|
||||
// This function returns a list of all series available in the Pokémon TCG database.
|
||||
func (s *tcgDexClient) Series(ctx context.Context) ([]*models.Serie, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.baseURL+"/series", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
series, err := doRequest[[]*models.Serie](s.httpClient, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return series, nil
|
||||
}
|
||||
|
||||
// GetSerie retrieves a specific serie by its ID.
|
||||
// This function is used to get detailed information about a specific series.
|
||||
func (s *tcgDexClient) GetSerie(ctx context.Context, seriesID string) (*models.Serie, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.baseURL+"/series/"+seriesID, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
serie, err := doRequest[*models.Serie](s.httpClient, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return serie, nil
|
||||
}
|
||||
55
client/sets.go
Normal file
55
client/sets.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/kratisto/tcgdex-golang/models"
|
||||
)
|
||||
|
||||
// Sets retrieves all sets.
|
||||
// This function returns a list of all sets available in the Pokémon TCG database.
|
||||
func (s *tcgDexClient) Sets(ctx context.Context) ([]*models.Set, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.baseURL+"/sets", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sets, err := doRequest[[]*models.Set](s.httpClient, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return sets, nil
|
||||
}
|
||||
|
||||
// GetSet retrieves a specific set by its ID.
|
||||
func (s *tcgDexClient) GetSet(ctx context.Context, setID string) (*models.Set, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.baseURL+"/sets/"+setID, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
set, err := doRequest[*models.Set](s.httpClient, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return set, nil
|
||||
}
|
||||
|
||||
// GetCardOfSet retrieves a specific card from a set by its ID.
|
||||
// This function is used to get a card that belongs to a specific set.
|
||||
func (s *tcgDexClient) GetCardOfSet(ctx context.Context, setID string, cardID string) (*models.Card, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.baseURL+"/sets/"+setID+"/cards/"+cardID, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
card, err := doRequest[*models.Card](s.httpClient, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return card, nil
|
||||
}
|
||||
Reference in New Issue
Block a user