-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcut.go
47 lines (40 loc) · 1.1 KB
/
shortcut.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
package httpclient
import (
"context"
"io"
"net/http"
)
/**
* otherParams type: []byte: body | string: body | io.Reader: body | context.Context: | http.Header | Option
*/
func DoMethod(method Method, url string, otherParams ...interface{}) (*Calling, error) {
calling := New(url).WithMethod(method)
for _, param := range otherParams {
switch p := param.(type) {
case []byte, string, io.Reader:
calling.WithBody(p)
case context.Context:
calling.WithContext(p)
case http.Header:
calling.WithHeader(p)
case *Option:
calling.WithOption(p)
}
}
if err := calling.Do(); err != nil {
return nil, err
}
return calling, nil
}
func Post(url string, otherParams ...interface{}) (*Calling, error) {
return DoMethod(POST, url, otherParams...)
}
func Get(url string, otherParams ...interface{}) (*Calling, error) {
return DoMethod(GET, url, otherParams...)
}
func Put(url string, otherParams ...interface{}) (*Calling, error) {
return DoMethod(PUT, url, otherParams...)
}
func Delete(url string, otherParams ...interface{}) (*Calling, error) {
return DoMethod(DELETE, url, otherParams...)
}