Skip to content

Commit 5425509

Browse files
zwczoulevigross
authored andcommitted
support global options within sessions (levigross#42)
1 parent ee73e84 commit 5425509

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

session.go

+45-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import "net/http"
55
// Session allows a user to make use of persistent cookies in between
66
// HTTP requests
77
type Session struct {
8+
// RequestOptions is global options
9+
RequestOptions *RequestOptions
810

911
// HTTPClient is the client that we will use to request the resources
1012
HTTPClient *http.Client
@@ -20,7 +22,42 @@ func NewSession(ro *RequestOptions) *Session {
2022

2123
ro.UseCookieJar = true
2224

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
2461
}
2562

2663
// Get takes 2 parameters and returns a Response Struct. These two options are:
@@ -29,6 +66,7 @@ func NewSession(ro *RequestOptions) *Session {
2966
// If you do not intend to use the `RequestOptions` you can just pass nil
3067
// A new session is created by calling NewSession with a request options struct
3168
func (s *Session) Get(url string, ro *RequestOptions) (*Response, error) {
69+
ro = s.combineRequestOptions(ro)
3270
return doSessionRequest("GET", url, ro, s.HTTPClient)
3371
}
3472

@@ -38,6 +76,7 @@ func (s *Session) Get(url string, ro *RequestOptions) (*Response, error) {
3876
// If you do not intend to use the `RequestOptions` you can just pass nil
3977
// A new session is created by calling NewSession with a request options struct
4078
func (s *Session) Put(url string, ro *RequestOptions) (*Response, error) {
79+
ro = s.combineRequestOptions(ro)
4180
return doSessionRequest("PUT", url, ro, s.HTTPClient)
4281
}
4382

@@ -47,6 +86,7 @@ func (s *Session) Put(url string, ro *RequestOptions) (*Response, error) {
4786
// If you do not intend to use the `RequestOptions` you can just pass nil
4887
// A new session is created by calling NewSession with a request options struct
4988
func (s *Session) Patch(url string, ro *RequestOptions) (*Response, error) {
89+
ro = s.combineRequestOptions(ro)
5090
return doSessionRequest("PATCH", url, ro, s.HTTPClient)
5191
}
5292

@@ -56,6 +96,7 @@ func (s *Session) Patch(url string, ro *RequestOptions) (*Response, error) {
5696
// If you do not intend to use the `RequestOptions` you can just pass nil
5797
// A new session is created by calling NewSession with a request options struct
5898
func (s *Session) Delete(url string, ro *RequestOptions) (*Response, error) {
99+
ro = s.combineRequestOptions(ro)
59100
return doSessionRequest("DELETE", url, ro, s.HTTPClient)
60101
}
61102

@@ -65,6 +106,7 @@ func (s *Session) Delete(url string, ro *RequestOptions) (*Response, error) {
65106
// If you do not intend to use the `RequestOptions` you can just pass nil
66107
// A new session is created by calling NewSession with a request options struct
67108
func (s *Session) Post(url string, ro *RequestOptions) (*Response, error) {
109+
ro = s.combineRequestOptions(ro)
68110
return doSessionRequest("POST", url, ro, s.HTTPClient)
69111
}
70112

@@ -74,6 +116,7 @@ func (s *Session) Post(url string, ro *RequestOptions) (*Response, error) {
74116
// If you do not intend to use the `RequestOptions` you can just pass nil
75117
// A new session is created by calling NewSession with a request options struct
76118
func (s *Session) Head(url string, ro *RequestOptions) (*Response, error) {
119+
ro = s.combineRequestOptions(ro)
77120
return doSessionRequest("HEAD", url, ro, s.HTTPClient)
78121
}
79122

@@ -83,6 +126,7 @@ func (s *Session) Head(url string, ro *RequestOptions) (*Response, error) {
83126
// If you do not intend to use the `RequestOptions` you can just pass nil
84127
// A new session is created by calling NewSession with a request options struct
85128
func (s *Session) Options(url string, ro *RequestOptions) (*Response, error) {
129+
ro = s.combineRequestOptions(ro)
86130
return doSessionRequest("OPTIONS", url, ro, s.HTTPClient)
87131
}
88132

0 commit comments

Comments
 (0)