forked from grokify/go-ringcentral-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fax_request.go
163 lines (146 loc) · 4.05 KB
/
fax_request.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package clientutil
import (
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"strconv"
"strings"
"time"
"github.com/grokify/gotilla/mime/multipartutil"
hum "github.com/grokify/gotilla/net/httputilmore"
"github.com/grokify/gotilla/net/urlutil"
)
const (
attachmentFieldName = "attachment"
FaxUrl = "/restapi/v1.0/account/~/extension/~/fax"
)
func BuildFaxApiUrl(serverUrl string) string {
return urlutil.JoinAbsolute(serverUrl, FaxUrl)
}
// FaxRequest is a fax request helper that can send more than one attachment.
// The core Swagger Codegen appears to only send one file at a time with a fixed
// MIME part name.
type FaxRequest struct {
CoverIndex int
CoverPageText string
Resolution string
SendTime *time.Time
IsoCode string
To []string
FilePaths []string
FileHeaders []*multipart.FileHeader
}
func NewFaxRequest() FaxRequest {
return FaxRequest{
CoverIndex: -1,
To: []string{},
FilePaths: []string{},
FileHeaders: []*multipart.FileHeader{}}
}
func (fax *FaxRequest) builder() (multipartutil.MultipartBuilder, error) {
builder := multipartutil.NewMultipartBuilder()
if fax.CoverIndex >= 0 {
if err := builder.WriteFieldString("coverIndex", strconv.Itoa(fax.CoverIndex)); err != nil {
return builder, err
}
}
if len(strings.TrimSpace(fax.CoverPageText)) > 0 {
if err := builder.WriteFieldString("coverPageText", fax.CoverPageText); err != nil {
return builder, err
}
}
if len(strings.TrimSpace(fax.Resolution)) > 0 {
if err := builder.WriteFieldString("faxResolution", fax.Resolution); err != nil {
return builder, err
}
}
if fax.SendTime != nil {
if err := builder.WriteFieldString("sendTime", fax.SendTime.Format(time.RFC3339)); err != nil {
return builder, err
}
}
for _, to := range fax.To {
to := strings.TrimSpace(to)
if len(to) > 0 {
if err := builder.WriteFieldString("to", to); err != nil {
return builder, err
}
}
}
for _, filePath := range fax.FilePaths {
if err := builder.WriteFilePath(attachmentFieldName, filePath); err != nil {
return builder, err
}
}
for _, fileHeader := range fax.FileHeaders {
if err := builder.WriteFileHeader(attachmentFieldName, fileHeader); err != nil {
return builder, err
}
}
err := builder.Close()
return builder, err
}
func (fax *FaxRequest) Post(httpClient *http.Client, url string) (*http.Response, error) {
builder, err := fax.builder()
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, url, ioutil.NopCloser(builder.Buffer))
if err != nil {
return nil, err
}
req.Header.Set(hum.HeaderContentType, builder.ContentType())
return httpClient.Do(req)
}
type FaxCoverPage int
const (
None FaxCoverPage = iota // 0
Ancient // 1
Birthday // 2
Blank // 3
Clasmod // 4
Classic // 5
Confidential // 5
Contempo // 7
Elegant // 8
Express // 9
Formal // 10
Jazzy // 11
Modern // 12
Urgent // 13
)
var faxCoverPages = [...]string{
"None", // 0
"Ancient", // 1
"Birthday", // 2
"Blank", // 3
"Clasmod", // 4
"Classic", // 5
"Confidential", // 6
"Contempo", // 7
"Elegant", // 8
"Express", // 9
"Formal", // 10
"Jazzy", // 11
"Modern", // 12
"Urgent", // 13
}
// String returns the English name of the fax cover page ("None", "Ancient", ...).
func (d FaxCoverPage) String() string {
if None <= d && d <= Urgent {
return faxCoverPages[d]
}
return faxCoverPages[0]
}
func FaxCoverPageNameToIndex(s string) (int, error) {
s = strings.ToLower(strings.TrimSpace(s))
l := len(faxCoverPages)
for i := 0; i < l; i++ {
name := strings.ToLower(faxCoverPages[i])
if s == name {
return i, nil
}
}
return 0, fmt.Errorf("FaxCoverPage NotFound [%v]", s)
}