Skip to content

Commit c618986

Browse files
committed
Improvements for gogdb content system update
1 parent a21b2df commit c618986

File tree

8 files changed

+443
-114
lines changed

8 files changed

+443
-114
lines changed

examples/externauth.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import webbrowser
22
import re
33

4-
from gogapi.token import Token, get_auth_url
4+
from gogapi import Token, get_auth_url
55

66

77
LOGIN_INSTRUCTIONS = """\

gogapi/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import gogapi.names as names
2+
13
from gogapi.token import get_auth_url, Token
24
from gogapi.api import GogApi
35
from gogapi.base import (

gogapi/api.py

+19-32
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"related_products", "changelog"
2424
]
2525
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"]
2727
CURRENCY_CODES = [
2828
"USD", "EUR", "GBP", "AUD", "RUB", "PLN", "CAD", "CHF", "NOK", "SEK", "DKK"
2929
]
@@ -56,6 +56,9 @@ class GogApi:
5656
def __init__(self, token=None):
5757
self.token = token
5858
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
5962

6063
# Helpers
6164

@@ -65,40 +68,22 @@ def request(self, method, url, authorized=True, allow_redirects=False,
6568
Wrapper around requests.request that also handles authorization,
6669
retries and logging
6770
"""
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()
7375
if self.token.expired():
7476
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
9379
else:
94-
logger.debug("%s %s", method, url)
80+
self.session.headers.pop("Authorization", None)
9581

9682
# Retries
9783
retries = REQUEST_RETRIES
9884
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)
10287
if resp.status_code < 400:
10388
return resp
10489
elif 400 <= resp.status_code < 500:
@@ -130,13 +115,13 @@ def request_json(self, *args, compressed=False, **kwargs):
130115
resp = self.request(*args, **kwargs)
131116
if not compressed:
132117
if DEBUG_JSON:
133-
logger.debug(resp.text)
118+
print(resp.text)
134119
return resp.json()
135120
else:
136121
json_comp = resp.content
137122
json_text = zlib.decompress(json_comp, 15).decode("utf-8")
138123
if DEBUG_JSON:
139-
logger.debug(json_text)
124+
print(json_text)
140125
return json.loads(json_text)
141126

142127
def get_json(self, *args, **kwargs):
@@ -178,8 +163,10 @@ def set_locale(self, country, currency, locale):
178163
return AttributeError("Invalid currency code {}".format(locale))
179164
elif locale not in LOCALE_CODES:
180165
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+
183170

184171
# Web APIs
185172

0 commit comments

Comments
 (0)