-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
55 lines (49 loc) · 1.17 KB
/
client.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
package gotypeform
import (
"fmt"
"io"
"io/ioutil"
"net/http"
)
const (
apiUrl = "https://api.typeform.com"
formsUrl = "/forms"
workspacesUrl = "/workspaces"
accountsUrl = "/accounts"
imagesUrl = "/images"
userUrl = "/me"
themesUrl = "/themes"
)
type Typeform struct {
token string
client *http.Client
}
func TypeformClient(token string) *Typeform {
client := &http.Client{}
return &Typeform{
token: token,
client: client,
}
}
func (tf *Typeform) buildAndExecRequest(method string, url string, body io.Reader) ([]byte, error) {
req, err := http.NewRequest(method, apiUrl+url, body)
req.Header.Add("Accept", "application/json")
bearer := "Bearer " + tf.token
req.Header.Add("Authorization", bearer)
if err != nil {
panic("Error while building Typeform request")
}
resp, err := tf.client.Do(req)
if err != nil {
fmt.Printf("%s", err)
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
return nil, fmt.Errorf("unknown error, status code: %d", resp.StatusCode)
}
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("%s", err)
}
return contents, err
}