This commit is contained in:
49
pokeapi/location_get.go
Normal file
49
pokeapi/location_get.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package pokeapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// GetLocation -
|
||||
func (c *Client) GetLocation(locationName string) (Location, error) {
|
||||
url := baseURL + "/location-area/" + locationName
|
||||
|
||||
if val, ok := c.cache.Get(url); ok {
|
||||
locationResp := Location{}
|
||||
err := json.Unmarshal(val, &locationResp)
|
||||
if err != nil {
|
||||
return Location{}, err
|
||||
}
|
||||
return locationResp, nil
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return Location{}, err
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return Location{}, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
dat, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return Location{}, err
|
||||
}
|
||||
|
||||
locationResp := Location{}
|
||||
err = json.Unmarshal(dat, &locationResp)
|
||||
if err != nil {
|
||||
return Location{}, err
|
||||
}
|
||||
|
||||
c.cache.Add(url, dat)
|
||||
|
||||
return locationResp, nil
|
||||
}
|
||||
@@ -10,3 +10,57 @@ type RespShallowLocations struct {
|
||||
URL string `json:"url"`
|
||||
} `json:"results"`
|
||||
}
|
||||
|
||||
// Location -
|
||||
type Location struct {
|
||||
EncounterMethodRates []struct {
|
||||
EncounterMethod struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"encounter_method"`
|
||||
VersionDetails []struct {
|
||||
Rate int `json:"rate"`
|
||||
Version struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"version"`
|
||||
} `json:"version_details"`
|
||||
} `json:"encounter_method_rates"`
|
||||
GameIndex int `json:"game_index"`
|
||||
ID int `json:"id"`
|
||||
Location struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"location"`
|
||||
Name string `json:"name"`
|
||||
Names []struct {
|
||||
Language struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"language"`
|
||||
Name string `json:"name"`
|
||||
} `json:"names"`
|
||||
PokemonEncounters []struct {
|
||||
Pokemon struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"pokemon"`
|
||||
VersionDetails []struct {
|
||||
EncounterDetails []struct {
|
||||
Chance int `json:"chance"`
|
||||
ConditionValues []interface{} `json:"condition_values"`
|
||||
MaxLevel int `json:"max_level"`
|
||||
Method struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"method"`
|
||||
MinLevel int `json:"min_level"`
|
||||
} `json:"encounter_details"`
|
||||
MaxChance int `json:"max_chance"`
|
||||
Version struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"version"`
|
||||
} `json:"version_details"`
|
||||
} `json:"pokemon_encounters"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user