alkira/api-02.py
2022-06-14 14:32:35 +02:00

107 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import sys
import re
import json
import time
import logging
import requests
import configparser
###############################################
CONFIG_FILE = "/Users/mischa/Alkira/xDev/alkira.cnf"
if not os.path.isfile(CONFIG_FILE):
logging.error(f"The config file {CONFIG_FILE} doesn't exist")
sys.exit(1)
config = configparser.RawConfigParser()
config.read(CONFIG_FILE)
ALKIRA_TENANT = config.get('alkira', 'ALKIRA_TENANT')
ALKIRA_USERNAME = config.get('alkira', 'ALKIRA_USERNAME')
ALKIRA_PASSWORD = config.get('alkira', 'ALKIRA_PASSWORD')
ALKIRA_BASE_URI = f'https://{ALKIRA_TENANT}/api'
###############################################
# Set default headers
headers = {'Content-Type': "application/json"}
# Set logging.INFO to logging.DEBUG for debug information
logging.basicConfig(level=logging.DEBUG)
logging = logging.getLogger('AlkiraAPI')
def alkira_login():
body = {'userName': ALKIRA_USERNAME,
'password': ALKIRA_PASSWORD}
session = requests.session()
response = alkira_post(session, '/login', body)
return session
def alkira_post(session, uri, body):
url = f'{ALKIRA_BASE_URI}{uri}'
try:
response = session.post(url, data=json.dumps(body), headers=headers)
response.raise_for_status()
except Exception as e:
logging.error(f'Error: {str(e)}')
sys.exit(1)
return response
def alkira_get(session, uri):
url = f'{ALKIRA_BASE_URI}{uri}'
try:
response = session.get(url, headers=headers)
response.raise_for_status()
except Exception as e:
logging.error(f'Error: {str(e)}')
sys.exit(1)
return response
def alkira_delete(session, uri):
url = f'{ALKIRA_BASE_URI}{uri}'
try:
response = session.delete(url, headers=headers)
response.raise_for_status()
except Exception as e:
logging.error(f'Error: {str(e)}')
sys.exit(1)
return response
s = alkira_login()
logging.debug(s)
r = alkira_get(s, '/tenantnetworks')
#logging.debug(r.text)
data = r.json()
print(data[0]['name'])
print(data[0]['id'])
#body = {"email": "john@abc.com",
#"firstName": "John",
#"lastName": "Doe",
#"password": "string",
#"roles": ["netadmin"],
#"userName": "john"}
#r = alkira_post(s, '/users', body)
#print(r.text)
r = alkira_get(s, '/users')
data = r.json()
print(json.dumps(data, indent=4))
for item in data:
if item.get('userName') == 'john':
user_id = item.get('id')
print(user_id)
#r = alkira_delete(s, f'/users/{user_id}')
#print(r.text)