-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
74 lines (62 loc) · 1.51 KB
/
option.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
package req
import (
"errors"
"io"
"os"
"path/filepath"
)
// http request header param
type Header map[string]string
// http request param
type Param map[string]string
// used to force append http request param to the uri
type QueryParam map[string]string
// used for set request's Host
type Host string
// represents a file to upload
type FileUpload struct {
// filename in multipart form.
FileName string
// form field name
FieldName string
// file to uplaod, required
File io.ReadCloser
}
type bodyJson struct {
v interface{}
}
// BodyJSON make the object be encoded in json format and set it to the request body
func BodyJSON(v interface{}) *bodyJson {
return &bodyJson{v: v}
}
type bodyXml struct {
v interface{}
}
// BodyXML make the object be encoded in xml format and set it to the request body
func BodyXML(v interface{}) *bodyXml {
return &bodyXml{v: v}
}
// File upload files matching the name pattern such as
// /usr/*/bin/go* (assuming the Separator is '/')
func File(patterns ...string) interface{} {
matches := []string{}
for _, pattern := range patterns {
m, err := filepath.Glob(pattern)
if err != nil {
return err
}
matches = append(matches, m...)
}
if len(matches) == 0 {
return errors.New("req: No file have been matched")
}
uploads := []FileUpload{}
for _, match := range matches {
if s, e := os.Stat(match); e != nil || s.IsDir() {
continue
}
file, _ := os.Open(match)
uploads = append(uploads, FileUpload{File: file, FileName: filepath.Base(match)})
}
return uploads
}