-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
134 lines (121 loc) · 3.24 KB
/
client.go
File metadata and controls
134 lines (121 loc) · 3.24 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package requests
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
)
type Client struct {
method string
urlPoint string
urlSite string
jsonData []byte
dataForm *url.Values
httpHeaders http.Header
httpRequest *http.Request
httpClient http.Client
Cookie map[string]string
}
type HttpClientInterface interface {
Query(data interface{}, queryType string) *Client
JsonQuery(data interface{}) *Client
FormQuery(data interface{}) *Client
QueryFunc(f func(c *Client) (interface{}, string)) *Client
QueryResult() io.Reader
Headers(m map[string]interface{}) *Client
Header(k string, value interface{}) *Client
HeadersFunc(f func(c *Client)) *Client
SetCookie(cookie map[string]string) *Client
Method(method string) *Client
GetMethod() *Client
PostMethod() *Client
PutMethod() *Client
UrlPoint(urlPoint string) *Client
GetUrl() string
UrlSite(urlSite string) *Client
SetRequest() HttpRequestInterface
}
func (c *Client) SetRequest() HttpRequestInterface {
var err error
if c.method == "" {
c.method = http.MethodGet
}
if c.method == http.MethodGet {
c.httpRequest, err = http.NewRequest(http.MethodGet, c.GetUrl(), nil)
c.httpRequest.URL.RawQuery = c.dataForm.Encode()
} else {
c.httpRequest, err = http.NewRequest(c.method, c.GetUrl(), c.QueryResult())
}
c.httpRequest.Header = c.httpHeaders
for k, v := range c.Cookie {
cookie := fmt.Sprintf("%s=%s", k, v)
if headerCookie := c.httpRequest.Header.Get("Cookie"); headerCookie != "" {
c.httpRequest.Header.Set("Cookie", headerCookie+"; "+cookie)
} else {
c.httpRequest.Header.Set("Cookie", cookie)
}
}
if err != nil {
log.Printf("http.NewRequest error: %v", err)
return &Request{httpRequest: nil}
}
return &Request{httpRequest: c.httpRequest, httpClient: c.httpClient, url: c.GetUrl()}
}
func (c *Client) SetCookie(cookie map[string]string) *Client {
for k, v := range cookie {
c.Cookie[k] = v
}
return c
}
func (c *Client) Header(k string, value interface{}) *Client {
c.httpHeaders.Set(k, fmt.Sprintf("%v", value))
return c
}
func (c *Client) Headers(m map[string]interface{}) *Client {
for k, v := range m {
c.Header(k, v)
}
return c
}
func (c *Client) HeadersFunc(f func(c *Client)) *Client {
f(c)
return c
}
func (c *Client) UrlSite(urlSite string) *Client {
if !strings.Contains(urlSite, "http") {
panic("urlSite error: " + urlSite + " is not support")
}
c.urlSite = urlSite
return c
}
func (c *Client) UrlPoint(urlPoint string) *Client {
c.urlPoint = urlPoint
return c
}
func (c *Client) GetUrl() string {
if strings.TrimSpace(c.urlPoint) != "" {
if c.urlSite[len(c.urlSite)-1:] != "/" && c.urlPoint[:1] != "/" {
log.Printf("urlSite error: %s%s is not support", c.urlSite, c.urlPoint)
c.urlPoint = "/" + c.urlPoint
}
}
return c.urlSite + c.urlPoint
}
func (c *Client) PostMethod() *Client {
return c.Method(http.MethodPost)
}
func (c *Client) GetMethod() *Client {
return c.Method(http.MethodGet)
}
func (c *Client) PutMethod() *Client {
return c.Method(http.MethodPut)
}
func (c *Client) Method(method string) *Client {
if strings.Contains(method, strings.Join([]string{http.MethodPost, http.MethodGet, http.MethodPut}, "")) {
panic("method error: " + method + " is not support")
}
c.method = method
return c
}