# Path: pyoauth/_compat.py
# def unquote_plus(v):
# 
# Path: pyoauth/constants.py
# SYMBOL_QUESTION_MARK = b("?")
# 
# SYMBOL_AMPERSAND = b("&")
# 
# SYMBOL_EQUAL = b("=")
# 
# OAUTH_PARAM_PREFIX = "oauth_"
# 
# OAUTH_VALUE_CALLBACK_OOB = b("oob")
# 
# OAUTH_PARAM_CONSUMER_SECRET = "oauth_consumer_secret"
# 
# OAUTH_PARAM_TOKEN_SECRET = "oauth_token_secret"
# 
# SYMBOL_EMPTY_BYTES = b("")
# 
# Path: pyoauth/error.py
# class InvalidQueryParametersError(OAuthError):
#     """
#     Raised when a query parameter is invalid.
#     """
#     pass
# 
# class InsecureOAuthParametersError(OAuthError):
#     """
#     Raised when an OAuth confidential parameter is passed into protocol
#     parameters.
#     """
#     pass
# 
# class InvalidOAuthParametersError(OAuthError):
#     """
#     Raised when invalid protocol parameters are detected.
#     """
#     pass
# 
# class InsecureOAuthUrlError(OAuthError):
#     """
#     Raised when an insecure (non-HTTPS) URL is detected for requesting OAuth
#     credentials.
#     """
#     pass
# 
# class InvalidUrlError(OAuthError):
#     """
#     Raised when a specified URL is invalid for consumption by a routine.
#     """
#     pass


import logging
from mom.builtins import is_sequence, bytes, is_bytes_or_unicode, is_bytes
from mom.codec.text import utf8_encode_if_unicode, \
    utf8_encode, utf8_decode_if_bytes
from mom.functional import select_dict, map_dict
from mom.builtins import b
from pyoauth._compat import urlparse, urlunparse, parse_qs as _parse_qs, \
    quote, \
    unquote_plus
from pyoauth.constants import SYMBOL_QUESTION_MARK, \
    SYMBOL_AMPERSAND, SYMBOL_EQUAL, OAUTH_PARAM_PREFIX, \
    OAUTH_VALUE_CALLBACK_OOB, OAUTH_PARAM_CONSUMER_SECRET, \
    OAUTH_PARAM_TOKEN_SECRET, SYMBOL_EMPTY_BYTES
from pyoauth.error import InvalidQueryParametersError, \
    InsecureOAuthParametersError, \
    InvalidOAuthParametersError, \
    InsecureOAuthUrlError, \
    InvalidUrlError

    .. WARNING:: Do NOT use this function with responses.

        Use ONLY with requests.

        Specifically used ONLY in base string construction, Authorization
        headers construction and parsing, and OAuth requests.

    :param query:
        Query string or query parameter dictionary. Does not filter out
        ``oauth_signature``, but DOES filter out ``oauth_consumer_secret`` and
        ``oauth_token_secret``. These secret parameters must never be
        transmitted.
    :returns:
        Filtered protocol parameters dictionary.
    """
    def predicate(name, value):
        """
        Allows only valid oauth parameters.

        Raises an error if multiple values are specified.

        :param name:
            Protocol parameter name.
        :param value:
            Protocol parameter value.
        :returns:
            ``True`` if the parameter should be included; ``False`` otherwise.
        """
        name = utf8_decode_if_bytes(name)

# Path: pyoauth/_compat.py
# def unquote_plus(v):
# 
# Path: pyoauth/constants.py
# SYMBOL_QUESTION_MARK = b("?")
# 
# SYMBOL_AMPERSAND = b("&")
# 
# SYMBOL_EQUAL = b("=")
# 
# OAUTH_PARAM_PREFIX = "oauth_"
# 
# OAUTH_VALUE_CALLBACK_OOB = b("oob")
# 
# OAUTH_PARAM_CONSUMER_SECRET = "oauth_consumer_secret"
# 
# OAUTH_PARAM_TOKEN_SECRET = "oauth_token_secret"
# 
# SYMBOL_EMPTY_BYTES = b("")
# 
# Path: pyoauth/error.py
# class InvalidQueryParametersError(OAuthError):
#     """
#     Raised when a query parameter is invalid.
#     """
#     pass
# 
# class InsecureOAuthParametersError(OAuthError):
#     """
#     Raised when an OAuth confidential parameter is passed into protocol
#     parameters.
#     """
#     pass
# 
# class InvalidOAuthParametersError(OAuthError):
#     """
#     Raised when invalid protocol parameters are detected.
#     """
#     pass
# 
# class InsecureOAuthUrlError(OAuthError):
#     """
#     Raised when an insecure (non-HTTPS) URL is detected for requesting OAuth
#     credentials.
#     """
#     pass
# 
# class InvalidUrlError(OAuthError):
#     """
#     Raised when a specified URL is invalid for consumption by a routine.
#     """
#     pass


# Path: pyoauth/url.py
import logging
from mom.builtins import is_sequence, bytes, is_bytes_or_unicode, is_bytes
from mom.codec.text import utf8_encode_if_unicode, \
    utf8_encode, utf8_decode_if_bytes
from mom.functional import select_dict, map_dict
from mom.builtins import b
from pyoauth._compat import urlparse, urlunparse, parse_qs as _parse_qs, \
    quote, \
    unquote_plus
from pyoauth.constants import SYMBOL_QUESTION_MARK, \
    SYMBOL_AMPERSAND, SYMBOL_EQUAL, OAUTH_PARAM_PREFIX, \
    OAUTH_VALUE_CALLBACK_OOB, OAUTH_PARAM_CONSUMER_SECRET, \
    OAUTH_PARAM_TOKEN_SECRET, SYMBOL_EMPTY_BYTES
from pyoauth.error import InvalidQueryParametersError, \
    InsecureOAuthParametersError, \
    InvalidOAuthParametersError, \
    InsecureOAuthUrlError, \
    InvalidUrlError


    .. WARNING:: Do NOT use this function with responses.

        Use ONLY with requests.

        Specifically used ONLY in base string construction, Authorization
        headers construction and parsing, and OAuth requests.

    :param query:
        Query string or query parameter dictionary. Does not filter out
        ``oauth_signature``, but DOES filter out ``oauth_consumer_secret`` and
        ``oauth_token_secret``. These secret parameters must never be
        transmitted.
    :returns:
        Filtered protocol parameters dictionary.
    """
    def predicate(name, value):
        """
        Allows only valid oauth parameters.

        Raises an error if multiple values are specified.

        :param name:
            Protocol parameter name.
        :param value:
            Protocol parameter value.
        :returns:
            ``True`` if the parameter should be included; ``False`` otherwise.
        """
        name = utf8_decode_if_bytes(name)

        if name.startswith(OAUTH_PARAM_PREFIX):