Compare commits

..

No commits in common. "7f69e6e1c2b06e9ca3e32b808d24fd1ce1a042a7" and "fbd655b2da0de9fc952d008076c07ae7f5d72bdc" have entirely different histories.

2 changed files with 10 additions and 23 deletions

View File

@ -1,10 +0,0 @@
kind: pipeline
type: docker
name: default
steps:
- name: greeting
image: alpine
commands:
- echo hello
- echo world

View File

@ -2,7 +2,6 @@ package weather
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -23,11 +22,7 @@ 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 {
@ -35,11 +30,13 @@ 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 werr WeatherError var (
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("%s: %s", ErrCreateRequestFailed, err) err = fmt.Errorf("failed to create new request: %s", err)
return return
} }
@ -50,22 +47,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("%s: %s", ErrRequestFailed, err) err = fmt.Errorf("HTTP request failed: %s", 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("%s: %s", ErrReadingResponse, err) err = fmt.Errorf("reading HTTP response failed: %s", err)
return return
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != 200 {
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("%s: %s", ErrUnmarshal, err) err = fmt.Errorf("unmarshaling JSON failed: %s", err)
return return
} }
@ -76,7 +73,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)
err = fmt.Errorf("%s: %s", ErrUnmarshal, err) log.Errorf("unmarshaling JSON failed: %s", err)
return return
} }