Skip to content

[_brighthub_request] Extend brighthub request for POST, PUT and DELETE methods #583

@dancasey-ie

Description

@dancasey-ie

Is your feature request related to a problem? Please describe.
There is currently no method for making POST or PUT requests to the brighthub apis.

Describe the solution you'd like
The brightwind.load.load.py::LoadBrightHub._brighthub_request function is currently used to make authenticated GET requests to the brighthub platform APIs.

The _brighthub_request() method should be moved out to a new brighthub utils module (brightwind.utils.brighthub.py::brighthub_request), along with its dependencies (_BrighthubAuth and __BASE_URI). The method should also be updated to handle all http request methods (GET, PUT, POST, DELETE).

def brighthub_request(method: str, url_extension: str, params=None):
    """Function to make a http (GET,POST,PUT,DELETE) request to the Brighthub endpoints

    Args:
        method (str): http method name in string object ex.. GET
        url (str): valid url of the api endpoint
        params (Dict, optional): parameters of the requests in dictionary. Defaults to None.

    Returns:
        response: response object containing the response details wrapped in response type object
    """
    log.debug("brighthub_request() entry")
    if not _BrighthubAuth.ID_TOKEN:
        _BrighthubAuth._get_id_token()

    headers = {"authorization": _BrighthubAuth.ID_TOKEN}
    url = f"{BASE_URL_BRIGHTHUB}/{url_extension}"
    max_retries = 3
    retry_delay = 3

    for retry in range(max_retries):
        try:
            if method == "GET":
                response = requests.get(
                    url=url, headers=headers, params=params, timeout=10
                )
                break
            elif method == "POST":
                response = requests.post(
                    url=url, headers=headers, json=params, timeout=10
                )
                break
            elif method == "PUT":
                response = requests.put(
                    url=url, headers=headers, json=params, timeout=10
                )
                break
            elif method == "DELETE":
                response = requests.delete(url=url, headers=headers, timeout=10)
                break
            else:
                response = requests.models.Response()
                response.status_code = 411
                response._content = b'{"message":"Unsupported request method."}'
                return response
        except requests.Timeout:
            log.error("Request timed out.")
        except requests.RequestException as e:
            log.error("An error occurred:", str(e))
        time.sleep(retry_delay)

    # If there is an auth error due to expired token
    if response.status_code == 401 and response.json().get("message") == "The incoming token has expired":
        # Authenticate again
        _BrighthubAuth._get_id_token()
        headers = {"authorization": _BrighthubAuth.ID_TOKEN}
        # Make the request again
        response = requests.get(url=url, headers=headers, params=params)

    log.debug("brighthub_request() exit")
    return response

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Enabling the user to interact with all available APIs, authenticated through the brightwind library enables users to perform programmatic updates to BrightHub.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Backlog

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions