-
Notifications
You must be signed in to change notification settings - Fork 137
/
session.go
136 lines (117 loc) · 4.73 KB
/
session.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package grequests
import "net/http"
// Session allows a user to make use of persistent cookies in between
// HTTP requests
type Session struct {
// RequestOptions is global options
RequestOptions *RequestOptions
// HTTPClient is the client that we will use to request the resources
HTTPClient *http.Client
}
// NewSession returns a session struct which enables can be used to maintain establish a persistent state with the
// server
// This function will set UseCookieJar to true as that is the purpose of using the session
func NewSession(ro *RequestOptions) *Session {
if ro == nil {
ro = &RequestOptions{}
}
ro.UseCookieJar = true
return &Session{RequestOptions: ro, HTTPClient: BuildHTTPClient(*ro)}
}
// Combine session options and request options
// 1. UserAgent
// 2. Host
// 3. Auth
// 4. Headers
func (s *Session) combineRequestOptions(ro *RequestOptions) *RequestOptions {
if ro == nil {
ro = &RequestOptions{}
}
if ro.UserAgent == "" && s.RequestOptions.UserAgent != "" {
ro.UserAgent = s.RequestOptions.UserAgent
}
if ro.Host == "" && s.RequestOptions.Host != "" {
ro.Host = s.RequestOptions.Host
}
if ro.Auth == nil && s.RequestOptions.Auth != nil {
ro.Auth = s.RequestOptions.Auth
}
if len(s.RequestOptions.Headers) > 0 || len(ro.Headers) > 0 {
headers := make(map[string]string)
for k, v := range s.RequestOptions.Headers {
headers[k] = v
}
for k, v := range ro.Headers {
headers[k] = v
}
ro.Headers = headers
}
return ro
}
// Get takes 2 parameters and returns a Response Struct. These two options are:
// 1. A URL
// 2. A RequestOptions struct
// If you do not intend to use the `RequestOptions` you can just pass nil
// A new session is created by calling NewSession with a request options struct
func (s *Session) Get(url string, ro *RequestOptions) (*Response, error) {
ro = s.combineRequestOptions(ro)
return doSessionRequest("GET", url, ro, s.HTTPClient)
}
// Put takes 2 parameters and returns a Response struct. These two options are:
// 1. A URL
// 2. A RequestOptions struct
// If you do not intend to use the `RequestOptions` you can just pass nil
// A new session is created by calling NewSession with a request options struct
func (s *Session) Put(url string, ro *RequestOptions) (*Response, error) {
ro = s.combineRequestOptions(ro)
return doSessionRequest("PUT", url, ro, s.HTTPClient)
}
// Patch takes 2 parameters and returns a Response struct. These two options are:
// 1. A URL
// 2. A RequestOptions struct
// If you do not intend to use the `RequestOptions` you can just pass nil
// A new session is created by calling NewSession with a request options struct
func (s *Session) Patch(url string, ro *RequestOptions) (*Response, error) {
ro = s.combineRequestOptions(ro)
return doSessionRequest("PATCH", url, ro, s.HTTPClient)
}
// Delete takes 2 parameters and returns a Response struct. These two options are:
// 1. A URL
// 2. A RequestOptions struct
// If you do not intend to use the `RequestOptions` you can just pass nil
// A new session is created by calling NewSession with a request options struct
func (s *Session) Delete(url string, ro *RequestOptions) (*Response, error) {
ro = s.combineRequestOptions(ro)
return doSessionRequest("DELETE", url, ro, s.HTTPClient)
}
// Post takes 2 parameters and returns a Response channel. These two options are:
// 1. A URL
// 2. A RequestOptions struct
// If you do not intend to use the `RequestOptions` you can just pass nil
// A new session is created by calling NewSession with a request options struct
func (s *Session) Post(url string, ro *RequestOptions) (*Response, error) {
ro = s.combineRequestOptions(ro)
return doSessionRequest("POST", url, ro, s.HTTPClient)
}
// Head takes 2 parameters and returns a Response channel. These two options are:
// 1. A URL
// 2. A RequestOptions struct
// If you do not intend to use the `RequestOptions` you can just pass nil
// A new session is created by calling NewSession with a request options struct
func (s *Session) Head(url string, ro *RequestOptions) (*Response, error) {
ro = s.combineRequestOptions(ro)
return doSessionRequest("HEAD", url, ro, s.HTTPClient)
}
// Options takes 2 parameters and returns a Response struct. These two options are:
// 1. A URL
// 2. A RequestOptions struct
// If you do not intend to use the `RequestOptions` you can just pass nil
// A new session is created by calling NewSession with a request options struct
func (s *Session) Options(url string, ro *RequestOptions) (*Response, error) {
ro = s.combineRequestOptions(ro)
return doSessionRequest("OPTIONS", url, ro, s.HTTPClient)
}
// CloseIdleConnections closes the idle connections that a session client may make use of
func (s *Session) CloseIdleConnections() {
s.HTTPClient.Transport.(*http.Transport).CloseIdleConnections()
}