Exemplo n.º 1
0
    def __init__(
            self,
            format_string=DEFAULT_FORMAT_STRING,
            scheme=DEFAULT_SCHEME,
            timeout=DEFAULT_TIMEOUT,
            proxies=None
        ):
        """
        Mostly-common geocoder validation, proxies, &c. Not all geocoders
        specify format_string and such.
        """
        self.format_string = format_string
        self.scheme = scheme
        if self.scheme not in ('http', 'https'): # pragma: no cover
            raise ConfigurationError(
                'Supported schemes are `http` and `https`.'
            )
        self.proxies = proxies
        self.timeout = timeout

        if self.proxies:
            install_opener(
                build_opener(
                    ProxyHandler(self.proxies)
                )
            )
        self.urlopen = urllib_urlopen
Exemplo n.º 2
0
    def __init__(
            self,
            format_string=DEFAULT_FORMAT_STRING,
            scheme=DEFAULT_SCHEME,
            timeout=DEFAULT_TIMEOUT,
            proxies=None,
            user_agent=None,
        ):
        """
        Mostly-common geocoder validation, proxies, &c. Not all geocoders
        specify format_string and such.
        """
        self.format_string = format_string
        self.scheme = scheme
        if self.scheme not in ('http', 'https'): # pragma: no cover
            raise ConfigurationError(
                'Supported schemes are `http` and `https`.'
            )
        self.proxies = proxies
        self.timeout = timeout
        self.headers = {'User-Agent': user_agent or DEFAULT_USER_AGENT}

        if self.proxies:
            install_opener(
                build_opener(
                    ProxyHandler(self.proxies)
                )
            )
        self.urlopen = urllib_urlopen
Exemplo n.º 3
0
    def addSimpleHTTPAuthHeader(self):
        """
        Create Urllib request object embedding HTTP simple authentication
        """
        sub_request = """
        <GeocodeRequest returnFreeForm="{is_freeform}">
            <Address countryCode="{query_type}">
                <freeFormAddress>{query}</freeFormAddress>
            </Address>
        </GeocodeRequest>
        """

        xml_request = self.xml_request.format(
            method_name='LocationUtilityService',
            sub_request=sub_request,
            maximum_responses=1
        )

        # Create query using parameters
        request_string = xml_request.format(
            is_freeform='false',
            query='rennes',
            query_type='PositionOfInterest'
        )

        params = {
            'xls':  request_string
        }

        top_level_url = "?".join((self.api, urlencode(params)))
        password_mgr = HTTPPasswordMgrWithDefaultRealm()

        # Add the username and password.
        # If we knew the realm, we could use it instead of None.
        password_mgr.add_password(
            None,
            top_level_url,
            self.username,
            self.password
        )

        handler = HTTPBasicAuthHandler(password_mgr)

        # create "opener" (OpenerDirector instance)
        opener = build_opener(handler)

        # Install the opener.
        # Now all calls to urllib.request.urlopen use our opener.
        install_opener(opener)