23
23
"related_products" , "changelog"
24
24
]
25
25
USER_EXPANDABLE = ["friendStatus" , "wishlistStatus" , "blockedStatus" ]
26
- LOCALE_CODES = ["de-DE" , "en-US" , "fr-FR" , "pt-BR" , "ru-RU" , "zh-Hans" ]
26
+ LOCALE_CODES = ["de-DE" , "en-US" , "fr-FR" , "pt-BR" , "pl-PL" , " ru-RU" , "zh-Hans" ]
27
27
CURRENCY_CODES = [
28
28
"USD" , "EUR" , "GBP" , "AUD" , "RUB" , "PLN" , "CAD" , "CHF" , "NOK" , "SEK" , "DKK"
29
29
]
@@ -56,6 +56,9 @@ class GogApi:
56
56
def __init__ (self , token = None ):
57
57
self .token = token
58
58
self .locale = (None , None , None ) # TODO: replace tuple
59
+ self .session = requests .Session ()
60
+ self .session .headers ["User-Agent" ] = USER_AGENT
61
+ self .force_authorize = False
59
62
60
63
# Helpers
61
64
@@ -65,40 +68,22 @@ def request(self, method, url, authorized=True, allow_redirects=False,
65
68
Wrapper around requests.request that also handles authorization,
66
69
retries and logging
67
70
"""
68
- # Set headers
69
- # Prevent getting blocked by default
70
- headers = {"User-Agent" : USER_AGENT }
71
- # Add a token to the request if it exists
72
- if self .token is not None :
71
+
72
+ if authorized or self .force_authorize :
73
+ if self .token is None :
74
+ raise NotAuthorizedError ()
73
75
if self .token .expired ():
74
76
self .token .refresh ()
75
- headers ["Authorization" ] = "Bearer " + self .token .access_token
76
- elif authorized :
77
- raise NotAuthorizedError ()
78
-
79
- headers .update (kwargs .pop ("headers" , {}))
80
-
81
- # Set cookies
82
- cookies = {}
83
- if all (self .locale ):
84
- cookies ["gog_lc" ] = "_" .join (self .locale )
85
- cookies .update (kwargs .pop ("cookies" , {}))
86
-
87
- # Log request
88
- if "params" in kwargs :
89
- full_url = url + "?" + "&" .join (
90
- str (key ) + "=" + str (value )
91
- for key , value in kwargs ["params" ].items ())
92
- logger .debug ("%s %s" , method , full_url )
77
+ self .session .headers ["Authorization" ] = \
78
+ "Bearer " + self .token .access_token
93
79
else :
94
- logger . debug ( "%s %s " , method , url )
80
+ self . session . headers . pop ( "Authorization " , None )
95
81
96
82
# Retries
97
83
retries = REQUEST_RETRIES
98
84
while retries > 0 :
99
- resp = requests .request (
100
- method , url , headers = headers , cookies = cookies ,
101
- allow_redirects = allow_redirects , ** kwargs )
85
+ resp = self .session .request (
86
+ method , url , allow_redirects = allow_redirects , ** kwargs )
102
87
if resp .status_code < 400 :
103
88
return resp
104
89
elif 400 <= resp .status_code < 500 :
@@ -130,13 +115,13 @@ def request_json(self, *args, compressed=False, **kwargs):
130
115
resp = self .request (* args , ** kwargs )
131
116
if not compressed :
132
117
if DEBUG_JSON :
133
- logger . debug (resp .text )
118
+ print (resp .text )
134
119
return resp .json ()
135
120
else :
136
121
json_comp = resp .content
137
122
json_text = zlib .decompress (json_comp , 15 ).decode ("utf-8" )
138
123
if DEBUG_JSON :
139
- logger . debug (json_text )
124
+ print (json_text )
140
125
return json .loads (json_text )
141
126
142
127
def get_json (self , * args , ** kwargs ):
@@ -178,8 +163,10 @@ def set_locale(self, country, currency, locale):
178
163
return AttributeError ("Invalid currency code {}" .format (locale ))
179
164
elif locale not in LOCALE_CODES :
180
165
return AttributeError ("Invalid locale code {}" .format (locale ))
181
- else :
182
- self .locale = (country , currency , locale )
166
+
167
+ self .locale = (country , currency , locale )
168
+ self .session .cookies ["gog_lc" ] = "_" .join (self .locale )
169
+
183
170
184
171
# Web APIs
185
172
0 commit comments