Skip to content

Commit 5f77f64

Browse files
authored
Merge pull request #5 from cikupin/feature/add-user
export struct
2 parents 2fa10fc + 9344dc9 commit 5f77f64

File tree

1 file changed

+62
-62
lines changed

1 file changed

+62
-62
lines changed

smooch.go

+62-62
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,21 @@ type Client interface {
7878
UploadAttachment(r io.Reader, upload AttachmentUpload) (*Attachment, error)
7979
}
8080

81-
type smoochClient struct {
82-
mux *http.ServeMux
83-
auth string
84-
appID string
85-
keyID string
86-
secret string
87-
logger Logger
88-
region string
89-
webhookEventHandlers []WebhookEventHandler
90-
httpClient *http.Client
91-
mtx sync.Mutex
81+
type SmoochClient struct {
82+
Mux *http.ServeMux
83+
Auth string
84+
AppID string
85+
KeyID string
86+
Secret string
87+
Logger Logger
88+
Region string
89+
WebhookEventHandlers []WebhookEventHandler
90+
HttpClient *http.Client
91+
Mtx sync.Mutex
9292
RedisStorage *storage.RedisStorage
9393
}
9494

95-
func New(o Options) (*smoochClient, error) {
95+
func New(o Options) (*SmoochClient, error) {
9696
if o.KeyID == "" {
9797
return nil, ErrKeyIDEmpty
9898
}
@@ -130,18 +130,18 @@ func New(o Options) (*smoochClient, error) {
130130
return nil, ErrWrongAuth
131131
}
132132

133-
sc := &smoochClient{
134-
auth: o.Auth,
135-
mux: o.Mux,
136-
appID: o.AppID,
137-
keyID: o.KeyID,
138-
secret: o.Secret,
139-
logger: o.Logger,
140-
region: region,
141-
httpClient: o.HttpClient,
133+
sc := &SmoochClient{
134+
Auth: o.Auth,
135+
Mux: o.Mux,
136+
AppID: o.AppID,
137+
KeyID: o.KeyID,
138+
Secret: o.Secret,
139+
Logger: o.Logger,
140+
Region: region,
141+
HttpClient: o.HttpClient,
142142
}
143143

144-
if sc.auth == AuthJWT {
144+
if sc.Auth == AuthJWT {
145145
if o.RedisPool == nil {
146146
return nil, ErrRedisNil
147147
}
@@ -157,32 +157,32 @@ func New(o Options) (*smoochClient, error) {
157157
}
158158
}
159159

160-
sc.mux.HandleFunc(o.WebhookURL, sc.handle)
160+
sc.Mux.HandleFunc(o.WebhookURL, sc.handle)
161161
return sc, nil
162162
}
163163

164-
func (sc *smoochClient) Handler() http.Handler {
165-
return sc.mux
164+
func (sc *SmoochClient) Handler() http.Handler {
165+
return sc.Mux
166166
}
167167

168168
// IsJWTExpired will check whether Smooch JWT is expired or not.
169-
func (sc *smoochClient) IsJWTExpired() (bool, error) {
169+
func (sc *SmoochClient) IsJWTExpired() (bool, error) {
170170
jwtToken, err := sc.RedisStorage.GetTokenFromRedis()
171171
if err != nil {
172172
if err == redis.ErrNil {
173173
return true, nil
174174
}
175175
return false, err
176176
}
177-
return isJWTExpired(jwtToken, sc.secret)
177+
return isJWTExpired(jwtToken, sc.Secret)
178178
}
179179

180180
// RenewToken will generate new Smooch JWT token.
181-
func (sc *smoochClient) RenewToken() (string, error) {
182-
sc.mtx.Lock()
183-
defer sc.mtx.Unlock()
181+
func (sc *SmoochClient) RenewToken() (string, error) {
182+
sc.Mtx.Lock()
183+
defer sc.Mtx.Unlock()
184184

185-
jwtToken, err := GenerateJWT("app", sc.keyID, sc.secret)
185+
jwtToken, err := GenerateJWT("app", sc.KeyID, sc.Secret)
186186
if err != nil {
187187
return "", err
188188
}
@@ -195,11 +195,11 @@ func (sc *smoochClient) RenewToken() (string, error) {
195195
return jwtToken, nil
196196
}
197197

198-
func (sc *smoochClient) AddWebhookEventHandler(handler WebhookEventHandler) {
199-
sc.webhookEventHandlers = append(sc.webhookEventHandlers, handler)
198+
func (sc *SmoochClient) AddWebhookEventHandler(handler WebhookEventHandler) {
199+
sc.WebhookEventHandlers = append(sc.WebhookEventHandlers, handler)
200200
}
201201

202-
func (sc *smoochClient) Send(userID string, message *Message) (*ResponsePayload, *ResponseData, error) {
202+
func (sc *SmoochClient) Send(userID string, message *Message) (*ResponsePayload, *ResponseData, error) {
203203
if userID == "" {
204204
return nil, nil, ErrUserIDEmpty
205205
}
@@ -217,7 +217,7 @@ func (sc *smoochClient) Send(userID string, message *Message) (*ResponsePayload,
217217
}
218218

219219
url := sc.getURL(
220-
fmt.Sprintf("/v1.1/apps/%s/appusers/%s/messages", sc.appID, userID),
220+
fmt.Sprintf("/v1.1/apps/%s/appusers/%s/messages", sc.AppID, userID),
221221
nil,
222222
)
223223

@@ -242,9 +242,9 @@ func (sc *smoochClient) Send(userID string, message *Message) (*ResponsePayload,
242242
}
243243

244244
// SendHSM will send message using Whatsapp HSM template
245-
func (sc *smoochClient) SendHSM(userID string, hsmMessage *HsmMessage) (*ResponsePayload, *ResponseData, error) {
245+
func (sc *SmoochClient) SendHSM(userID string, hsmMessage *HsmMessage) (*ResponsePayload, *ResponseData, error) {
246246
url := sc.getURL(
247-
fmt.Sprintf("/v1.1/apps/%s/appusers/%s/messages", sc.appID, userID),
247+
fmt.Sprintf("/v1.1/apps/%s/appusers/%s/messages", sc.AppID, userID),
248248
nil,
249249
)
250250

@@ -268,9 +268,9 @@ func (sc *smoochClient) SendHSM(userID string, hsmMessage *HsmMessage) (*Respons
268268
return &responsePayload, respData, nil
269269
}
270270

271-
func (sc *smoochClient) GetAppUser(userID string) (*AppUser, *ResponseData, error) {
271+
func (sc *SmoochClient) GetAppUser(userID string) (*AppUser, *ResponseData, error) {
272272
url := sc.getURL(
273-
fmt.Sprintf("/v1.1/apps/%s/appusers/%s", sc.appID, userID),
273+
fmt.Sprintf("/v1.1/apps/%s/appusers/%s", sc.AppID, userID),
274274
nil,
275275
)
276276

@@ -289,9 +289,9 @@ func (sc *smoochClient) GetAppUser(userID string) (*AppUser, *ResponseData, erro
289289
}
290290

291291
// PreCreateAppUser will register user to smooch
292-
func (sc *smoochClient) PreCreateAppUser(userID, surname, givenName string) (*AppUser, *ResponseData, error) {
292+
func (sc *SmoochClient) PreCreateAppUser(userID, surname, givenName string) (*AppUser, *ResponseData, error) {
293293
url := sc.getURL(
294-
fmt.Sprintf("/v1.1/apps/%s/appusers", sc.appID),
294+
fmt.Sprintf("/v1.1/apps/%s/appusers", sc.AppID),
295295
nil,
296296
)
297297

@@ -334,9 +334,9 @@ func (sc *smoochClient) PreCreateAppUser(userID, surname, givenName string) (*Ap
334334
}
335335

336336
// LinkAppUserToChannel will link user to specifiied channel
337-
func (sc *smoochClient) LinkAppUserToChannel(userID, channelType, confirmationType, phoneNumber string) (*AppUser, *ResponseData, error) {
337+
func (sc *SmoochClient) LinkAppUserToChannel(userID, channelType, confirmationType, phoneNumber string) (*AppUser, *ResponseData, error) {
338338
url := sc.getURL(
339-
fmt.Sprintf("/v1.1/apps/%s/appusers/%s/channels", sc.appID, userID),
339+
fmt.Sprintf("/v1.1/apps/%s/appusers/%s/channels", sc.AppID, userID),
340340
nil,
341341
)
342342

@@ -384,7 +384,7 @@ func (sc *smoochClient) LinkAppUserToChannel(userID, channelType, confirmationTy
384384
return response.AppUser, respData, nil
385385
}
386386

387-
func (sc *smoochClient) UploadFileAttachment(filepath string, upload AttachmentUpload) (*Attachment, *ResponseData, error) {
387+
func (sc *SmoochClient) UploadFileAttachment(filepath string, upload AttachmentUpload) (*Attachment, *ResponseData, error) {
388388
r, err := os.Open(filepath)
389389
if err != nil {
390390
return nil, nil, err
@@ -394,7 +394,7 @@ func (sc *smoochClient) UploadFileAttachment(filepath string, upload AttachmentU
394394
return sc.UploadAttachment(r, upload)
395395

396396
}
397-
func (sc *smoochClient) UploadAttachment(r io.Reader, upload AttachmentUpload) (*Attachment, *ResponseData, error) {
397+
func (sc *SmoochClient) UploadAttachment(r io.Reader, upload AttachmentUpload) (*Attachment, *ResponseData, error) {
398398

399399
queryParams := url.Values{
400400
"access": []string{upload.Access},
@@ -410,7 +410,7 @@ func (sc *smoochClient) UploadAttachment(r io.Reader, upload AttachmentUpload) (
410410
}
411411

412412
url := sc.getURL(
413-
fmt.Sprintf("/v1.1/apps/%s/attachments", sc.appID),
413+
fmt.Sprintf("/v1.1/apps/%s/attachments", sc.AppID),
414414
queryParams,
415415
)
416416

@@ -433,9 +433,9 @@ func (sc *smoochClient) UploadAttachment(r io.Reader, upload AttachmentUpload) (
433433
return &response, respData, nil
434434
}
435435

436-
func (sc *smoochClient) DeleteAttachment(attachment *Attachment) (*ResponseData, error) {
436+
func (sc *SmoochClient) DeleteAttachment(attachment *Attachment) (*ResponseData, error) {
437437
url := sc.getURL(
438-
fmt.Sprintf("/v1.1/apps/%s/attachments", sc.appID),
438+
fmt.Sprintf("/v1.1/apps/%s/attachments", sc.AppID),
439439
nil,
440440
)
441441

@@ -458,7 +458,7 @@ func (sc *smoochClient) DeleteAttachment(attachment *Attachment) (*ResponseData,
458458
return respData, nil
459459
}
460460

461-
func (sc *smoochClient) handle(w http.ResponseWriter, r *http.Request) {
461+
func (sc *SmoochClient) handle(w http.ResponseWriter, r *http.Request) {
462462
w.Header().Set("Content-Type", "application/json")
463463
if r.Method != http.MethodPost {
464464
w.WriteHeader(http.StatusBadRequest)
@@ -468,15 +468,15 @@ func (sc *smoochClient) handle(w http.ResponseWriter, r *http.Request) {
468468
body, err := ioutil.ReadAll(r.Body)
469469
if err != nil {
470470
w.WriteHeader(http.StatusInternalServerError)
471-
sc.logger.Errorw("request body read failed", "err", err)
471+
sc.Logger.Errorw("request body read failed", "err", err)
472472
return
473473
}
474474

475475
var payload Payload
476476
err = json.Unmarshal(body, &payload)
477477
if err != nil {
478478
w.WriteHeader(http.StatusUnprocessableEntity)
479-
sc.logger.Errorw("could not decode response", "err", err)
479+
sc.Logger.Errorw("could not decode response", "err", err)
480480
return
481481
}
482482

@@ -485,15 +485,15 @@ func (sc *smoochClient) handle(w http.ResponseWriter, r *http.Request) {
485485
sc.dispatch(&payload)
486486
}
487487

488-
func (sc *smoochClient) dispatch(p *Payload) {
489-
for _, handler := range sc.webhookEventHandlers {
488+
func (sc *SmoochClient) dispatch(p *Payload) {
489+
for _, handler := range sc.WebhookEventHandlers {
490490
handler(p)
491491
}
492492
}
493493

494-
func (sc *smoochClient) getURL(endpoint string, values url.Values) string {
494+
func (sc *SmoochClient) getURL(endpoint string, values url.Values) string {
495495
rootURL := usRootURL
496-
if sc.region == RegionEU {
496+
if sc.Region == RegionEU {
497497
rootURL = euRootURL
498498
}
499499

@@ -509,7 +509,7 @@ func (sc *smoochClient) getURL(endpoint string, values url.Values) string {
509509
return u.String()
510510
}
511511

512-
func (sc *smoochClient) createRequest(
512+
func (sc *SmoochClient) createRequest(
513513
method string,
514514
url string,
515515
buf *bytes.Buffer,
@@ -527,7 +527,7 @@ func (sc *smoochClient) createRequest(
527527
header.Set(contentTypeHeaderKey, contentTypeJSON)
528528
}
529529

530-
if sc.auth == AuthJWT {
530+
if sc.Auth == AuthJWT {
531531
isExpired, err := sc.IsJWTExpired()
532532
if err != nil {
533533
return nil, err
@@ -559,14 +559,14 @@ func (sc *smoochClient) createRequest(
559559
}
560560
req.Header = header
561561

562-
if sc.auth == AuthBasic {
563-
req.SetBasicAuth(sc.keyID, sc.secret)
562+
if sc.Auth == AuthBasic {
563+
req.SetBasicAuth(sc.KeyID, sc.Secret)
564564
}
565565

566566
return req, nil
567567
}
568568

569-
func (sc *smoochClient) createMultipartRequest(
569+
func (sc *SmoochClient) createMultipartRequest(
570570
url string,
571571
values map[string]io.Reader) (*http.Request, error) {
572572
buf := new(bytes.Buffer)
@@ -610,8 +610,8 @@ func (sc *smoochClient) createMultipartRequest(
610610
return req, nil
611611
}
612612

613-
func (sc *smoochClient) sendRequest(req *http.Request, v interface{}) (*ResponseData, error) {
614-
response, err := sc.httpClient.Do(req)
613+
func (sc *SmoochClient) sendRequest(req *http.Request, v interface{}) (*ResponseData, error) {
614+
response, err := sc.HttpClient.Do(req)
615615
if err != nil {
616616
return nil, err
617617
}

0 commit comments

Comments
 (0)