generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 5
/
message_service.go
67 lines (54 loc) · 1.78 KB
/
message_service.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
package httpsms
import (
"context"
"encoding/json"
"net/http"
"strconv"
)
// MessageService is the API client for the `/` endpoint
type MessageService service
// Send adds a new SMS message to be sent by the android phone
//
// API Docs: https://api.httpsms.com/index.html#/Messages/post_messages_send
func (service *MessageService) Send(ctx context.Context, params *MessageSendParams) (*MessageResponse, *Response, error) {
request, err := service.client.newRequest(ctx, http.MethodPost, "/v1/messages/send", params)
if err != nil {
return nil, nil, err
}
response, err := service.client.do(request)
if err != nil {
return nil, response, err
}
message := new(MessageResponse)
if err = json.Unmarshal(*response.Body, message); err != nil {
return nil, response, err
}
return message, response, nil
}
// Index returns a list of messages which are sent between 2 phone numbers. It will be sorted by timestamp in descending order.
//
// API Docs: https://api.httpsms.com/index.html#/Messages/get_messages
func (service *MessageService) Index(ctx context.Context, params *MessageIndexParams) (*MessagesResponse, *Response, error) {
request, err := service.client.newRequest(ctx, http.MethodGet, "/v1/messages", nil)
if err != nil {
return nil, nil, err
}
q := request.URL.Query()
q.Add("skip", strconv.Itoa(params.Skip))
q.Add("owner", params.Owner)
q.Add("contact", params.Contact)
q.Add("limit", strconv.Itoa(params.Limit))
if params.Query != nil {
q.Add("query", *params.Query)
}
request.URL.RawQuery = q.Encode()
response, err := service.client.do(request)
if err != nil {
return nil, response, err
}
messages := new(MessagesResponse)
if err = json.Unmarshal(*response.Body, messages); err != nil {
return nil, response, err
}
return messages, response, nil
}