-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
66 lines (57 loc) · 1.36 KB
/
helpers.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
package octoclient
import (
"encoding/json"
"io"
"mime/multipart"
"os"
"path/filepath"
"strings"
)
func ConvertStructToJSON(obj OctoPayload) (*strings.Reader, error) {
jsonData, err := json.Marshal(obj)
if err != nil {
return nil, err
}
return strings.NewReader(string(jsonData)), nil
}
func ConvertByteToStruct(body []byte) (OctoResponse, error) {
var response OctoResponse
err := json.Unmarshal(body, &response)
if err != nil {
return OctoResponse{}, err
}
return response, nil
}
func trimTrailingSlash(url string) string {
forwardSlash := "/"
return strings.TrimSuffix(url, forwardSlash)
}
func processTextFields(textFields []OctoTextField, multiPartWriter *multipart.Writer) error {
var err error
for _, textField := range textFields {
err = multiPartWriter.WriteField(textField.FieldName, textField.FieldValue)
if err != nil {
return err
}
}
return err
}
func processFileFields(fileFields []OctoFileField, multiPartWriter *multipart.Writer) error {
for _, fileField := range fileFields {
file, err := os.Open(fileField.FilePath)
if err != nil {
return err
}
defer file.Close()
fileName := filepath.Base(fileField.FilePath)
fileWriter, err := multiPartWriter.CreateFormFile(fileField.FieldName, fileName)
if err != nil {
return err
}
_, err = io.Copy(fileWriter, file)
if err != nil {
return err
}
}
return nil
}