-
Notifications
You must be signed in to change notification settings - Fork 7
/
termii.go
82 lines (71 loc) · 2.03 KB
/
termii.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gotermii
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/pkg/errors"
)
// Config is a representation of config variables
type Config struct {
APIKey string `json:"apiKey"`
BaseURL string `json:"baseURL"`
SenderID string `json:"senderId"`
}
// Client is a representation of a termii client
type Client struct {
config Config
client *http.Client
}
// ConfigFromEnvVars provides the default config from env vars for termii
func ConfigFromEnvVars() Config {
return Config{
APIKey: os.Getenv("TERMII_API_KEY"),
BaseURL: os.Getenv("TERMII_URL"),
SenderID: os.Getenv("TERMII_SENDER_ID"),
}
}
// NewClient creates a termii client using configuration variables
func NewClient() Client {
return Client{config: ConfigFromEnvVars(), client: &http.Client{Timeout: 30 * time.Second}}
}
func (s *Client) makeRequest(method, rURL string, reqBody interface{}, resp interface{}) error {
URL := fmt.Sprintf("%s/%s", s.config.BaseURL, rURL)
var body io.Reader
if reqBody != nil {
bb, err := json.Marshal(reqBody)
if err != nil {
return errors.Wrap(err, "client - unable to marshal request struct")
}
body = bytes.NewReader(bb)
}
req, err := http.NewRequest(method, URL, body)
req.Header.Set("Content-Type", "application/json")
if err != nil {
return errors.Wrap(err, "client - unable to create request body")
}
res, err := s.client.Do(req)
if err != nil {
return errors.Wrap(err, "client - failed to execute request")
}
var bb []byte
if res != nil {
bb, _ = ioutil.ReadAll(res.Body)
}
if os.Getenv("DEBUG_LOGS") == "true" {
log.Printf("got response %s", string(bb))
}
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusNoContent && res.StatusCode != http.StatusCreated {
return errors.Errorf("invalid status code received, expected 200/201/204, got %v, url=%s, with response body=%s",
res.StatusCode, URL, bb)
}
if err := json.Unmarshal(bb, &resp); err != nil {
return errors.Wrap(err, "unable to unmarshal response body")
}
return nil
}