github.com/grokify/go-ringcentral-client@v0.3.31/office/v1/util/fax_request.go (about) 1 package clientutil 2 3 import ( 4 "fmt" 5 "io" 6 "mime/multipart" 7 "net/http" 8 "strconv" 9 "strings" 10 "time" 11 12 "github.com/grokify/mogo/mime/multipartutil" 13 "github.com/grokify/mogo/net/http/httputilmore" 14 "github.com/grokify/mogo/net/urlutil" 15 ) 16 17 const ( 18 attachmentFieldName = "attachment" 19 FaxUrl = "/restapi/v1.0/account/~/extension/~/fax" 20 ) 21 22 func BuildFaxApiUrl(serverUrl string) string { 23 return urlutil.JoinAbsolute(serverUrl, FaxUrl) 24 } 25 26 // FaxRequest is a fax request helper that can send more than one attachment. 27 // The core Swagger Codegen appears to only send one file at a time with a fixed 28 // MIME part name. 29 type FaxRequest struct { 30 CoverIndex int 31 CoverPageText string 32 Resolution string 33 SendTime *time.Time 34 IsoCode string 35 To []string 36 FilePaths []string 37 FileHeaders []*multipart.FileHeader 38 } 39 40 func NewFaxRequest() FaxRequest { 41 return FaxRequest{ 42 CoverIndex: -1, 43 To: []string{}, 44 FilePaths: []string{}, 45 FileHeaders: []*multipart.FileHeader{}} 46 } 47 48 func (fax *FaxRequest) builder() (multipartutil.MultipartBuilder, error) { 49 builder := multipartutil.NewMultipartBuilder() 50 51 if fax.CoverIndex >= 0 { 52 if err := builder.WriteFieldString("coverIndex", strconv.Itoa(fax.CoverIndex)); err != nil { 53 return builder, err 54 } 55 } 56 if len(strings.TrimSpace(fax.CoverPageText)) > 0 { 57 if err := builder.WriteFieldString("coverPageText", fax.CoverPageText); err != nil { 58 return builder, err 59 } 60 } 61 if len(strings.TrimSpace(fax.Resolution)) > 0 { 62 if err := builder.WriteFieldString("faxResolution", fax.Resolution); err != nil { 63 return builder, err 64 } 65 } 66 if fax.SendTime != nil { 67 if err := builder.WriteFieldString("sendTime", fax.SendTime.Format(time.RFC3339)); err != nil { 68 return builder, err 69 } 70 } 71 for _, to := range fax.To { 72 to := strings.TrimSpace(to) 73 if len(to) > 0 { 74 if err := builder.WriteFieldString("to", to); err != nil { 75 return builder, err 76 } 77 } 78 } 79 80 for _, filePath := range fax.FilePaths { 81 if err := builder.WriteFilePath(attachmentFieldName, filePath); err != nil { 82 return builder, err 83 } 84 } 85 for _, fileHeader := range fax.FileHeaders { 86 if err := builder.WriteFileHeader(attachmentFieldName, fileHeader); err != nil { 87 return builder, err 88 } 89 } 90 91 err := builder.Close() 92 return builder, err 93 } 94 95 func (fax *FaxRequest) Post(httpClient *http.Client, url string) (*http.Response, error) { 96 if !strings.Contains(url, "fax") { 97 url = urlutil.JoinAbsolute(url, FaxUrl) 98 } 99 builder, err := fax.builder() 100 if err != nil { 101 return nil, err 102 } 103 104 req, err := http.NewRequest(http.MethodPost, url, io.NopCloser(builder.Buffer)) 105 if err != nil { 106 return nil, err 107 } 108 if ct, err := builder.ContentTypeHeader(httputilmore.ContentTypeMultipartFormData); err != nil { 109 return nil, err 110 } else { 111 req.Header.Set(httputilmore.HeaderContentType, ct) 112 return httpClient.Do(req) 113 } 114 } 115 116 type FaxCoverPage int 117 118 const ( 119 None FaxCoverPage = iota // 0 120 Ancient // 1 121 Birthday // 2 122 Blank // 3 123 Clasmod // 4 124 Classic // 5 125 Confidential // 5 126 Contempo // 7 127 Elegant // 8 128 Express // 9 129 Formal // 10 130 Jazzy // 11 131 Modern // 12 132 Urgent // 13 133 ) 134 135 var faxCoverPages = [...]string{ 136 "None", // 0 137 "Ancient", // 1 138 "Birthday", // 2 139 "Blank", // 3 140 "Clasmod", // 4 141 "Classic", // 5 142 "Confidential", // 6 143 "Contempo", // 7 144 "Elegant", // 8 145 "Express", // 9 146 "Formal", // 10 147 "Jazzy", // 11 148 "Modern", // 12 149 "Urgent", // 13 150 } 151 152 // String returns the English name of the fax cover page ("None", "Ancient", ...). 153 func (d FaxCoverPage) String() string { 154 if None <= d && d <= Urgent { 155 return faxCoverPages[d] 156 } 157 return faxCoverPages[0] 158 } 159 160 func FaxCoverPageNameToIndex(s string) (int, error) { 161 s = strings.ToLower(strings.TrimSpace(s)) 162 l := len(faxCoverPages) 163 for i := 0; i < l; i++ { 164 name := strings.ToLower(faxCoverPages[i]) 165 if s == name { 166 return i, nil 167 } 168 } 169 return 0, fmt.Errorf("FaxCoverPage NotFound [%v]", s) 170 }