Skip to content

Commit 908f194

Browse files
authored
Merge pull request #40 from skystrife/develop
fix(dev): Use requests.Session object for updating cookies
2 parents 0976a43 + 47efca0 commit 908f194

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

piazza_api/network.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ class Network(object):
4747
"""Abstraction for a Piazza "Network" (or class)
4848
4949
:param network_id: ID of the network
50-
:param cookies: RequestsCookieJar containing cookies used for authentication
50+
:param session: requests.Session object containing cookies used for
51+
authentication
5152
"""
52-
def __init__(self, network_id, cookies):
53+
def __init__(self, network_id, session):
5354
self._nid = network_id
5455
self._rpc = PiazzaRPC(network_id=self._nid)
55-
self._rpc.cookies = cookies
56+
self._rpc.session = session
5657

5758
ff = namedtuple('FeedFilters', ['unread', 'following', 'folder'])
5859
self._feed_filters = ff(UnreadFilter, FollowingFilter, FolderFilter)

piazza_api/piazza.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def network(self, network_id):
4343
https://piazza.com/class/{network_id}
4444
"""
4545
self._ensure_authenticated()
46-
return Network(network_id, self._rpc_api.cookies)
46+
return Network(network_id, self._rpc_api.session)
4747

4848
def get_user_profile(self):
4949
"""Get profile of the current user

piazza_api/rpc.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self, network_id=None):
3131
"logic": "https://piazza.com/logic/api",
3232
"main": "https://piazza.com/main/api",
3333
}
34-
self.cookies = None
34+
self.session = requests.Session()
3535

3636
def user_login(self, email=None, password=None):
3737
"""Login with email, password and get back a session cookie
@@ -51,14 +51,13 @@ def user_login(self, email=None, password=None):
5151
}
5252
# If the user/password match, the server respond will contain a
5353
# session cookie that you can use to authenticate future requests.
54-
r = requests.post(
54+
r = self.session.post(
5555
self.base_api_urls["logic"],
5656
data=json.dumps(login_data),
5757
)
5858
if r.json()["result"] not in ["OK"]:
5959
raise AuthenticationError("Could not authenticate.\n{}"
6060
.format(r.json()))
61-
self.cookies = r.cookies
6261

6362
def demo_login(self, auth=None, url=None):
6463
"""Authenticate with a "Share Your Class" URL using a demo user.
@@ -76,10 +75,9 @@ def demo_login(self, auth=None, url=None):
7675
if url is None:
7776
url = "https://piazza.com/demo_login"
7877
params = dict(nid=self._nid, auth=auth)
79-
res = requests.get(url, params=params)
78+
res = self.session.get(url, params=params)
8079
else:
81-
res = requests.get(url)
82-
self.cookies = res.cookies
80+
res = self.session.get(url)
8381

8482
def content_get(self, cid, nid=None):
8583
"""Get data from post `cid` in network `nid`
@@ -379,9 +377,9 @@ def request(self, method, data=None, nid=None, nid_key='nid',
379377
data = {}
380378

381379
headers = {}
382-
if "session_id" in self.cookies:
383-
headers["CSRF-Token"] = self.cookies["session_id"]
384-
380+
if "session_id" in self.session.cookies:
381+
headers["CSRF-Token"] = self.session.cookies["session_id"]
382+
385383
# Adding a nonce to the request
386384
endpoint = self.base_api_urls[api_type]
387385
if api_type == "logic":
@@ -390,13 +388,12 @@ def request(self, method, data=None, nid=None, nid_key='nid',
390388
_piazza_nonce()
391389
)
392390

393-
response = requests.post(
391+
response = self.session.post(
394392
endpoint,
395393
data=json.dumps({
396394
"method": method,
397395
"params": dict({nid_key: nid}, **data)
398396
}),
399-
cookies=self.cookies,
400397
headers=headers
401398
)
402399
return response if return_response else response.json()
@@ -410,7 +407,7 @@ def _check_authenticated(self):
410407
411408
:raises: NotAuthenticatedError
412409
"""
413-
if self.cookies is None:
410+
if not self.session.cookies:
414411
raise NotAuthenticatedError("You must authenticate before "
415412
"making any other requests.")
416413

0 commit comments

Comments
 (0)