forked from andrexus/goinwx
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
contact.go
149 lines (122 loc) · 3.79 KB
/
contact.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package goinwx
import (
"github.com/fatih/structs"
"github.com/go-viper/mapstructure/v2"
)
const (
methodContactInfo = "contact.info"
methodContactList = "contact.list"
methodContactCreate = "contact.create"
methodContactDelete = "contact.delete"
methodContactUpdate = "contact.update"
)
// ContactService API access to Contact.
type ContactService service
// Create Creates a contact.
func (s *ContactService) Create(request *ContactCreateRequest) (int, error) {
req := s.client.NewRequest(methodContactCreate, structs.Map(request))
resp, err := s.client.Do(req)
if err != nil {
return 0, err
}
result := make(map[string]int)
err = mapstructure.Decode(resp, &result)
if err != nil {
return 0, err
}
return result["id"], nil
}
// Delete Deletes a contact.
func (s *ContactService) Delete(roID int) error {
req := s.client.NewRequest(methodContactDelete, map[string]interface{}{
"id": roID,
})
_, err := s.client.Do(req)
return err
}
// Update Updates a contact.
func (s *ContactService) Update(request *ContactUpdateRequest) error {
req := s.client.NewRequest(methodContactUpdate, structs.Map(request))
_, err := s.client.Do(req)
return err
}
// Info Get information about a contact.
func (s *ContactService) Info(contactID int) (*ContactInfoResponse, error) {
requestMap := make(map[string]interface{})
requestMap["wide"] = 1
if contactID != 0 {
requestMap["id"] = contactID
}
req := s.client.NewRequest(methodContactInfo, requestMap)
resp, err := s.client.Do(req)
if err != nil {
return nil, err
}
result := ContactInfoResponse{}
err = mapstructure.Decode(resp, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// List Search contacts.
func (s *ContactService) List(search string) (*ContactListResponse, error) {
requestMap := make(map[string]interface{})
if search != "" {
requestMap["search"] = search
}
req := s.client.NewRequest(methodContactList, requestMap)
resp, err := s.client.Do(req)
if err != nil {
return nil, err
}
result := ContactListResponse{}
err = mapstructure.Decode(resp, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// ContactCreateRequest API model.
type ContactCreateRequest struct {
Type string `structs:"type"`
Name string `structs:"name"`
Org string `structs:"org,omitempty"`
Street string `structs:"street"`
City string `structs:"city"`
PostalCode string `structs:"pc"`
StateProvince string `structs:"sp,omitempty"`
CountryCode string `structs:"cc"`
Voice string `structs:"voice"`
Fax string `structs:"fax,omitempty"`
Email string `structs:"email"`
Remarks string `structs:"remarks,omitempty"`
Protection bool `structs:"protection,omitempty"`
Testing bool `structs:"testing,omitempty"`
}
// ContactUpdateRequest API model.
type ContactUpdateRequest struct {
ID int `structs:"id"`
Name string `structs:"name,omitempty"`
Org string `structs:"org,omitempty"`
Street string `structs:"street,omitempty"`
City string `structs:"city,omitempty"`
PostalCode string `structs:"pc,omitempty"`
StateProvince string `structs:"sp,omitempty"`
CountryCode string `structs:"cc,omitempty"`
Voice string `structs:"voice,omitempty"`
Fax string `structs:"fax,omitempty"`
Email string `structs:"email,omitempty"`
Remarks string `structs:"remarks,omitempty"`
Protection bool `structs:"protection,omitempty"`
Testing bool `structs:"testing,omitempty"`
}
// ContactInfoResponse API model.
type ContactInfoResponse struct {
Contact Contact `mapstructure:"contact"`
}
// ContactListResponse API model.
type ContactListResponse struct {
Count int `mapstructure:"count"`
Contacts []Contact `mapstructure:"contact"`
}