Skip to content
Snippets Groups Projects
RestApiClient.py 4.63 KiB
Newer Older
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import copy, json, logging, requests
from requests.auth import HTTPBasicAuth
from typing import Any, Dict, List, Set, Tuple, Union

LOGGER = logging.getLogger(__name__)

DEFAULT_BASE_URL = '/restconf/v2/data'
DEFAULT_SCHEME   = 'https'
DEFAULT_TIMEOUT  = 120
DEFAULT_VERIFY   = False

HTTP_STATUS_OK         = 200
HTTP_STATUS_CREATED    = 201
HTTP_STATUS_ACCEPTED   = 202
HTTP_STATUS_NO_CONTENT = 204

HTTP_OK_CODES = {
    HTTP_STATUS_OK,
    HTTP_STATUS_CREATED,
    HTTP_STATUS_ACCEPTED,
    HTTP_STATUS_NO_CONTENT,
}

class RestApiClient:
    def __init__(self, address : str, port : int, settings : Dict[str, Any] = dict()) -> None:
        username = settings.get('username')
        password = settings.get('password')
        self._auth = HTTPBasicAuth(username, password) if username is not None and password is not None else None

        scheme   = settings.get('scheme',   DEFAULT_SCHEME  )
        base_url = settings.get('base_url', DEFAULT_BASE_URL)
        self._base_url = '{:s}://{:s}:{:d}{:s}'.format(scheme, address, int(port), base_url)

        self._timeout = int(settings.get('timeout', DEFAULT_TIMEOUT))
        self._verify  = bool(settings.get('verify', DEFAULT_VERIFY))
        self, object_name : str, url : str, filters : List[Tuple[str, str]],
        expected_http_status : Set[int] = {HTTP_STATUS_OK}
    ) -> Union[Dict, List]:
        str_filters = ''.join([
            '[{:s}={:s}]'.format(filter_field, filter_value)
            for filter_field, filter_value in filters
        ])

        MSG = 'Get {:s}({:s})'
        LOGGER.info(MSG.format(str(object_name), str(str_filters)))
        response = requests.get(
            self._base_url + url + str_filters,
            timeout=self._timeout, verify=self._verify, auth=self._auth
        )
        LOGGER.info('  Response: {:s}'.format(str(response)))

        if response.status_code in expected_http_status: return json.loads(response.content)

        MSG = 'Could not get {:s}({:s}): status_code={:s} reply={:s}'
        msg = MSG.format(str(object_name), str(str_filters), str(response.status_code), str(response))
        LOGGER.error(msg)
        return Exception(msg)

    def update(
        self, object_name : str, url : str, data : Dict, headers : Dict[str, Any] = dict(),
        expected_http_status : Set[int] = HTTP_OK_CODES
    ) -> bool:
        headers = copy.deepcopy(headers)
        if 'content-type' not in {header_name.lower() for header_name in headers.keys()}:
            headers.update({'content-type': 'application/json'})

        MSG = 'Create/Update {:s}({:s})'
        LOGGER.info(MSG.format(str(object_name), str(data)))
        response = requests.post(
            self._base_url + url, data=json.dumps(data), headers=headers,
            timeout=self._timeout, verify=self._verify, auth=self._auth
        )
        LOGGER.info('  Response: {:s}'.format(str(response)))
        if response.status_code in expected_http_status: return True

        MSG = 'Could not create/update {:s}({:s}): status_code={:s} reply={:s}'
        LOGGER.error(MSG.format(str(object_name), str(data), str(response.status_code), str(response)))
        return False

    def delete(
        self, object_name : str, url : str, filters : List[Tuple[str, str]],
        expected_http_status : Set[int] = HTTP_OK_CODES
    ) -> bool:
        str_filters = ''.join([
            '[{:s}={:s}]'.format(filter_field, filter_value)
            for filter_field, filter_value in filters
        ])

        MSG = 'Delete {:s}({:s})'
        LOGGER.info(MSG.format(str(object_name), str(str_filters)))
        response = requests.delete(
            self._base_url + url + str_filters,
            timeout=self._timeout, verify=self._verify, auth=self._auth
        )
        LOGGER.info('  Response: {:s}'.format(str(response)))

        if response.status_code in expected_http_status: return True

        MSG = 'Could not delete {:s}({:s}): status_code={:s} reply={:s}'
        LOGGER.error(MSG.format(str(object_name), str(str_filters), str(response.status_code), str(response)))
        return False