forked from freman/go-steamauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
steamweb.go
176 lines (149 loc) · 4.34 KB
/
steamweb.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2015 Shannon Wynter. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package steamauth
import (
"encoding/json"
"errors"
"io"
"net/http"
"strings"
)
import "net/url"
type responseHandlerFunc func(*http.Response) error
type steamWeb struct {
*http.Client
headers http.Header
params url.Values
urlStr string
method string
oV interface{}
oF responseHandlerFunc
}
// SteamWeb returns a convenient chainable steamWeb object that allows
// you to perform requests against the steam API with a simple sequence
// of method calls.
func SteamWeb() *steamWeb {
return &steamWeb{
Client: &http.Client{},
headers: http.Header{
"User-Agent": []string{"Mozilla/5.0 (Linux; U; Android 4.1.1; en-us; Google Nexus 4 - 4.1.1 - API 16 - 768x1280 Build/JRO03S) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"},
"Accept": []string{"text/javascript, text/html, application/xml, text/xml, */*"},
},
params: url.Values{},
method: "GET",
}
}
func (s *steamWeb) newRequest() (*http.Request, error) {
var body io.Reader
urlStr := s.urlStr
if len(s.params) > 0 {
switch s.method {
case "GET":
query := s.params.Encode()
if strings.Contains(urlStr, "?") {
urlStr = "&" + query
} else {
urlStr = "?" + query
}
case "POST":
body = strings.NewReader(s.params.Encode())
s.headers.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
}
}
req, err := http.NewRequest(s.method, urlStr, body)
if err != nil {
return nil, err
}
req.Header = s.headers
return req, nil
}
// SetJar set this requests cookiejar
func (s *steamWeb) SetJar(cookieJar http.CookieJar) *steamWeb {
s.Jar = cookieJar
return s
}
// SetJarFromUser set this request's cookiejar from an already
// authenticated user via UserLogin.
func (s *steamWeb) SetJarFromUser(user *UserLogin) *steamWeb {
s.Jar = user.cookieJar
return s
}
// SetHeaders set this requests headers
func (s *steamWeb) SetHeaders(headers http.Header) *steamWeb {
s.headers = headers
return s
}
// SetReferrer is a shortcut method to set the referrer header
// in this request, don't use `SetHeaders` after you call this
// method or it'll be overwritten
func (s *steamWeb) SetReferrer(referrer string) *steamWeb {
s.headers.Set("Referrer", referrer)
return s
}
// AddHeader add a value to the headers of this request
func (s *steamWeb) AddHeader(name, value string) *steamWeb {
s.headers.Add(name, value)
return s
}
// SetHeader set a header for this request
func (s *steamWeb) SetHeader(name, value string) *steamWeb {
s.headers.Set(name, value)
return s
}
// SetParams sets the `POST` form params, or adds query
// parameters to a `GET` request
func (s *steamWeb) SetParams(data url.Values) *steamWeb {
s.params = data
return s
}
// Post allows you to prepare a post request for a given url
func (s *steamWeb) Post(urlStr string) *steamWeb {
s.urlStr = urlStr
s.method = "POST"
return s
}
// Get allows you to prepare a get request for a given url
func (s *steamWeb) Get(urlStr string) *steamWeb {
s.urlStr = urlStr
s.method = "GET"
return s
}
// HandleJSON will configure the request parse a json response
// into the given `v` interface
func (s *steamWeb) HandleJSON(v interface{}) *steamWeb {
s.oV = v
s.oF = s.handleJSON
return s
}
// Do the request, execute it then do any post processing
func (s *steamWeb) Do() (*http.Response, error) {
req, err := s.newRequest()
if err == nil {
logRequest(req)
logCookies(s, req)
resp, err := s.Client.Do(req)
logResponse(resp)
// Ouput format the content via the handle function...
if err == nil && s.oF != nil {
err = s.oF(resp)
}
return resp, err
}
return nil, err
}
// MobileLoginRequest is a shotcut method that sets the referrer
// before executing the request
func (s *steamWeb) MobileLoginRequest() (*http.Response, error) {
return s.
SetReferrer(APIEndpoints.CommunityBase.String() + "/mobilelogin?oauth_client_id=DE45CD61&oauth_scope=read_profile%20write_profile%20read_client%20write_client").
Do()
}
func (s *steamWeb) handleJSON(r *http.Response) error {
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
return errors.New("incorrect content type, expecting application/json")
}
defer r.Body.Close()
decoder := json.NewDecoder(r.Body)
return decoder.Decode(s.oV)
}