Skip to content

Instantly share code, notes, and snippets.

@Nick011
Last active August 7, 2019 18:39
Show Gist options
  • Save Nick011/0560f586273713a8849bfcf20d2ccedc to your computer and use it in GitHub Desktop.
Save Nick011/0560f586273713a8849bfcf20d2ccedc to your computer and use it in GitHub Desktop.
# Warning: I have not tested this
import requests
from datetime import datetime, timedelta
api_key = '(Your API Key)'
username = '(Your API Username)'
password = '(Your API Password)'
header = {
'x-api-key': api_key
}
base_url = 'https://api.perch.rocks/v1'
auth_url = base_url + '/auth/access_token'
escalated_status = 4
escalated_alerts_url = f'{base_url}/alerts?status={escalated_status}'
poll_minutes = 10
last_poll = datetime.now()
def authenticate():
# Authenticate the user and set the auth token header
auth_url = base_url + '/auth/access_token'
req_body = {
'username': username,
'password': password
}
res = requests.post(auth_url, data=req_body, headers=header)
res_body = res.json()
header['Authorization'] = 'Bearer ' + res_body['access_token']
def main():
authenticate()
while True:
now = datetime.now()
if now < last_poll + timedelta(minutes=poll_minutes):
continue
url = f'{escalated_alerts_url}&status_updated_at__gte={last_poll.isoformat()}'
res = requests.get(url, headers=header)
if not res.ok:
print('There was some error with fetching escalations')
data = res.json()
#TODO: do things with data.
last_poll = now
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment