chore: Refactor IPAPI lookup function for error handling and test coverage
This commit is contained in:
@@ -63,21 +63,21 @@ func buildURL(ip string) (string, error) {
|
|||||||
// https://ip-api.com/docs/api:json
|
// https://ip-api.com/docs/api:json
|
||||||
func Lookup(ip string) (data IPAPI, err error) {
|
func Lookup(ip string) (data IPAPI, err error) {
|
||||||
|
|
||||||
url, error := buildURL(ip)
|
url, err := buildURL(ip)
|
||||||
if error != nil {
|
if err != nil {
|
||||||
return data, error
|
return data, err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if error != nil {
|
if err != nil {
|
||||||
return data, error
|
return data, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
err = json.NewDecoder(resp.Body).Decode(&data)
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
||||||
if error != nil {
|
if err != nil {
|
||||||
return data, error
|
return data, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if data.Status == "fail" {
|
if data.Status == "fail" {
|
||||||
|
|||||||
Reference in New Issue
Block a user