This repository has been archived by the owner on May 23, 2019. It is now read-only.
forked from AnalyticalFlavorSystems/closeio-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lead.go
130 lines (121 loc) · 3.42 KB
/
lead.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
121
122
123
124
125
126
127
128
129
130
package closeio
import (
"encoding/json"
"net/url"
)
type Lead struct {
Name string `json:"name,omitempty"`
Url string `json:"url,omitempty"`
Description string `json:"description,omitempty"`
StatusId string `json:"status_id,omitempty"`
Status string `json:"status,omitempty"`
Contacts *[]Contact `json:"contacts"`
Custom map[string]string `json:"custom,omitempty"`
Addresses *[]Address `json:"addresses"`
}
type LeadResp struct {
StatusId string `json:"status_id"`
StatusLabel string `json:"status_label"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Addresses []Address `json:"addresses"`
Custom map[string]string `json:"custom"`
Name string `json:"name"`
Contacts []ContactResp `json:"contacts"`
Url string `json:"url"`
Id string `json:"id"`
DateUpdated string `json:"date_updated"`
DateCreated string `json:"date_created"`
CreatedBy string `json:"created_by"`
UpdatedBy string `json:"updated_by"`
OrganizationId string `json:"organization_id"`
HtmlUrl string `json:"html_url"`
Opportunities *[]OpportunityResp `json:"opportunities"`
//Tasks []string `json:"tasks"` // TODO: change this
}
type Leads struct {
HasMore bool `json:"has_more"`
TotalResults int `json:"total_results"`
Data []LeadResp `json:"data"`
}
type LeadSearch struct {
Query string
}
func (c *Closeio) Leads(ls *LeadSearch) (l *Leads, err error) {
leadType := ""
if ls == nil {
leadType = "lead/"
} else {
// Set query and encode
// TODO: Set limit, etc.
v := url.Values{}
v.Set("query", ls.Query)
query := v.Encode()
leadType = "lead/?" + query
}
resp, err := request(leadType, "GET", c.Token, nil)
if err != nil {
return nil, err
}
leads := Leads{}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&leads)
if err != nil {
return nil, err
}
return &leads, nil
}
func (c *Closeio) CreateLead(lead *Lead) (l *LeadResp, err error) {
data, err := marshal(lead)
if err != nil {
return nil, err
}
resp, err := request("lead/", "POST", c.Token, data)
if err != nil {
return nil, err
}
leadresp := LeadResp{}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&leadresp)
if err != nil {
return nil, err
}
return &leadresp, nil
}
func (c *Closeio) GetLead(id string) (l *LeadResp, err error) {
resp, err := request("lead/"+id+"/", "GET", c.Token, nil)
if err != nil {
return nil, err
}
lead := LeadResp{}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&lead)
if err != nil {
return nil, err
}
return &lead, nil
}
func (c *Closeio) UpdateLead(id string, lead *map[string]string) (l *LeadResp, err error) {
data, err := marshal(lead)
if err != nil {
return nil, err
}
resp, err := request("lead/"+id+"/", "PUT", c.Token, data)
if err != nil {
return nil, err
}
lr := LeadResp{}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&lr)
if err != nil {
return nil, err;
}
return &lr, nil
}
func (c *Closeio) DeleteLead(id string) error {
_, err := request("lead/"+id+"/", "DELETE", c.Token, nil)
if err != nil {
return err
}
return nil
}