Weather app: http requests
This commit is contained in:
34
go-demo-5/geo/geo.go
Normal file
34
go-demo-5/geo/geo.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package geo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type GeoData struct {
|
||||
City string `json:"city"`
|
||||
}
|
||||
|
||||
func GetMyLocation(city string) (*GeoData, error) {
|
||||
if city != "" {
|
||||
return &GeoData{
|
||||
City: city,
|
||||
}, nil
|
||||
}
|
||||
resp, err := http.Get("https://ipapi.co/json/")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, errors.New("NOT200")
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var geo GeoData
|
||||
json.Unmarshal(body, &geo)
|
||||
return &geo, nil
|
||||
}
|
||||
Reference in New Issue
Block a user