@@ -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