Skip to content

Commit 2fa10fc

Browse files
authored
Merge pull request #4 from cikupin/feature/add-user
Feature/add-user-data
2 parents 6d77535 + 1854333 commit 2fa10fc

File tree

6 files changed

+379
-84
lines changed

6 files changed

+379
-84
lines changed

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ _**Note** : This a modified version version of [EddyTravels/smooch](https://gith
77
## Additional Feature
88

99
- Token expiration & its checking.
10+
- Pre-create app user and link app user to specified channel functionality.
11+
- Send message in whatsapp HSM format.
1012
- Renew token functionality whenever token is expired.
13+
- Support smooch basic auth and JWT auth.
1114
- Redis support as a centralized storage to store JWT token for supporting autoscaling environment. Use redigo as redis library.
1215

1316
## Tips
@@ -22,6 +25,32 @@ $ go get -u github.com/kitabisa/smooch
2225

2326
## Example
2427

28+
Using basic authentication :
29+
30+
31+
```
32+
import (
33+
"os"
34+
35+
"github.com/kitabisa/smooch"
36+
)
37+
38+
func main() {
39+
smoochClient, err := smooch.New(smooch.Options{
40+
Auth: smooch.AuthBasic,
41+
AppID: os.Getenv("SMOOCH_APP_ID"),
42+
KeyID: os.Getenv("SMOOCH_KEY_ID"),
43+
Secret: os.Getenv("SMOOCH_SECRET"),
44+
})
45+
46+
if err != nil {
47+
panic(err)
48+
}
49+
}
50+
```
51+
52+
Using JWT authentication :
53+
2554
```
2655
import (
2756
"os"
@@ -31,6 +60,7 @@ import (
3160
3261
func main() {
3362
smoochClient, err := smooch.New(smooch.Options{
63+
Auth: smooch.AuthJWT,
3464
AppID: os.Getenv("SMOOCH_APP_ID"),
3565
KeyID: os.Getenv("SMOOCH_KEY_ID"),
3666
Secret: os.Getenv("SMOOCH_SECRET"),

error.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import (
66
"net/http"
77
)
88

9+
// ResponseData defines data for every response
10+
type ResponseData struct {
11+
HTTPCode int
12+
Flag string
13+
}
14+
915
type SmoochError struct {
1016
message string
1117
code int
@@ -19,11 +25,11 @@ func (e *SmoochError) Error() string {
1925
return e.message
2026
}
2127

22-
func checkSmoochError(r *http.Response) error {
28+
func checkSmoochError(r *http.Response) (*ResponseData, error) {
2329
var errorPayload ErrorPayload
2430
decodeErr := json.NewDecoder(r.Body).Decode(&errorPayload)
2531
if decodeErr != nil {
26-
return decodeErr
32+
return nil, decodeErr
2733
}
2834

2935
err := &SmoochError{
@@ -35,5 +41,9 @@ func checkSmoochError(r *http.Response) error {
3541
code: r.StatusCode,
3642
}
3743

38-
return err
44+
respData := &ResponseData{
45+
HTTPCode: r.StatusCode,
46+
Flag: errorPayload.Details.Code,
47+
}
48+
return respData, err
3949
}

error_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestCheckSmoochError(t *testing.T) {
2727
Body: r,
2828
}
2929

30-
err := checkSmoochError(response)
30+
_, err := checkSmoochError(response)
3131
assert.Error(t, err)
3232
assert.EqualError(t, err, "StatusCode: 401 Code: unauthorized Message: Authorization is required")
3333
}

0 commit comments

Comments
 (0)