forked from mailtarget/mailtarget-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
39 lines (33 loc) · 973 Bytes
/
http.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
package layang
import (
"errors"
)
type SuccessResponse struct {
TransmissionId string `json:"transmissionId"`
}
type ErrorResponse struct {
Error string `json:"error"`
Status int `json:"status"`
Code string `json:"code"`
Message string `json:"message"`
}
func (l *Layang) sendMessage(message *Message) (*SuccessResponse, *ErrorResponse, error) {
resp, err := l.resty.R().
SetHeader("Content-Type", "application/json").
SetHeader("Authorization", "Bearer "+l.apiKey).
SetHeader("Accept", "application/json").
SetBody(message).
EnableTrace().
SetResult(SuccessResponse{}).
SetError(ErrorResponse{}).
Post(l.apiBase + "/" + l.apiVersion + "/layang/transmissions")
if resp.IsSuccess() {
return resp.Result().(*SuccessResponse), nil, err
}
if resp.IsError() {
errorResponse := resp.Error().(*ErrorResponse)
errorResponse.Status = resp.StatusCode()
return nil, errorResponse, errors.New(resp.String())
}
return nil, nil, err
}