-
Notifications
You must be signed in to change notification settings - Fork 0
/
okhttp.go
110 lines (98 loc) · 2.27 KB
/
okhttp.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
101
102
103
104
105
106
107
108
109
110
package okhttp
import (
"fmt"
"github.com/mredencom/okhttp/log"
"net/http"
"net/url"
"strings"
)
type Handler func(*Response)
// NewRequest 建立一个请求
func NewRequest(method, uri string) (r *Request, err error) {
switch strings.ToUpper(method) {
case http.MethodGet:
method = http.MethodGet
break
case http.MethodPost:
method = http.MethodPost
break
case http.MethodPut:
method = http.MethodPut
break
case http.MethodDelete:
method = http.MethodDelete
break
case http.MethodHead:
method = http.MethodHead
break
case http.MethodPatch:
method = http.MethodPatch
break
case http.MethodOptions:
method = http.MethodOptions
break
case http.MethodTrace:
method = http.MethodTrace
break
case http.MethodConnect:
method = http.MethodConnect
break
default:
err = NoMatchHttpMethod
break
}
// parse url
parse, err := url.Parse(uri)
if err != nil {
return nil, err
}
// header
header := http.Header{}
header.Set("User-Agent", fmt.Sprintf("Okhttp/%s", Version))
r = &Request{
method: method,
url: parse,
header: header,
allowRedirect: true,
debug: false,
isPrintBody: false,
l: log.NewLogger(0),
}
return
}
// Get created a get request
func Get(uri string) (*Request, error) {
return NewRequest(http.MethodGet, uri)
}
// Post created a post request
func Post(uri string) (*Request, error) {
return NewRequest(http.MethodPost, uri)
}
// Put created a put request
func Put(uri string) (*Request, error) {
return NewRequest(http.MethodPut, uri)
}
// Delete created a delete request
func Delete(uri string) (*Request, error) {
return NewRequest(http.MethodDelete, uri)
}
// Head created a head request
func Head(uri string) (*Request, error) {
return NewRequest(http.MethodHead, uri)
}
// Patch created a patch request
func Patch(uri string) (*Request, error) {
return NewRequest(http.MethodPatch, uri)
}
// Options created a options request
func Options(uri string) (*Request, error) {
return NewRequest(http.MethodOptions, uri)
}
// Trace created a trace request
func Trace(uri string) (*Request, error) {
return NewRequest(http.MethodTrace, uri)
}
// Connect created a connect request
func Connect(uri string) (*Request, error) {
return NewRequest(http.MethodConnect, uri)
}