github.com/topi314/pogo-icons@v0.0.0-20250403082451-64aa0bf0322a/internal/pokeapi/api.go (about)

     1  package pokeapi
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  	"time"
     9  )
    10  
    11  func NewAPI(endpoint string) Client {
    12  	return &clientAPI{
    13  		client: &http.Client{
    14  			Timeout: 10 * time.Second,
    15  		},
    16  		endpoint: endpoint,
    17  	}
    18  }
    19  
    20  type clientAPI struct {
    21  	client       *http.Client
    22  	endpoint     string
    23  	pokemonForms []PokemonForm
    24  }
    25  
    26  func (c *clientAPI) GetPokemon(ctx context.Context) ([]PokemonForm, error) {
    27  	if c.pokemonForms != nil {
    28  		return c.pokemonForms, nil
    29  	}
    30  
    31  	url := c.endpoint + "/pokemon"
    32  
    33  	var allPokemonForms []PokemonForm
    34  	for {
    35  		p, err := c.getPokemonPage(ctx, url)
    36  		if err != nil {
    37  			return nil, err
    38  		}
    39  
    40  		for _, result := range p.Results {
    41  			pokemonForm := newPokemonForm(result)
    42  			allPokemonForms = append(allPokemonForms, pokemonForm)
    43  		}
    44  
    45  		if p.Next == "" {
    46  			break
    47  		}
    48  
    49  		url = p.Next
    50  	}
    51  
    52  	c.pokemonForms = allPokemonForms
    53  	return allPokemonForms, nil
    54  }
    55  
    56  func (c *clientAPI) getPokemonPage(ctx context.Context, url string) (Page[Pokemon], error) {
    57  	rq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
    58  	if err != nil {
    59  		return Page[Pokemon]{}, fmt.Errorf("error creating request: %w", err)
    60  	}
    61  	rs, err := c.client.Do(rq)
    62  	if err != nil {
    63  		return Page[Pokemon]{}, fmt.Errorf("error executing request: %w", err)
    64  	}
    65  	defer rs.Body.Close()
    66  
    67  	var p Page[Pokemon]
    68  	if err = json.NewDecoder(rs.Body).Decode(&p); err != nil {
    69  		return Page[Pokemon]{}, fmt.Errorf("error decoding response: %w", err)
    70  	}
    71  
    72  	return p, nil
    73  }
    74  
    75  func (c *clientAPI) GetPokemonForm(ctx context.Context, name string) (PokemonForm, error) {
    76  	rq, err := http.NewRequestWithContext(ctx, http.MethodGet, c.endpoint+"/pokemon/"+name, nil)
    77  	if err != nil {
    78  		return PokemonForm{}, fmt.Errorf("error creating request: %w", err)
    79  	}
    80  
    81  	rs, err := c.client.Do(rq)
    82  	if err != nil {
    83  		return PokemonForm{}, fmt.Errorf("error executing request: %w", err)
    84  	}
    85  	defer rs.Body.Close()
    86  
    87  	if rs.StatusCode == http.StatusNotFound {
    88  		return PokemonForm{}, fmt.Errorf("failed to find pokemon: %w", ErrNotFound)
    89  	}
    90  
    91  	if rs.StatusCode != http.StatusOK {
    92  		return PokemonForm{}, fmt.Errorf("error fetching pokemon: %w", fmt.Errorf("unexpected status code: %d", rs.StatusCode))
    93  	}
    94  
    95  	var p Pokemon
    96  	if err = json.NewDecoder(rs.Body).Decode(&p); err != nil {
    97  		return PokemonForm{}, fmt.Errorf("error decoding response: %w", err)
    98  	}
    99  
   100  	return newPokemonForm(p), nil
   101  }
   102  
   103  func (c *clientAPI) GetSprite(ctx context.Context, url string) (*http.Response, error) {
   104  	rq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
   105  	if err != nil {
   106  		return nil, fmt.Errorf("error creating sprite request: %w", err)
   107  	}
   108  
   109  	rs, err := c.client.Do(rq)
   110  	if err != nil {
   111  		return nil, fmt.Errorf("error executing sprite request: %w", err)
   112  	}
   113  
   114  	return rs, nil
   115  }