pvc/client-cli/cli_lib/common.py

102 lines
3.1 KiB
Python
Raw Normal View History

#!/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>
#
# 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
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
def call_api(config, operation, request_uri, params=None, data=None, files=None):
# Craft the URI
uri = '{}://{}{}{}'.format(
config['api_scheme'],
config['api_host'],
config['api_prefix'],
request_uri
)
# Craft the authentication header if required
if config['api_key']:
headers = {'X-Api-Key': config['api_key']}
else:
headers = None
# Determine the request type and hit the API
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,
data=data,
files=files
)
if operation == 'put':
response = requests.put(
uri,
headers=headers,
params=params,
data=data,
files=files
)
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:
message = 'Failed to connect to the API: {}'.format(e)
response = ErrorResponse({'message':message}, 500)
# 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)
# Return the response object
return response