-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompany.go
More file actions
111 lines (91 loc) · 2.53 KB
/
company.go
File metadata and controls
111 lines (91 loc) · 2.53 KB
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
package intercom
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/url"
"strings"
) //import
const (
COMPANIES_API_ENDPOINT string = "https://api.intercom.io/companies"
) //const
func NewCompany(httpMethod string) *Company_t {
httpMethod = strings.TrimSpace(strings.ToUpper(httpMethod))
if httpMethod == "POST" {
return &Company_t{
CustomAttributes: make(map[string]interface{}),
Plan: "",
} //NewCompany
} //if
return &Company_t{
CustomAttributes: make(map[string]interface{}),
Plan: CompanyPlan_t{},
} //NewCompany
} //NewCompany
func (this *Intercom_t) GetCompany(com *Company_t) (err error) {
var (
intercomUrl string = USERS_GET_API_ENDPOINT
req *http.Request
resp *http.Response
client = new(http.Client)
) //var
if com.CompanyId != "" {
intercomUrl += "?company_id=" + com.CompanyId
} else if com.Name != "" {
intercomUrl += "?name=" + url.QueryEscape(com.Name)
} //else if
// Create new GET request
if req, err = http.NewRequest("GET", intercomUrl, 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(com); err != nil {
return err
} //if
return nil
} //GetUser
func (this *Intercom_t) PostCompany(com *Company_t) (err error) {
var (
req *http.Request
resp *http.Response
buffer = new(bytes.Buffer)
client = new(http.Client)
) //var
// Encode user struct into JSON
if err = json.NewEncoder(buffer).Encode(*com); err != nil {
return err
} //if
// Create new POST request
if req, err = http.NewRequest("POST", COMPANIES_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
} //PostCompany