@@ -5,6 +5,8 @@ import "net/http"
5
5
// Session allows a user to make use of persistent cookies in between
6
6
// HTTP requests
7
7
type Session struct {
8
+ // RequestOptions is global options
9
+ RequestOptions * RequestOptions
8
10
9
11
// HTTPClient is the client that we will use to request the resources
10
12
HTTPClient * http.Client
@@ -20,7 +22,42 @@ func NewSession(ro *RequestOptions) *Session {
20
22
21
23
ro .UseCookieJar = true
22
24
23
- return & Session {HTTPClient : BuildHTTPClient (* ro )}
25
+ return & Session {RequestOptions : ro , HTTPClient : BuildHTTPClient (* ro )}
26
+ }
27
+
28
+ // Combine session options and request options
29
+ // 1. UserAgent
30
+ // 2. Host
31
+ // 3. Auth
32
+ // 4. Headers
33
+ func (s * Session ) combineRequestOptions (ro * RequestOptions ) * RequestOptions {
34
+ if ro == nil {
35
+ ro = & RequestOptions {}
36
+ }
37
+
38
+ if ro .UserAgent == "" && s .RequestOptions .UserAgent != "" {
39
+ ro .UserAgent = s .RequestOptions .UserAgent
40
+ }
41
+
42
+ if ro .Host == "" && s .RequestOptions .Host != "" {
43
+ ro .Host = s .RequestOptions .Host
44
+ }
45
+
46
+ if ro .Auth == nil && s .RequestOptions .Auth != nil {
47
+ ro .Auth = s .RequestOptions .Auth
48
+ }
49
+
50
+ if len (s .RequestOptions .Headers ) > 0 || len (ro .Headers ) > 0 {
51
+ headers := make (map [string ]string )
52
+ for k , v := range s .RequestOptions .Headers {
53
+ headers [k ] = v
54
+ }
55
+ for k , v := range ro .Headers {
56
+ headers [k ] = v
57
+ }
58
+ ro .Headers = headers
59
+ }
60
+ return ro
24
61
}
25
62
26
63
// Get takes 2 parameters and returns a Response Struct. These two options are:
@@ -29,6 +66,7 @@ func NewSession(ro *RequestOptions) *Session {
29
66
// If you do not intend to use the `RequestOptions` you can just pass nil
30
67
// A new session is created by calling NewSession with a request options struct
31
68
func (s * Session ) Get (url string , ro * RequestOptions ) (* Response , error ) {
69
+ ro = s .combineRequestOptions (ro )
32
70
return doSessionRequest ("GET" , url , ro , s .HTTPClient )
33
71
}
34
72
@@ -38,6 +76,7 @@ func (s *Session) Get(url string, ro *RequestOptions) (*Response, error) {
38
76
// If you do not intend to use the `RequestOptions` you can just pass nil
39
77
// A new session is created by calling NewSession with a request options struct
40
78
func (s * Session ) Put (url string , ro * RequestOptions ) (* Response , error ) {
79
+ ro = s .combineRequestOptions (ro )
41
80
return doSessionRequest ("PUT" , url , ro , s .HTTPClient )
42
81
}
43
82
@@ -47,6 +86,7 @@ func (s *Session) Put(url string, ro *RequestOptions) (*Response, error) {
47
86
// If you do not intend to use the `RequestOptions` you can just pass nil
48
87
// A new session is created by calling NewSession with a request options struct
49
88
func (s * Session ) Patch (url string , ro * RequestOptions ) (* Response , error ) {
89
+ ro = s .combineRequestOptions (ro )
50
90
return doSessionRequest ("PATCH" , url , ro , s .HTTPClient )
51
91
}
52
92
@@ -56,6 +96,7 @@ func (s *Session) Patch(url string, ro *RequestOptions) (*Response, error) {
56
96
// If you do not intend to use the `RequestOptions` you can just pass nil
57
97
// A new session is created by calling NewSession with a request options struct
58
98
func (s * Session ) Delete (url string , ro * RequestOptions ) (* Response , error ) {
99
+ ro = s .combineRequestOptions (ro )
59
100
return doSessionRequest ("DELETE" , url , ro , s .HTTPClient )
60
101
}
61
102
@@ -65,6 +106,7 @@ func (s *Session) Delete(url string, ro *RequestOptions) (*Response, error) {
65
106
// If you do not intend to use the `RequestOptions` you can just pass nil
66
107
// A new session is created by calling NewSession with a request options struct
67
108
func (s * Session ) Post (url string , ro * RequestOptions ) (* Response , error ) {
109
+ ro = s .combineRequestOptions (ro )
68
110
return doSessionRequest ("POST" , url , ro , s .HTTPClient )
69
111
}
70
112
@@ -74,6 +116,7 @@ func (s *Session) Post(url string, ro *RequestOptions) (*Response, error) {
74
116
// If you do not intend to use the `RequestOptions` you can just pass nil
75
117
// A new session is created by calling NewSession with a request options struct
76
118
func (s * Session ) Head (url string , ro * RequestOptions ) (* Response , error ) {
119
+ ro = s .combineRequestOptions (ro )
77
120
return doSessionRequest ("HEAD" , url , ro , s .HTTPClient )
78
121
}
79
122
@@ -83,6 +126,7 @@ func (s *Session) Head(url string, ro *RequestOptions) (*Response, error) {
83
126
// If you do not intend to use the `RequestOptions` you can just pass nil
84
127
// A new session is created by calling NewSession with a request options struct
85
128
func (s * Session ) Options (url string , ro * RequestOptions ) (* Response , error ) {
129
+ ro = s .combineRequestOptions (ro )
86
130
return doSessionRequest ("OPTIONS" , url , ro , s .HTTPClient )
87
131
}
88
132
0 commit comments