Skip to content

Commit 5711f18

Browse files
committed
Fix global cookie storage
1 parent f589586 commit 5711f18

6 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎generate.sh‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ openapi-generator-cli generate \
88
--git-repo-id=vrchatapi-python \
99
-o . \
1010
-i https://vrchatapi.github.io/specification/openapi.yaml \
11-
--http-user-agent="vrchatapi-python"
11+
--http-user-agent="vrchatapi-py"
12+
13+
# Enable Global cookies
14+
sed -i '/cert_reqs = ssl.CERT_NONE/r patches/cookiejar_init.py' ./vrchatapi/rest.py
15+
sed -i '/headers = headers or {}/r patches/cookiejar_add.py' ./vrchatapi/rest.py
16+
17+
sed -i 's/if _preload_content/abcdefvrc\n\n if _preload_content/g' ./vrchatapi/rest.py
18+
sed -i '/abcdefvrc/r patches/cookiejar_extract.py' ./vrchatapi/rest.py
19+
sed -i 's/ abcdefvrc//g' ./vrchatapi/rest.py

‎patches/cookiejar_add.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
# VRChatAPI: Build a mock Request object to work with
3+
from urllib.request import Request
4+
mock_request_object = Request(url=url, method=method, headers=headers)
5+
self.cookie_jar.add_cookie_header(mock_request_object)
6+
if "Cookie" in mock_request_object.unredirected_hdrs:
7+
headers["Cookie"] = mock_request_object.unredirected_hdrs["Cookie"]

‎patches/cookiejar_extract.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# VRChatAPI: Extract and save cookies for global storage
2+
self.cookie_jar.extract_cookies(r, mock_request_object)

‎patches/cookiejar_init.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# VRChatAPI: Init global cookie storage
3+
from http.cookiejar import CookieJar
4+
self.cookie_jar = CookieJar()

‎vrchatapi/api_client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
7777
self.default_headers[header_name] = header_value
7878
self.cookie = cookie
7979
# Set default User-Agent.
80-
self.user_agent = 'vrchatapi-python'
80+
self.user_agent = 'vrchatapi-py'
8181

8282
def __enter__(self):
8383
return self

‎vrchatapi/rest.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def __init__(self, configuration, pools_size=4, maxsize=None):
5656
else:
5757
cert_reqs = ssl.CERT_NONE
5858

59+
# VRChatAPI: Init global cookie storage
60+
from http.cookiejar import CookieJar
61+
self.cookie_jar = CookieJar()
62+
5963
addition_pool_args = {}
6064
if configuration.assert_hostname is not None:
6165
addition_pool_args['assert_hostname'] = configuration.assert_hostname # noqa: E501
@@ -129,6 +133,13 @@ def request(self, method, url, query_params=None, headers=None,
129133
post_params = post_params or {}
130134
headers = headers or {}
131135

136+
# VRChatAPI: Build a mock Request object to work with
137+
from urllib.request import Request
138+
mock_request_object = Request(url=url, method=method, headers=headers)
139+
self.cookie_jar.add_cookie_header(mock_request_object)
140+
if "Cookie" in mock_request_object.unredirected_hdrs:
141+
headers["Cookie"] = mock_request_object.unredirected_hdrs["Cookie"]
142+
132143
timeout = None
133144
if _request_timeout:
134145
if isinstance(_request_timeout, (int, float)): # noqa: E501,F821
@@ -204,6 +215,10 @@ def request(self, method, url, query_params=None, headers=None,
204215
msg = "{0}\n{1}".format(type(e).__name__, str(e))
205216
raise ApiException(status=0, reason=msg)
206217

218+
219+
# VRChatAPI: Extract and save cookies for global storage
220+
self.cookie_jar.extract_cookies(r, mock_request_object)
221+
207222
if _preload_content:
208223
r = RESTResponse(r)
209224

0 commit comments

Comments
 (0)