Beispiel #1
0
def http_post(uri, payload, params=None, **kwargs):
    """Make a new HTTP/POST request.

    Args:
        uri (string): URI to send the payload to
        payload (dict): JSON payload to send to the HTTP server
        params (dict): URI query parameters

    Returns:
        obj: requests.Response

    """
    headers = http_headers(**kwargs)

    lg().debug("http<post>: '%s'", log_sanitize_string(uri))
    lg().debug("http<post/params>: '%s'", log_sanitize_string(params))
    lg().debug("http<post/headers>: '%s'", log_sanitize_string(headers))
    lg().debug("http<post/payload>: '%s'", log_sanitize_string(payload))

    result = post(
        uri,
        allow_redirects=opts.http_allow_redirects,
        timeout=opts.http_timeout,
        headers=headers,
        params=params,
        json=payload,
    )

    result.raise_for_status()
    return result
Beispiel #2
0
    def message_dump(self, logtext, message):
        """Dump the message details in JSON to the logger

        Ignored unless application uses the debug level.

        Args:
            logtext (string): log entry text to write
            message (obj): the message to log

        """
        if lg().level <= DEBUG:
            json = to_json(message, minified=opts.debug_minify_json)
            self.log(logtext % log_sanitize_string(json))
Beispiel #3
0
    def message_log_serialized(self, prefix, message):
        """Logs the given serialized message in hex format.

        Args:
            message (obj): Message to be logged

        """
        if lg().level <= DEBUG:
            _len = len(message)
            _hex = hexlify(bytearray(message))
            self.log(
                "<%s:serialized> [%s] %s" %
                (prefix, _len, log_sanitize_string(_hex)), )
Beispiel #4
0
def http_get(uri, **kwargs):
    """Make a new HTTP/GET request.

    Args:
        uri (string): URI to retrieve

    Returns:
        obj: requests.Response

    """
    headers = http_headers(**kwargs)

    lg().debug("http<get>: '%s'", log_sanitize_string(uri))
    lg().debug("http<get/headers>: '%s'", log_sanitize_string(headers))

    result = get(
        uri,
        allow_redirects=opts.http_allow_redirects,
        timeout=opts.http_timeout,
        headers=headers,
    )

    result.raise_for_status()
    return result
Beispiel #5
0
def script_dispatcher(input_uri):
    """Match input URI to a handling (media) script.

    Args:
        input_uri (string): the input URI to match

    Returns:
        obj: The parsed media meta data in a PluginMediaParser subclass

    Raises:
        NoParserError if no matching handler could not be found

    """
    lg().debug(
        "dispatcher<%s>: match '%s'",
        NS_NAME,
        log_sanitize_string(input_uri),
    )

    (uri_handlers, uri_components) = (
        plugin_handlers[NS_NAME],
        urlparse(input_uri),
    )

    for handler in uri_handlers:
        try:
            # Either return a new subclassed PluginMediaParser object, or
            # raise the CannotParseError exception.
            #
            return handler.inquire(uri_components)
        except CannotParseError:
            # Rinse and repeat until we run out of handlers.
            #
            pass
        except:
            # Fail at all other exceptions by passing the raised
            # exception.
            #
            raise
    # When we run out of handlers, inform the caller that we could
    # not find a matching parser for the given input URI.
    #
    raise NoParserError(
        "Unable to find a matching parser for URI <%s>" % input_uri,
    )