-
Notifications
You must be signed in to change notification settings - Fork 6
/
methods.go
100 lines (85 loc) · 1.87 KB
/
methods.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
package fetch
import (
"io"
"github.com/go-zoox/headers"
)
// Head is http.head
func (f *Fetch) Head(url string, config ...*Config) *Fetch {
return f.
SetConfig(config...).
SetMethod(HEAD).
SetURL(url)
}
// Get is http.get
func (f *Fetch) Get(url string, config ...*Config) *Fetch {
return f.
SetConfig(config...).
SetMethod(GET).
SetURL(url)
}
// Post is http.post
func (f *Fetch) Post(url string, config ...*Config) *Fetch {
return f.
SetConfig(config...).
SetMethod(POST).
SetURL(url)
}
// Put is http.put
func (f *Fetch) Put(url string, config ...*Config) *Fetch {
return f.
SetConfig(config...).
SetMethod(PUT).
SetURL(url)
}
// Patch is http.patch
func (f *Fetch) Patch(url string, config ...*Config) *Fetch {
return f.
SetConfig(config...).
SetMethod(PATCH).
SetURL(url)
}
// Delete is http.delete
func (f *Fetch) Delete(url string, config ...*Config) *Fetch {
return f.
SetConfig(config...).
SetMethod(DELETE).
SetURL(url)
}
// Download downloads file by url
func (f *Fetch) Download(url string, filepath string, config ...*Config) *Fetch {
return f.
SetHeader(headers.AcceptEncoding, "gzip").
SetConfig(config...).
SetMethod(GET).
SetURL(url).
SetDownloadFilePath(filepath)
}
// Upload upload a file
func (f *Fetch) Upload(url string, file io.Reader, config ...*Config) *Fetch {
return f.
SetConfig(config...).
SetMethod(POST).
SetURL(url).
SetHeader(headers.ContentType, "multipart/form-data").
SetBody(map[string]interface{}{
"file": file,
})
}
// Stream ...
func (f *Fetch) Stream(url string, config ...*Config) *Fetch {
var cfg *Config = &Config{}
if len(config) > 0 {
cfg = config[0]
}
if cfg.Method == "" {
cfg.Method = GET
}
cfg.IsStream = true
return f.
SetConfig(cfg).
SetURL(url)
}
// func (f *Fetch) JSON() *Response {
// f.SetHeader("accept", "application/json")
// return f.Execute()
// }