xref: /OpenGrok/tools/src/main/python/opengrok_tools/utils/restful.py (revision 732be1c2480241b1b9a3e4ddc958855fb839662b)
12d57dc69SVladimir Kotal#
22d57dc69SVladimir Kotal# CDDL HEADER START
32d57dc69SVladimir Kotal#
42d57dc69SVladimir Kotal# The contents of this file are subject to the terms of the
52d57dc69SVladimir Kotal# Common Development and Distribution License (the "License").
62d57dc69SVladimir Kotal# You may not use this file except in compliance with the License.
72d57dc69SVladimir Kotal#
82d57dc69SVladimir Kotal# See LICENSE.txt included in this distribution for the specific
92d57dc69SVladimir Kotal# language governing permissions and limitations under the License.
102d57dc69SVladimir Kotal#
112d57dc69SVladimir Kotal# When distributing Covered Code, include this CDDL HEADER in each
122d57dc69SVladimir Kotal# file and include the License file at LICENSE.txt.
132d57dc69SVladimir Kotal# If applicable, add the following below this CDDL HEADER, with the
142d57dc69SVladimir Kotal# fields enclosed by brackets "[]" replaced with your own identifying
152d57dc69SVladimir Kotal# information: Portions Copyright [yyyy] [name of copyright owner]
162d57dc69SVladimir Kotal#
172d57dc69SVladimir Kotal# CDDL HEADER END
182d57dc69SVladimir Kotal#
192d57dc69SVladimir Kotal
202d57dc69SVladimir Kotal#
212d97c0a2SVladimir Kotal# Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
222d57dc69SVladimir Kotal#
232d57dc69SVladimir Kotal
242d57dc69SVladimir Kotalimport json
252d57dc69SVladimir Kotalimport logging
262d57dc69SVladimir Kotal
272d57dc69SVladimir Kotalimport requests
282d57dc69SVladimir Kotal
292d57dc69SVladimir Kotalfrom .patterns import COMMAND_PROPERTY
302d57dc69SVladimir Kotalfrom .webutil import get_proxies
312d57dc69SVladimir Kotal
322d57dc69SVladimir KotalCONTENT_TYPE = 'Content-Type'
332d57dc69SVladimir KotalAPPLICATION_JSON = 'application/json'   # default
342d57dc69SVladimir Kotal
352d57dc69SVladimir Kotal
36*732be1c2SVladimir Kotaldef do_api_call(verb, uri, params=None, headers=None, data=None, timeout=None):
372d57dc69SVladimir Kotal    """
382d57dc69SVladimir Kotal    Perform an API call. Will raise an exception if the request fails.
392d57dc69SVladimir Kotal    :param verb: string holding HTTP verb
402d57dc69SVladimir Kotal    :param uri: URI string
412d57dc69SVladimir Kotal    :param params: request parameters
422d57dc69SVladimir Kotal    :param headers: HTTP headers dictionary
432d57dc69SVladimir Kotal    :param data: data or None
44*732be1c2SVladimir Kotal    :param timeout: optional connect timeout in seconds.
45*732be1c2SVladimir Kotal                    If None, default (60 seconds) will be used.
462d57dc69SVladimir Kotal    :return: the result of the handler call, can be None
472d57dc69SVladimir Kotal    """
482d57dc69SVladimir Kotal    logger = logging.getLogger(__name__)
492d57dc69SVladimir Kotal
502d57dc69SVladimir Kotal    handler = getattr(requests, verb.lower())
512d57dc69SVladimir Kotal    if handler is None or not callable(handler):
522d57dc69SVladimir Kotal        raise Exception('Unknown HTTP verb: {}'.format(verb))
532d57dc69SVladimir Kotal
54*732be1c2SVladimir Kotal    if timeout is None:
55*732be1c2SVladimir Kotal        timeout = 60
56*732be1c2SVladimir Kotal
57*732be1c2SVladimir Kotal    logger.debug("{} API call: {} with data '{}', timeout {} seconds and headers: {}".
58*732be1c2SVladimir Kotal                 format(verb, uri, data, timeout, headers))
592d57dc69SVladimir Kotal    r = handler(
602d57dc69SVladimir Kotal        uri,
612d57dc69SVladimir Kotal        data=data,
622d57dc69SVladimir Kotal        params=params,
632d57dc69SVladimir Kotal        headers=headers,
64*732be1c2SVladimir Kotal        proxies=get_proxies(uri),
65*732be1c2SVladimir Kotal        timeout=timeout
662d57dc69SVladimir Kotal    )
672d57dc69SVladimir Kotal
682d57dc69SVladimir Kotal    if r is None:
692d57dc69SVladimir Kotal        raise Exception("API call failed")
702d57dc69SVladimir Kotal
712d57dc69SVladimir Kotal    r.raise_for_status()
722d57dc69SVladimir Kotal
732d57dc69SVladimir Kotal    return r
742d57dc69SVladimir Kotal
752d57dc69SVladimir Kotal
762d97c0a2SVladimir Kotaldef subst(src, substitutions):
772d97c0a2SVladimir Kotal    if substitutions:
782d97c0a2SVladimir Kotal        for pattern, value in substitutions.items():
792d97c0a2SVladimir Kotal            if value:
802d97c0a2SVladimir Kotal                src = src.replace(pattern, value)
812d97c0a2SVladimir Kotal
822d97c0a2SVladimir Kotal    return src
832d97c0a2SVladimir Kotal
842d97c0a2SVladimir Kotal
85*732be1c2SVladimir Kotaldef call_rest_api(command, substitutions=None, http_headers=None, timeout=None):
862d57dc69SVladimir Kotal    """
872d57dc69SVladimir Kotal    Make RESTful API call. Occurrence of the pattern in the URI
882d57dc69SVladimir Kotal    (first part of the command) or data payload will be replaced by the name.
892d57dc69SVladimir Kotal
902d57dc69SVladimir Kotal    Default content type is application/json.
912d57dc69SVladimir Kotal
9289229afdSVladimir Kotal    :param command: command (list of URI, HTTP verb, data payload,
9389229afdSVladimir Kotal                             HTTP header dictionary)
942d97c0a2SVladimir Kotal    :param substitutions: dictionary of pattern:value for command and/or
952d97c0a2SVladimir Kotal                          data substitution
9689229afdSVladimir Kotal    :param http_headers: optional dictionary of HTTP headers to be appended
97*732be1c2SVladimir Kotal    :param timeout: optional timeout in seconds for API call response
982d57dc69SVladimir Kotal    :return return value from given requests method
992d57dc69SVladimir Kotal    """
1002d57dc69SVladimir Kotal
1012d57dc69SVladimir Kotal    logger = logging.getLogger(__name__)
1022d57dc69SVladimir Kotal
1032d57dc69SVladimir Kotal    if not isinstance(command, dict) or command.get(COMMAND_PROPERTY) is None:
1042d57dc69SVladimir Kotal        raise Exception("invalid command")
1052d57dc69SVladimir Kotal
1062d57dc69SVladimir Kotal    command = command[COMMAND_PROPERTY]
1072d57dc69SVladimir Kotal
1082d57dc69SVladimir Kotal    uri, verb, data, *_ = command
1092d57dc69SVladimir Kotal    try:
1102d57dc69SVladimir Kotal        headers = command[3]
1112d57dc69SVladimir Kotal        if headers and not isinstance(headers, dict):
1122d57dc69SVladimir Kotal            raise Exception("headers must be a dictionary")
1132d57dc69SVladimir Kotal    except IndexError:
1142d57dc69SVladimir Kotal        headers = {}
1152d57dc69SVladimir Kotal
1162d57dc69SVladimir Kotal    if headers is None:
1172d57dc69SVladimir Kotal        headers = {}
1182d57dc69SVladimir Kotal
119b369c884SVladimir Kotal    logger.debug("Headers from the command: {}".format(headers))
12089229afdSVladimir Kotal    if http_headers:
121b369c884SVladimir Kotal        logger.debug("Updating HTTP headers for command {} with {}".
122b369c884SVladimir Kotal                     format(command, http_headers))
12389229afdSVladimir Kotal        headers.update(http_headers)
12489229afdSVladimir Kotal
1252d97c0a2SVladimir Kotal    uri = subst(uri, substitutions)
1262d57dc69SVladimir Kotal    header_names = [x.lower() for x in headers.keys()]
1272d57dc69SVladimir Kotal
1282d57dc69SVladimir Kotal    if data:
1292d57dc69SVladimir Kotal        if CONTENT_TYPE.lower() not in header_names:
1302d57dc69SVladimir Kotal            logger.debug("Adding header: {} = {}".
1312d57dc69SVladimir Kotal                         format(CONTENT_TYPE, APPLICATION_JSON))
1322d57dc69SVladimir Kotal            headers[CONTENT_TYPE] = APPLICATION_JSON
1332d57dc69SVladimir Kotal
1342d57dc69SVladimir Kotal        for (k, v) in headers.items():
1352d57dc69SVladimir Kotal            if k.lower() == CONTENT_TYPE.lower():
1362d57dc69SVladimir Kotal                if headers[k].lower() == APPLICATION_JSON.lower():
1372d57dc69SVladimir Kotal                    logger.debug("Converting {} to JSON".format(data))
1382d57dc69SVladimir Kotal                    data = json.dumps(data)
1392d57dc69SVladimir Kotal                break
1402d57dc69SVladimir Kotal
1412d97c0a2SVladimir Kotal        data = subst(data, substitutions)
1422d57dc69SVladimir Kotal        logger.debug("entity data: {}".format(data))
1432d57dc69SVladimir Kotal
144*732be1c2SVladimir Kotal    return do_api_call(verb, uri, headers=headers, data=data, timeout=timeout)
145