This commit is contained in:
2025-03-10 22:14:46 +02:00
parent bea73f0115
commit 0148583f5b
5 changed files with 144 additions and 6 deletions

20
pokeapi/client.go Normal file
View File

@@ -0,0 +1,20 @@
package pokeapi
import (
"net/http"
"time"
)
// Client -
type Client struct {
httpClient http.Client
}
// NewClient -
func NewClient(timeout time.Duration) Client {
return Client{
httpClient: http.Client{
Timeout: timeout,
},
}
}

42
pokeapi/location_list.go Normal file
View File

@@ -0,0 +1,42 @@
package pokeapi
import (
"encoding/json"
"io"
"net/http"
)
// ListLocations -
func (c *Client) ListLocations(pageURL *string) (RespShallowLocations, error) {
url := baseURL + "/location-area"
if pageURL != nil {
url = *pageURL
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return RespShallowLocations{}, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return RespShallowLocations{}, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
dat, err := io.ReadAll(resp.Body)
if err != nil {
return RespShallowLocations{}, err
}
locationsResp := RespShallowLocations{}
err = json.Unmarshal(dat, &locationsResp)
if err != nil {
return RespShallowLocations{}, err
}
return locationsResp, nil
}

5
pokeapi/pokeapi.go Normal file
View File

@@ -0,0 +1,5 @@
package pokeapi
const (
baseURL = "https://pokeapi.co/api/v2"
)

View File

@@ -0,0 +1,12 @@
package pokeapi
// RespShallowLocations -
type RespShallowLocations struct {
Count int `json:"count"`
Next *string `json:"next"`
Previous *string `json:"previous"`
Results []struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"results"`
}