forked from alex-rufo/kiwi-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kiwi.go
39 lines (31 loc) · 777 Bytes
/
kiwi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package kiwi
import (
"encoding/json"
"errors"
"net/http"
)
const apiURL = "https://api.skypicker.com"
var (
// ErrorStatusCodeNotOK when the status code is not 200
ErrorStatusCodeNotOK = errors.New("Status code not 200")
)
// Kiwi structure that handles the different calls to the API
type Kiwi struct {
endpoint string
}
// New creates a new Kiwi structure with the default endpoint
func New() *Kiwi {
return &Kiwi{endpoint: apiURL}
}
// Call executed an API call to the kiwi endpoint
func (k *Kiwi) call(path string, v interface{}) error {
resp, err := http.Get(k.endpoint + path)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return ErrorStatusCodeNotOK
}
return json.NewDecoder(resp.Body).Decode(v)
}