44class SplitTableMixin (object ):
55 UPDATE_PROJECTS_TIME = 10 * 60
66
7+ def __init__ (self ):
8+ self .session = requests .session ()
9+ if self .username :
10+ self .session .auth = HTTPBasicAuth (self .username , self .password )
11+ self .session .headers .update ({'Content-Type' : 'application/json' })
12+
713 def _collection_name (self , project ):
814 if self .collection_prefix :
915 return "%s_%s" % (self .collection_prefix , project )
@@ -32,10 +38,7 @@ def _list_project(self):
3238 prefix = ''
3339
3440 url = self .base_url + "_all_dbs"
35- res = requests .get (url ,
36- data = json .dumps ({}),
37- headers = {"Content-Type" : "application/json" },
38- auth = HTTPBasicAuth (self .username , self .password )).json ()
41+ res = self .session .get (url , json = {}).json ()
3942 for each in res :
4043 if each .startswith ('_' ):
4144 continue
@@ -45,19 +48,15 @@ def _list_project(self):
4548
4649 def create_database (self , name ):
4750 url = self .base_url + name
48- res = requests .put (url ,
49- headers = {"Content-Type" : "application/json" },
50- auth = HTTPBasicAuth (self .username , self .password )).json ()
51+ res = self .session .put (url ).json ()
5152 if 'error' in res and res ['error' ] == 'unauthorized' :
5253 raise Exception ("Supplied credentials are incorrect. Reason: {} for User: {} Password: {}" .format (res ['reason' ], self .username , self .password ))
5354 return res
5455
5556
5657 def get_doc (self , db_name , doc_id ):
5758 url = self .base_url + db_name + "/" + doc_id
58- res = requests .get (url ,
59- headers = {"Content-Type" : "application/json" },
60- auth = HTTPBasicAuth (self .username , self .password )).json ()
59+ res = self .session .get (url ).json ()
6160 if "error" in res and res ["error" ] == "not_found" :
6261 return None
6362 return res
@@ -66,10 +65,7 @@ def get_doc(self, db_name, doc_id):
6665 def get_docs (self , db_name , selector ):
6766 url = self .base_url + db_name + "/_find"
6867 selector ['use_index' ] = self .index
69- res = requests .post (url ,
70- data = json .dumps (selector ),
71- headers = {"Content-Type" : "application/json" },
72- auth = HTTPBasicAuth (self .username , self .password )).json ()
68+ res = self .session .post (url , json = selector ).json ()
7369 if 'error' in res and res ['error' ] == 'not_found' :
7470 return []
7571 return res ['docs' ]
@@ -81,10 +77,7 @@ def get_all_docs(self, db_name):
8177
8278 def insert_doc (self , db_name , doc_id , doc ):
8379 url = self .base_url + db_name + "/" + doc_id
84- return requests .put (url ,
85- data = json .dumps (doc ),
86- headers = {"Content-Type" : "application/json" },
87- auth = HTTPBasicAuth (self .username , self .password )).json ()
80+ return self .session .put (url , json = doc ).json ()
8881
8982
9083 def update_doc (self , db_name , doc_id , new_doc ):
@@ -94,14 +87,9 @@ def update_doc(self, db_name, doc_id, new_doc):
9487 for key in new_doc :
9588 doc [key ] = new_doc [key ]
9689 url = self .base_url + db_name + "/" + doc_id
97- return requests .put (url ,
98- data = json .dumps (doc ),
99- headers = {"Content-Type" : "application/json" },
100- auth = HTTPBasicAuth (self .username , self .password )).json ()
90+ return self .session .put (url , json = doc ).json ()
10191
10292
10393 def delete (self , url ):
104- return requests .delete (url ,
105- headers = {"Content-Type" : "application/json" },
106- auth = HTTPBasicAuth (self .username , self .password )).json ()
94+ return self .session .delete (url ).json ()
10795
0 commit comments