def post_data()

in Solutions/Mimecast/Data Connectors/MimecastSEG/SharedCode/sentinel.py [0:0]


def post_data(body, log_type):
    """Build and send a request to the POST API.

    Args:
        body (str): Data to post into Sentinel log analytics workspace
        log_type (str): Custom log table name in which data wil be added.

    Returns:
        status_code: Returns the response status code got while posting data to sentinel.
    """
    __method_name = inspect.currentframe().f_code.co_name
    method = "POST"
    content_type = "application/json"
    resource = "/api/logs"
    rfc1123date = datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT")
    content_length = len(body)
    try:
        signature = build_signature(
            rfc1123date,
            content_length,
            method,
            content_type,
            resource,
        )
    except Exception as err:
        applogger.error(
            "{}(method={}) : Error in build signature-{}".format(
                consts.LOGS_STARTS_WITH,
                __method_name,
                err,
            )
        )
        raise MimecastException()
    uri = "https://" + consts.WORKSPACE_ID + ".ods.opinsights.azure.com" + resource + "?api-version=2016-04-01"

    headers = {
        "content-type": content_type,
        "Authorization": signature,
        "Log-Type": log_type,
        "x-ms-date": rfc1123date,
    }
    retry_count = 0
    while retry_count < consts.SENTINEL_RETRY_COUNT:
        try:

            response = requests.post(uri, data=body, headers=headers, timeout=consts.MAX_TIMEOUT_SENTINEL)

            result = handle_response(response, body, log_type, async_call=False)

            if result is not False:
                return result
            retry_count += 1
            continue
        except requests.exceptions.ConnectionError as error:
            try:
                if isinstance(error.args[0].reason, NameResolutionError):
                    applogger.error(
                        "{}(method={}) : {} : Workspace ID is wrong: {}, Sleeping for {} seconds and retrying..".format(
                            consts.LOGS_STARTS_WITH,
                            __method_name,
                            log_type,
                            error,
                            consts.INGESTION_ERROR_SLEEP_TIME,
                        )
                    )
                    time.sleep(consts.INGESTION_ERROR_SLEEP_TIME)
                    retry_count += 1
                    continue
            except Exception as unknown_connect_error:
                applogger.error(
                    "{}(method={}) : {} : Unknown Error in ConnectionError: {}, Sleeping for {} seconds."
                    " and retrying..".format(
                        consts.LOGS_STARTS_WITH,
                        __method_name,
                        log_type,
                        unknown_connect_error,
                        consts.INGESTION_ERROR_SLEEP_TIME,
                    )
                )
                time.sleep(consts.INGESTION_ERROR_SLEEP_TIME)
                retry_count += 1
                continue
            applogger.error(
                "{}(method={}) : {} : Unknown Connection Error, sleeping for {} seconds and retrying.."
                "Error - {}".format(
                    consts.LOGS_STARTS_WITH,
                    __method_name,
                    log_type,
                    consts.INGESTION_ERROR_SLEEP_TIME,
                    error,
                )
            )
            time.sleep(consts.INGESTION_ERROR_SLEEP_TIME)
            retry_count += 1
            continue
        except requests.exceptions.Timeout as error:
            applogger.error(
                "{}(method={}) : {} : sleeping - {} seconds and retrying.. Timeout Error: {}".format(
                    consts.LOGS_STARTS_WITH,
                    __method_name,
                    log_type,
                    consts.INGESTION_ERROR_SLEEP_TIME,
                    error,
                )
            )
            time.sleep(consts.INGESTION_ERROR_SLEEP_TIME)
            retry_count += 1
            continue
        except requests.RequestException as error:
            applogger.error(
                "{}(method={}) : {} : Request Error: {}".format(
                    consts.LOGS_STARTS_WITH,
                    __method_name,
                    log_type,
                    error,
                )
            )
            raise MimecastException()
        except MimecastException:
            raise MimecastException()
        except Exception as error:
            applogger.error(
                "{}(method={}) : {} : Unknown Error: {}.".format(
                    consts.LOGS_STARTS_WITH,
                    __method_name,
                    log_type,
                    error,
                )
            )
            raise MimecastException()
    applogger.error(
        "{}(method={}) : {} : Maximum Retry count of {} exceeded, hence stopping execution.".format(
            consts.LOGS_STARTS_WITH,
            __method_name,
            log_type,
            consts.SENTINEL_RETRY_COUNT,
        )
    )
    raise MimecastException()