11package ghttp
22
33import (
4- "crypto/tls"
54 "github.com/google/uuid"
65 "io/ioutil"
76 "net/http"
@@ -10,27 +9,45 @@ import (
109 "time"
1110)
1211
12+ const DefaultTimeout = 8 * time .Second
13+
14+ var DefaultClient = & http.Client {
15+ Timeout : DefaultTimeout ,
16+ //Transport: &http.Transport{
17+ // TLSClientConfig: &tls.Config{
18+ // InsecureSkipVerify: true,
19+ // },
20+ //},
21+ }
22+
1323type Request struct {
1424 method string
1525 contentType string
1626 expectedType string
1727 uri string
1828 payload interface {}
1929 header http.Header
20- timeout time.Duration
2130 RequestId string
31+
32+ client * http.Client
2233}
2334
24- func NewRequest () * Request {
35+ func NewRequest (options ... Option ) * Request {
2536 request := & Request {
2637 header : make (http.Header ),
2738 RequestId : uuid .New ().String (),
39+ client : DefaultClient ,
2840 }
41+
42+ for _ , o := range options {
43+ o .Apply (request )
44+ }
45+
2946 return request
3047}
3148
3249func Init (method string ) * Request {
33- return NewRequest ().ContentType ("form" ).Method (method ). Timeout ( 8 * time . Second )
50+ return NewRequest ().ContentType ("form" ).Method (method )
3451}
3552
3653func Post (uri string , payload interface {}) * Request {
@@ -42,14 +59,6 @@ func Get(uri string, payload interface{}) *Request {
4259}
4360
4461func (r * Request ) Send () (* Response , error ) {
45- client := & http.Client {
46- Timeout : r .timeout ,
47- Transport : & http.Transport {
48- TLSClientConfig : & tls.Config {
49- InsecureSkipVerify : true ,
50- },
51- },
52- }
5362 params := ""
5463
5564 switch r .payload .(type ) {
@@ -79,7 +88,10 @@ func (r *Request) Send() (*Response, error) {
7988 return nil , err
8089 }
8190
82- request .Header .Set ("Content-Type" , r .contentType )
91+ if r .contentType != "" {
92+ request .Header .Set ("Content-Type" , r .contentType )
93+ }
94+
8395 for key , values := range r .header {
8496 for _ , value := range values {
8597 request .Header .Add (key , value )
@@ -88,6 +100,7 @@ func (r *Request) Send() (*Response, error) {
88100
89101 start := time .Now ()
90102
103+ client := r .getClient ()
91104 rep , err := client .Do (request )
92105 if err != nil {
93106 return nil , err
@@ -127,11 +140,6 @@ func (r *Request) Uri(uri string) *Request {
127140 return r
128141}
129142
130- func (r * Request ) Timeout (timeout time.Duration ) * Request {
131- r .timeout = timeout
132- return r
133- }
134-
135143func (r * Request ) Body (payload interface {}) * Request {
136144 r .payload = payload
137145 return r
@@ -152,3 +160,42 @@ func (r *Request) SetHeader(name string, value string) *Request {
152160 r .header .Set (name , value )
153161 return r
154162}
163+
164+ func (r * Request ) getClient () * http.Client {
165+ if r .client == nil {
166+ r .client = DefaultClient
167+ }
168+ return r .client
169+ }
170+
171+ // An Option configures a Request.
172+ type Option interface {
173+ Apply (* Request )
174+ }
175+
176+ // OptionFunc is a function that configures a Request.
177+ type OptionFunc func (* Request )
178+
179+ // Apply calls f(Request)
180+ func (f OptionFunc ) Apply (r * Request ) {
181+ f (r )
182+ }
183+
184+ // WithClient can be used to set the client of a Request to the given value.
185+ func WithClient (client * http.Client ) Option {
186+ return OptionFunc (func (r * Request ) {
187+ r .client = client
188+ })
189+ }
190+
191+ func WithTransport (transport http.RoundTripper ) Option {
192+ return OptionFunc (func (r * Request ) {
193+ r .getClient ().Transport = transport
194+ })
195+ }
196+
197+ func WithContentType (mime string ) Option {
198+ return OptionFunc (func (r * Request ) {
199+ r .ContentType (mime )
200+ })
201+ }
0 commit comments