-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers.go
56 lines (49 loc) · 1.15 KB
/
helpers.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
package main
import (
"os"
"time"
"bytes"
"io"
"strings"
"log"
"net/http"
"mime/multipart"
"encoding/json"
)
func LookupStrEnv(name string, dflt string) (string) {
strEnvStr, ok := os.LookupEnv(name)
if ok == false {
return dflt
}
return strEnvStr
}
func call(apiKey string, urlPath string) (ApiResponse, error) {
client := &http.Client{
Timeout: time.Second * 10,
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
fw, err := writer.CreateFormField("api_key")
if err != nil {
}
_, err = io.Copy(fw, strings.NewReader(apiKey))
if err != nil {
return ApiResponse{}, err
}
writer.Close()
req, err := http.NewRequest("POST", urlPath, bytes.NewReader(body.Bytes()))
if err != nil {
return ApiResponse{}, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, _ := client.Do(req)
if resp.StatusCode != http.StatusOK {
log.Printf("Request failed with response code: %d", resp.StatusCode)
}
defer resp.Body.Close()
var response ApiResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
log.Fatal(err)
}
return response, nil
}