aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornico <nico@magicbroccoli.de>2020-03-06 12:43:15 +0100
committernico <nico@magicbroccoli.de>2020-03-06 12:43:15 +0100
commitc8d88a537d3afdc10618e92ea63dc353a3250058 (patch)
tree63e65831a7921586de339fdfd060450a0235dc5c
parent95616508ceb29303a87b71544c3b36929c0bd3d2 (diff)
better session handeling
* improved session handeling Due to the with statement in the session creation process, every requests to the api created a new session. This patch decreases the enormous TCP overhead, by creating a single global session to reuse.
-rwxr-xr-xejabberdrpc.py21
1 files changed, 14 insertions, 7 deletions
diff --git a/ejabberdrpc.py b/ejabberdrpc.py
index e89c14a..7de19e6 100755
--- a/ejabberdrpc.py
+++ b/ejabberdrpc.py
@@ -20,7 +20,10 @@ class EjabberdMetrics:
self.url = url
self._cmd = self._rpc
else:
+ import requests
+
self._url = url
+ self.session = requests.Session()
self._cmd = self._rest
@property
@@ -43,15 +46,19 @@ class EjabberdMetrics:
return None
- def _rest(self, command: str, data):
- import requests
+ def _rest(self, command: str, data) -> dict:
+ # add authentication header to the session obj
+ if self.session.auth is None:
+ self.session.auth = self._auth
+
+ # post
+ r = self.session.post('/'.join([self._url, command]), json=data)
- with requests.Session() as s:
- r = s.post(f'{self._url}/{command}', auth=self._auth, json=data)
+ # proceed if response is ok
+ if r.ok:
+ return r.json()
- if r.status_code == 200:
- return r.json()
- return{}
+ return {}
def _rpc(self, command: str, data):
from xmlrpc import client