4
4
class SplitTableMixin (object ):
5
5
UPDATE_PROJECTS_TIME = 10 * 60
6
6
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
+
7
13
def _collection_name (self , project ):
8
14
if self .collection_prefix :
9
15
return "%s_%s" % (self .collection_prefix , project )
@@ -32,10 +38,7 @@ def _list_project(self):
32
38
prefix = ''
33
39
34
40
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 ()
39
42
for each in res :
40
43
if each .startswith ('_' ):
41
44
continue
@@ -45,19 +48,15 @@ def _list_project(self):
45
48
46
49
def create_database (self , name ):
47
50
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 ()
51
52
if 'error' in res and res ['error' ] == 'unauthorized' :
52
53
raise Exception ("Supplied credentials are incorrect. Reason: {} for User: {} Password: {}" .format (res ['reason' ], self .username , self .password ))
53
54
return res
54
55
55
56
56
57
def get_doc (self , db_name , doc_id ):
57
58
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 ()
61
60
if "error" in res and res ["error" ] == "not_found" :
62
61
return None
63
62
return res
@@ -66,10 +65,7 @@ def get_doc(self, db_name, doc_id):
66
65
def get_docs (self , db_name , selector ):
67
66
url = self .base_url + db_name + "/_find"
68
67
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 ()
73
69
if 'error' in res and res ['error' ] == 'not_found' :
74
70
return []
75
71
return res ['docs' ]
@@ -81,10 +77,7 @@ def get_all_docs(self, db_name):
81
77
82
78
def insert_doc (self , db_name , doc_id , doc ):
83
79
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 ()
88
81
89
82
90
83
def update_doc (self , db_name , doc_id , new_doc ):
@@ -94,14 +87,9 @@ def update_doc(self, db_name, doc_id, new_doc):
94
87
for key in new_doc :
95
88
doc [key ] = new_doc [key ]
96
89
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 ()
101
91
102
92
103
93
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 ()
107
95
0 commit comments