@@ -78,21 +78,21 @@ type Client interface {
78
78
UploadAttachment (r io.Reader , upload AttachmentUpload ) (* Attachment , error )
79
79
}
80
80
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
92
92
RedisStorage * storage.RedisStorage
93
93
}
94
94
95
- func New (o Options ) (* smoochClient , error ) {
95
+ func New (o Options ) (* SmoochClient , error ) {
96
96
if o .KeyID == "" {
97
97
return nil , ErrKeyIDEmpty
98
98
}
@@ -130,18 +130,18 @@ func New(o Options) (*smoochClient, error) {
130
130
return nil , ErrWrongAuth
131
131
}
132
132
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 ,
142
142
}
143
143
144
- if sc .auth == AuthJWT {
144
+ if sc .Auth == AuthJWT {
145
145
if o .RedisPool == nil {
146
146
return nil , ErrRedisNil
147
147
}
@@ -157,32 +157,32 @@ func New(o Options) (*smoochClient, error) {
157
157
}
158
158
}
159
159
160
- sc .mux .HandleFunc (o .WebhookURL , sc .handle )
160
+ sc .Mux .HandleFunc (o .WebhookURL , sc .handle )
161
161
return sc , nil
162
162
}
163
163
164
- func (sc * smoochClient ) Handler () http.Handler {
165
- return sc .mux
164
+ func (sc * SmoochClient ) Handler () http.Handler {
165
+ return sc .Mux
166
166
}
167
167
168
168
// IsJWTExpired will check whether Smooch JWT is expired or not.
169
- func (sc * smoochClient ) IsJWTExpired () (bool , error ) {
169
+ func (sc * SmoochClient ) IsJWTExpired () (bool , error ) {
170
170
jwtToken , err := sc .RedisStorage .GetTokenFromRedis ()
171
171
if err != nil {
172
172
if err == redis .ErrNil {
173
173
return true , nil
174
174
}
175
175
return false , err
176
176
}
177
- return isJWTExpired (jwtToken , sc .secret )
177
+ return isJWTExpired (jwtToken , sc .Secret )
178
178
}
179
179
180
180
// 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 ()
184
184
185
- jwtToken , err := GenerateJWT ("app" , sc .keyID , sc .secret )
185
+ jwtToken , err := GenerateJWT ("app" , sc .KeyID , sc .Secret )
186
186
if err != nil {
187
187
return "" , err
188
188
}
@@ -195,11 +195,11 @@ func (sc *smoochClient) RenewToken() (string, error) {
195
195
return jwtToken , nil
196
196
}
197
197
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 )
200
200
}
201
201
202
- func (sc * smoochClient ) Send (userID string , message * Message ) (* ResponsePayload , * ResponseData , error ) {
202
+ func (sc * SmoochClient ) Send (userID string , message * Message ) (* ResponsePayload , * ResponseData , error ) {
203
203
if userID == "" {
204
204
return nil , nil , ErrUserIDEmpty
205
205
}
@@ -217,7 +217,7 @@ func (sc *smoochClient) Send(userID string, message *Message) (*ResponsePayload,
217
217
}
218
218
219
219
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 ),
221
221
nil ,
222
222
)
223
223
@@ -242,9 +242,9 @@ func (sc *smoochClient) Send(userID string, message *Message) (*ResponsePayload,
242
242
}
243
243
244
244
// 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 ) {
246
246
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 ),
248
248
nil ,
249
249
)
250
250
@@ -268,9 +268,9 @@ func (sc *smoochClient) SendHSM(userID string, hsmMessage *HsmMessage) (*Respons
268
268
return & responsePayload , respData , nil
269
269
}
270
270
271
- func (sc * smoochClient ) GetAppUser (userID string ) (* AppUser , * ResponseData , error ) {
271
+ func (sc * SmoochClient ) GetAppUser (userID string ) (* AppUser , * ResponseData , error ) {
272
272
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 ),
274
274
nil ,
275
275
)
276
276
@@ -289,9 +289,9 @@ func (sc *smoochClient) GetAppUser(userID string) (*AppUser, *ResponseData, erro
289
289
}
290
290
291
291
// 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 ) {
293
293
url := sc .getURL (
294
- fmt .Sprintf ("/v1.1/apps/%s/appusers" , sc .appID ),
294
+ fmt .Sprintf ("/v1.1/apps/%s/appusers" , sc .AppID ),
295
295
nil ,
296
296
)
297
297
@@ -334,9 +334,9 @@ func (sc *smoochClient) PreCreateAppUser(userID, surname, givenName string) (*Ap
334
334
}
335
335
336
336
// 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 ) {
338
338
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 ),
340
340
nil ,
341
341
)
342
342
@@ -384,7 +384,7 @@ func (sc *smoochClient) LinkAppUserToChannel(userID, channelType, confirmationTy
384
384
return response .AppUser , respData , nil
385
385
}
386
386
387
- func (sc * smoochClient ) UploadFileAttachment (filepath string , upload AttachmentUpload ) (* Attachment , * ResponseData , error ) {
387
+ func (sc * SmoochClient ) UploadFileAttachment (filepath string , upload AttachmentUpload ) (* Attachment , * ResponseData , error ) {
388
388
r , err := os .Open (filepath )
389
389
if err != nil {
390
390
return nil , nil , err
@@ -394,7 +394,7 @@ func (sc *smoochClient) UploadFileAttachment(filepath string, upload AttachmentU
394
394
return sc .UploadAttachment (r , upload )
395
395
396
396
}
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 ) {
398
398
399
399
queryParams := url.Values {
400
400
"access" : []string {upload .Access },
@@ -410,7 +410,7 @@ func (sc *smoochClient) UploadAttachment(r io.Reader, upload AttachmentUpload) (
410
410
}
411
411
412
412
url := sc .getURL (
413
- fmt .Sprintf ("/v1.1/apps/%s/attachments" , sc .appID ),
413
+ fmt .Sprintf ("/v1.1/apps/%s/attachments" , sc .AppID ),
414
414
queryParams ,
415
415
)
416
416
@@ -433,9 +433,9 @@ func (sc *smoochClient) UploadAttachment(r io.Reader, upload AttachmentUpload) (
433
433
return & response , respData , nil
434
434
}
435
435
436
- func (sc * smoochClient ) DeleteAttachment (attachment * Attachment ) (* ResponseData , error ) {
436
+ func (sc * SmoochClient ) DeleteAttachment (attachment * Attachment ) (* ResponseData , error ) {
437
437
url := sc .getURL (
438
- fmt .Sprintf ("/v1.1/apps/%s/attachments" , sc .appID ),
438
+ fmt .Sprintf ("/v1.1/apps/%s/attachments" , sc .AppID ),
439
439
nil ,
440
440
)
441
441
@@ -458,7 +458,7 @@ func (sc *smoochClient) DeleteAttachment(attachment *Attachment) (*ResponseData,
458
458
return respData , nil
459
459
}
460
460
461
- func (sc * smoochClient ) handle (w http.ResponseWriter , r * http.Request ) {
461
+ func (sc * SmoochClient ) handle (w http.ResponseWriter , r * http.Request ) {
462
462
w .Header ().Set ("Content-Type" , "application/json" )
463
463
if r .Method != http .MethodPost {
464
464
w .WriteHeader (http .StatusBadRequest )
@@ -468,15 +468,15 @@ func (sc *smoochClient) handle(w http.ResponseWriter, r *http.Request) {
468
468
body , err := ioutil .ReadAll (r .Body )
469
469
if err != nil {
470
470
w .WriteHeader (http .StatusInternalServerError )
471
- sc .logger .Errorw ("request body read failed" , "err" , err )
471
+ sc .Logger .Errorw ("request body read failed" , "err" , err )
472
472
return
473
473
}
474
474
475
475
var payload Payload
476
476
err = json .Unmarshal (body , & payload )
477
477
if err != nil {
478
478
w .WriteHeader (http .StatusUnprocessableEntity )
479
- sc .logger .Errorw ("could not decode response" , "err" , err )
479
+ sc .Logger .Errorw ("could not decode response" , "err" , err )
480
480
return
481
481
}
482
482
@@ -485,15 +485,15 @@ func (sc *smoochClient) handle(w http.ResponseWriter, r *http.Request) {
485
485
sc .dispatch (& payload )
486
486
}
487
487
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 {
490
490
handler (p )
491
491
}
492
492
}
493
493
494
- func (sc * smoochClient ) getURL (endpoint string , values url.Values ) string {
494
+ func (sc * SmoochClient ) getURL (endpoint string , values url.Values ) string {
495
495
rootURL := usRootURL
496
- if sc .region == RegionEU {
496
+ if sc .Region == RegionEU {
497
497
rootURL = euRootURL
498
498
}
499
499
@@ -509,7 +509,7 @@ func (sc *smoochClient) getURL(endpoint string, values url.Values) string {
509
509
return u .String ()
510
510
}
511
511
512
- func (sc * smoochClient ) createRequest (
512
+ func (sc * SmoochClient ) createRequest (
513
513
method string ,
514
514
url string ,
515
515
buf * bytes.Buffer ,
@@ -527,7 +527,7 @@ func (sc *smoochClient) createRequest(
527
527
header .Set (contentTypeHeaderKey , contentTypeJSON )
528
528
}
529
529
530
- if sc .auth == AuthJWT {
530
+ if sc .Auth == AuthJWT {
531
531
isExpired , err := sc .IsJWTExpired ()
532
532
if err != nil {
533
533
return nil , err
@@ -559,14 +559,14 @@ func (sc *smoochClient) createRequest(
559
559
}
560
560
req .Header = header
561
561
562
- if sc .auth == AuthBasic {
563
- req .SetBasicAuth (sc .keyID , sc .secret )
562
+ if sc .Auth == AuthBasic {
563
+ req .SetBasicAuth (sc .KeyID , sc .Secret )
564
564
}
565
565
566
566
return req , nil
567
567
}
568
568
569
- func (sc * smoochClient ) createMultipartRequest (
569
+ func (sc * SmoochClient ) createMultipartRequest (
570
570
url string ,
571
571
values map [string ]io.Reader ) (* http.Request , error ) {
572
572
buf := new (bytes.Buffer )
@@ -610,8 +610,8 @@ func (sc *smoochClient) createMultipartRequest(
610
610
return req , nil
611
611
}
612
612
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 )
615
615
if err != nil {
616
616
return nil , err
617
617
}
0 commit comments