-
Notifications
You must be signed in to change notification settings - Fork 0
/
arena_api.py
46 lines (41 loc) · 1.6 KB
/
arena_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import requests
import urllib
class ArenaAPI(object):
def __init__(self, app):
self.app = app
self._authorisation = None
def config(self, key):
return self.app.config['arena'][key]
def authorisation(self):
if self._authorisation is None:
auth = self.app.config.get('keycloak', False)
if not auth:
return 'test'
result = requests.post(auth['url'], data={
'client_id': auth['client_id'],
'client_secret': auth['client_secret'],
'grant_type': auth['grant_type'],
'username': auth['username'],
'password': auth['password']
})
result.raise_for_status()
self._authorisation = result.json()['access_token']
return self._authorisation
def record_pdd(self, alert):
try:
self._pdd(alert, self.authorisation())
return
except requests.HTTPError as error:
self._authorisation = None
if error.response.status_code != 403:
raise
self._pdd(alert, self.authorisation())
def _pdd(self, alert, authorisation):
pdd_config = self.app.config['pdd']
headers = {"x-version": pdd_config['xver'],
"authorization": authorisation,
"content-type": "application/x-www-form-urlencoded"}
response = requests.post(pdd_config['url'],
headers=headers,
data=urllib.urlencode(alert))
response.raise_for_status()