forked from ajph/intercom-go
/
tag.go
120 lines (98 loc) · 2.78 KB
/
tag.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package intercom
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"strconv"
) //import
const (
TAG_DELETE_API_ENDPOINT string = "https://api.intercom.io/tags/"
TAG_GET_API_ENDPOINT string = "https://api.intercom.io/tags"
TAG_POST_API_ENDPOINT string = "https://api.intercom.io/tags"
) //const
func (this *Intercom_t) DeleteTag(id uint) (err error) {
var (
req *http.Request
resp *http.Response
client = new(http.Client)
) //var
// Create new DELETE request. Add Tag ID to end of URL
if req, err = http.NewRequest("DELETE", TAG_DELETE_API_ENDPOINT+strconv.Itoa(int(id)), nil); err != nil {
return err
} //if
// Set authentication and headers
req.SetBasicAuth(this.AppId, this.ApiKey)
req.Header.Set("Accept", "application/json")
// Perform DELETE request
if resp, err = client.Do(req); err != nil {
return err
} //if
defer resp.Body.Close()
// Check reponse code and report any errors
// Intercom sends back a 200 for valid requests
if resp.StatusCode != 200 {
return errors.New(resp.Status)
} //if
return nil
} //DeleteTag
func (this *Intercom_t) GetTagList(tags *TagList_t) (err error) {
var (
req *http.Request
resp *http.Response
client = new(http.Client)
) //var
// Create new GET request
if req, err = http.NewRequest("GET", TAG_GET_API_ENDPOINT, nil); err != nil {
return err
} //if
// Set authentication and headers
req.SetBasicAuth(this.AppId, this.ApiKey)
req.Header.Set("Accept", "application/json")
// Perform GET request
if resp, err = client.Do(req); err != nil {
return err
} //if
defer resp.Body.Close()
// Check reponse code and report any errors
// Intercom sends back a 200 for valid requests
if resp.StatusCode != 200 {
return errors.New(resp.Status)
} //if
// Decode JSON response into User_t struct
if err = json.NewDecoder(resp.Body).Decode(tags); err != nil {
return err
} //if
return nil
} //GetTagList
func (this *Intercom_t) PostTag(tag Tag_t) (err error) {
var (
req *http.Request
resp *http.Response
buffer = new(bytes.Buffer)
client = new(http.Client)
) //var
// Encode tag struct into JSON
if err = json.NewEncoder(buffer).Encode(tag); err != nil {
return err
} //if
// Create new POST request
if req, err = http.NewRequest("POST", TAG_POST_API_ENDPOINT, buffer); err != nil {
return err
} //if
// Set authentication and headers
req.SetBasicAuth(this.AppId, this.ApiKey)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
// Perform POST request
if resp, err = client.Do(req); err != nil {
return err
} //if
defer resp.Body.Close()
// Check reponse code and report any errors
// Intercom sends back a 200 for valid requests
if resp.StatusCode != 200 {
return errors.New(resp.Status)
} //if
return err
} //PostTag