Compare commits
2 Commits
fbd655b2da
...
7f69e6e1c2
Author | SHA1 | Date | |
---|---|---|---|
7f69e6e1c2 | |||
53a6efa13e |
10
.drone.yml
Normal file
10
.drone.yml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: default
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: greeting
|
||||||
|
image: alpine
|
||||||
|
commands:
|
||||||
|
- echo hello
|
||||||
|
- echo world
|
@ -2,6 +2,7 @@ package weather
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -23,6 +24,10 @@ const (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
EndpointWeather = lib.BuildURI(OpenWeatherMapURI, "/data/2.5/weather")
|
EndpointWeather = lib.BuildURI(OpenWeatherMapURI, "/data/2.5/weather")
|
||||||
|
ErrUnmarshal = errors.New("unmarshaling JSON failed")
|
||||||
|
ErrReadingResponse = errors.New("reading HTTP response failed")
|
||||||
|
ErrRequestFailed = errors.New("HTTP request failed")
|
||||||
|
ErrCreateRequestFailed = errors.New("failed to create new HTTP request")
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewClient(token string) *WeatherClient {
|
func NewClient(token string) *WeatherClient {
|
||||||
@ -30,13 +35,11 @@ func NewClient(token string) *WeatherClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *WeatherClient) Get(loc string) (w Weather, err error) {
|
func (c *WeatherClient) Get(loc string) (w Weather, err error) {
|
||||||
var (
|
var werr WeatherError
|
||||||
werr WeatherError
|
|
||||||
)
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", EndpointWeather, nil)
|
req, err := http.NewRequest("GET", EndpointWeather, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("failed to create new request: %s", err)
|
err = fmt.Errorf("%s: %s", ErrCreateRequestFailed, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,22 +50,22 @@ func (c *WeatherClient) Get(loc string) (w Weather, err error) {
|
|||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("HTTP request failed: %s", err)
|
err = fmt.Errorf("%s: %s", ErrRequestFailed, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("reading HTTP response failed: %s", err)
|
err = fmt.Errorf("%s: %s", ErrReadingResponse, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != http.StatusOK {
|
||||||
err = json.Unmarshal(body, &werr)
|
err = json.Unmarshal(body, &werr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("%s", body)
|
log.Debugf("%s", body)
|
||||||
err = fmt.Errorf("unmarshaling JSON failed: %s", err)
|
err = fmt.Errorf("%s: %s", ErrUnmarshal, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +76,7 @@ func (c *WeatherClient) Get(loc string) (w Weather, err error) {
|
|||||||
err = json.Unmarshal(body, &w)
|
err = json.Unmarshal(body, &w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("%s", body)
|
log.Debugf("%s", body)
|
||||||
log.Errorf("unmarshaling JSON failed: %s", err)
|
err = fmt.Errorf("%s: %s", ErrUnmarshal, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user