-
Notifications
You must be signed in to change notification settings - Fork 14
/
contact.go
156 lines (128 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
150
151
152
153
154
155
156
package goinwx
import (
"github.com/fatih/structs"
"github.com/mitchellh/mapstructure"
)
const (
methodContactInfo = "contact.info"
methodContactList = "contact.list"
methodContactCreate = "contact.create"
methodContactDelete = "contact.delete"
methodContactUpdate = "contact.update"
)
type ContactService interface {
Create(*ContactCreateRequest) (int, error)
Update(*ContactUpdateRequest) error
Delete(int) error
Info(int) (*ContactInfoResponse, error)
List(string) (*ContactListResponse, error)
}
type ContactServiceOp struct {
client *Client
}
var _ ContactService = &ContactServiceOp{}
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"`
}
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"`
}
type ContactInfoResponse struct {
Contact Contact `mapstructure:"contact"`
}
type ContactListResponse struct {
Count int
Contacts []Contact `mapstructure:"contact"`
}
func (s *ContactServiceOp) 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
}
var result map[string]int
err = mapstructure.Decode(*resp, &result)
if err != nil {
return 0, err
}
return result["id"], nil
}
func (s *ContactServiceOp) Delete(roId int) error {
req := s.client.NewRequest(methodContactDelete, map[string]interface{}{
"id": roId,
})
_, err := s.client.Do(*req)
if err != nil {
return err
}
return nil
}
func (s *ContactServiceOp) Update(request *ContactUpdateRequest) error {
req := s.client.NewRequest(methodContactUpdate, structs.Map(request))
_, err := s.client.Do(*req)
if err != nil {
return err
}
return nil
}
func (s *ContactServiceOp) Info(contactId int) (*ContactInfoResponse, error) {
var 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
}
var result ContactInfoResponse
err = mapstructure.Decode(*resp, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (s *ContactServiceOp) List(search string) (*ContactListResponse, error) {
var 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
}
var result ContactListResponse
err = mapstructure.Decode(*resp, &result)
if err != nil {
return nil, err
}
return &result, nil
}