forked from Vonage/vonage-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nexmo.go
103 lines (88 loc) · 2.94 KB
/
nexmo.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
// Simple client for using Nexmo's communication APIs. See https://nexmo.com for more information about the APIs.
package nexmo
import (
"fmt"
"net/http"
"runtime"
"github.com/dghubble/sling"
)
// The main client object
type Client struct {
sling *sling.Sling
Insight *InsightService
SMS *SMSService
Call *CallService
Verify *VerifyService
Developer *DeveloperService
Application *ApplicationService
}
// Get a new Client object with the auth configured
func NewClient(httpClient *http.Client, authSet *AuthSet) *Client {
base := sling.New().
Client(httpClient).
Set("User-Agent", "nexmo-go/beta Go/"+runtime.Version())
return &Client{
sling: base,
Insight: newInsightService(base.New(), authSet),
SMS: newSMSService(base.New(), authSet),
Call: newCallService(base.New(), authSet),
Verify: newVerifyService(base.New(), authSet),
Developer: newDeveloperService(base.New(), authSet),
Application: newApplicationService(base.New(), authSet),
}
}
func New(httpClient *http.Client, authSet *AuthSet) *Client {
return NewClient(httpClient, authSet)
}
type APIError struct {
Status int64
ErrorMessage string
}
func (a APIError) Error() string {
return fmt.Sprintf("%d: %s", a.Status, a.ErrorMessage)
}
// func (s *Sling) DoWithPrejudice(req *http.Request, successV, errorV interface{}) (*http.Response, error) {
// resp, err := s.Do(req, successV, nil)
// if err != nil {
// return resp, err
// }
// // when err is nil, resp contains a non-nil resp.Body which must be closed
// defer resp.Body.Close()
// var buf bytes.Buffer
// body := io.TeeReader(resp.Body, &buf)
// // Don't try to decode on 204s
// if resp.StatusCode == 204 {
// return resp, nil
// }
// if successV != nil || errorV != nil {
// err = decodeResponseJSON(resp.StatusCode, body, successV, errorV)
// }
// // This is where we will put our prejudice:
// if err != nil {
// log.Println("Could not parse response:", buf)
// }
// return resp, err
// }
// // decodeResponse decodes response Body into the value pointed to by successV
// // if the response is a success (2XX) or into the value pointed to by failureV
// // otherwise. If the successV or failureV argument to decode into is nil,
// // decoding is skipped.
// // Caller is responsible for closing the resp.Body.
// func decodeResponseJSON(statusCode int, body io.Reader, successV, failureV interface{}) error {
// if 200 <= statusCode && statusCode <= 299 {
// if successV != nil {
// return decodeResponseBodyJSON(body, successV)
// }
// } else {
// if failureV != nil {
// return decodeResponseBodyJSON(body, failureV)
// }
// }
// return nil
// }
// // decodeResponseBodyJSON JSON decodes a Response Body into the value pointed
// // to by v.
// // Caller must provide a non-nil v and close the resp.Body.
// func decodeResponseBodyJSON(body io.Reader, v interface{}) error {
// return json.NewDecoder(body).Decode(v)
// }