forked from intercom/intercom-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
company_api.go
100 lines (88 loc) · 2.9 KB
/
company_api.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
package intercom
import (
"encoding/json"
"errors"
"fmt"
"gopkg.in/intercom/intercom-go.v2/interfaces"
)
// CompanyRepository defines the interface for working with Companies through the API.
type CompanyRepository interface {
find(CompanyIdentifiers) (Company, error)
list(companyListParams) (CompanyList, error)
scroll(scrollParam string) (CompanyList, error)
save(*Company) (Company, error)
}
// CompanyAPI implements CompanyRepository
type CompanyAPI struct {
httpClient interfaces.HTTPClient
}
type requestCompany struct {
ID string `json:"id,omitempty"`
CompanyID string `json:"company_id,omitempty"`
Name string `json:"name,omitempty"`
RemoteCreatedAt int64 `json:"remote_created_at,omitempty"`
MonthlySpend int64 `json:"monthly_spend,omitempty"`
Plan string `json:"plan,omitempty"`
CustomAttributes map[string]interface{} `json:"custom_attributes,omitempty"`
}
func (api CompanyAPI) find(params CompanyIdentifiers) (Company, error) {
company := Company{}
data, err := api.getClientForFind(params)
if err != nil {
return company, err
}
err = json.Unmarshal(data, &company)
return company, err
}
func (api CompanyAPI) getClientForFind(params CompanyIdentifiers) ([]byte, error) {
switch {
case params.ID != "":
return api.httpClient.Get(fmt.Sprintf("/companies/%s", params.ID), nil)
case params.CompanyID != "", params.Name != "":
return api.httpClient.Get("/companies", params)
}
return nil, errors.New("Missing Company Identifier")
}
func (api CompanyAPI) list(params companyListParams) (CompanyList, error) {
companyList := CompanyList{}
data, err := api.httpClient.Get("/companies", params)
if err != nil {
return companyList, err
}
err = json.Unmarshal(data, &companyList)
return companyList, err
}
func (api CompanyAPI) scroll(scrollParam string) (CompanyList, error) {
companyList := CompanyList{}
params := scrollParams{ScrollParam: scrollParam }
data, err := api.httpClient.Get("/companies/scroll", params)
if err != nil {
return companyList, err
}
err = json.Unmarshal(data, &companyList)
return companyList, err
}
func (api CompanyAPI) save(company *Company) (Company, error) {
requestCompany := requestCompany{
ID: company.ID,
Name: company.Name,
CompanyID: company.CompanyID,
RemoteCreatedAt: company.RemoteCreatedAt,
MonthlySpend: company.MonthlySpend,
Plan: api.getPlanName(company),
CustomAttributes: company.CustomAttributes,
}
savedCompany := Company{}
data, err := api.httpClient.Post("/companies", &requestCompany)
if err != nil {
return savedCompany, err
}
err = json.Unmarshal(data, &savedCompany)
return savedCompany, err
}
func (api CompanyAPI) getPlanName(company *Company) string {
if company.Plan == nil {
return ""
}
return company.Plan.Name
}