2020-01-08 19:34:24 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# common.py - PVC CLI client function library, Common functions
|
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2020-01-08 19:38:02 -05:00
|
|
|
# Copyright (C) 2018-2020 Joshua M. Boniface <joshua@boniface.me>
|
2020-01-08 19:34:24 -05:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import click
|
|
|
|
|
2020-02-19 16:19:34 -05:00
|
|
|
class ErrorResponse(requests.Response):
|
|
|
|
def __init__(self, json_data, status_code):
|
|
|
|
self.json_data = json_data
|
|
|
|
self.status_code = status_code
|
|
|
|
|
|
|
|
def json(self):
|
|
|
|
return self.json_data
|
|
|
|
|
2020-02-09 20:42:56 -05:00
|
|
|
def call_api(config, operation, request_uri, params=None, data=None, files=None):
|
2020-01-08 19:34:24 -05:00
|
|
|
# Craft the URI
|
|
|
|
uri = '{}://{}{}{}'.format(
|
|
|
|
config['api_scheme'],
|
|
|
|
config['api_host'],
|
|
|
|
config['api_prefix'],
|
|
|
|
request_uri
|
|
|
|
)
|
2020-01-30 09:19:36 -05:00
|
|
|
|
2020-01-08 19:34:24 -05:00
|
|
|
# Craft the authentication header if required
|
|
|
|
if config['api_key']:
|
|
|
|
headers = {'X-Api-Key': config['api_key']}
|
|
|
|
else:
|
|
|
|
headers = None
|
2020-01-30 09:19:36 -05:00
|
|
|
|
2020-01-08 19:34:24 -05:00
|
|
|
# Determine the request type and hit the API
|
2020-01-30 09:19:36 -05:00
|
|
|
try:
|
|
|
|
if operation == 'get':
|
|
|
|
response = requests.get(
|
|
|
|
uri,
|
|
|
|
headers=headers,
|
|
|
|
params=params,
|
|
|
|
data=data
|
|
|
|
)
|
|
|
|
if operation == 'post':
|
|
|
|
response = requests.post(
|
|
|
|
uri,
|
|
|
|
headers=headers,
|
|
|
|
params=params,
|
2020-02-09 20:42:56 -05:00
|
|
|
data=data,
|
|
|
|
files=files
|
2020-01-30 09:19:36 -05:00
|
|
|
)
|
|
|
|
if operation == 'put':
|
|
|
|
response = requests.put(
|
|
|
|
uri,
|
|
|
|
headers=headers,
|
|
|
|
params=params,
|
2020-02-09 20:42:56 -05:00
|
|
|
data=data,
|
|
|
|
files=files
|
2020-01-30 09:19:36 -05:00
|
|
|
)
|
|
|
|
if operation == 'patch':
|
|
|
|
response = requests.patch(
|
|
|
|
uri,
|
|
|
|
headers=headers,
|
|
|
|
params=params,
|
|
|
|
data=data
|
|
|
|
)
|
|
|
|
if operation == 'delete':
|
|
|
|
response = requests.delete(
|
|
|
|
uri,
|
|
|
|
headers=headers,
|
|
|
|
params=params,
|
|
|
|
data=data
|
|
|
|
)
|
|
|
|
except Exception as e:
|
2020-02-19 16:19:34 -05:00
|
|
|
message = 'Failed to connect to the API: {}'.format(e)
|
|
|
|
response = ErrorResponse({'message':message}, 500)
|
2020-01-30 09:19:36 -05:00
|
|
|
|
2020-01-08 19:34:24 -05:00
|
|
|
# Display debug output
|
|
|
|
if config['debug']:
|
|
|
|
click.echo('API endpoint: {}'.format(uri), err=True)
|
|
|
|
click.echo('Response code: {}'.format(response.status_code), err=True)
|
|
|
|
click.echo('Response headers: {}'.format(response.headers), err=True)
|
2020-01-08 19:41:01 -05:00
|
|
|
click.echo(err=True)
|
2020-01-30 09:19:36 -05:00
|
|
|
|
2020-01-08 19:34:24 -05:00
|
|
|
# Return the response object
|
|
|
|
return response
|
|
|
|
|