Skip to content
Permalink
Browse files

Add getter/setter for api key, region

  • Loading branch information
nyalldawson committed Nov 3, 2020
1 parent 98d0971 commit e70caeb3e151974971ad9dcffefa03faa04b7a9a
@@ -67,6 +67,39 @@ Converts a JSON result returned from the Google Maps service to a geocoder resul
void setEndpoint( const QString &endpoint );
%Docstring
Sets a specific API ``endpoint`` to use for requests. This is for internal testing purposes only.
%End

QString apiKey() const;
%Docstring
Returns the API key which will be used when accessing the Google Maps API.

.. seealso:: :py:func:`setApiKey`
%End

void setApiKey( const QString &key );
%Docstring
Sets the API ``key`` to use when accessing the Google Maps API.

All use of this geocoder will be associated with the specified key for Google's billing purposes!

.. seealso:: :py:func:`apiKey`
%End

QString region() const;
%Docstring
Returns the optional region bias which will be used to prioritize results in a certain region.

.. seealso:: :py:func:`setRegion`
%End

void setRegion( const QString &region );
%Docstring
Sets the optional ``region`` bias which will be used to prioritize results in a certain region.

The ``region`` argument must be set to a two letter country code top-level domain value,
e.g. "gb" for Great Britain.

.. seealso:: :py:func:`region`
%End

};
@@ -251,3 +251,23 @@ void QgsGoogleMapsGeocoder::setEndpoint( const QString &endpoint )
{
mEndpoint = endpoint;
}

QString QgsGoogleMapsGeocoder::apiKey() const
{
return mApiKey;
}

void QgsGoogleMapsGeocoder::setApiKey( const QString &apiKey )
{
mApiKey = apiKey;
}

QString QgsGoogleMapsGeocoder::region() const
{
return mRegion;
}

void QgsGoogleMapsGeocoder::setRegion( const QString &region )
{
mRegion = region;
}
@@ -72,6 +72,39 @@ class CORE_EXPORT QgsGoogleMapsGeocoder : public QgsGeocoderInterface
*/
void setEndpoint( const QString &endpoint );

/**
* Returns the API key which will be used when accessing the Google Maps API.
*
* \see setApiKey()
*/
QString apiKey() const;

/**
* Sets the API \a key to use when accessing the Google Maps API.
*
* All use of this geocoder will be associated with the specified key for Google's billing purposes!
*
* \see apiKey()
*/
void setApiKey( const QString &key );

/**
* Returns the optional region bias which will be used to prioritize results in a certain region.
*
* \see setRegion()
*/
QString region() const;

/**
* Sets the optional \a region bias which will be used to prioritize results in a certain region.
*
* The \a region argument must be set to a two letter country code top-level domain value,
* e.g. "gb" for Great Britain.
*
* \see region()
*/
void setRegion( const QString &region );

private:

QString mApiKey;
@@ -59,6 +59,21 @@ def tearDownClass(cls):
QgsSettings().clear()
# shutil.rmtree(cls.basetestpath, True)

def test_basic(self):
"""
Basic tests
"""
geocoder = QgsGoogleMapsGeocoder('my key')
self.assertEqual(geocoder.apiKey(), 'my key')
geocoder.setApiKey('ggggg')
self.assertEqual(geocoder.apiKey(), 'ggggg')

self.assertFalse(geocoder.region())
geocoder.setRegion('xx')
self.assertEqual(geocoder.region(), 'xx')

self.assertEqual(geocoder.requestUrl('20 green st, twaddlingham').toString(), 'https://maps.googleapis.com/maps/api/geocode/json?region=xx&sensor=false&address=20 green st, twaddlingham&key=ggggg')

def test_url(self):
geocoder = QgsGoogleMapsGeocoder('my key')
geocoder.setEndpoint(self.endpoint)

0 comments on commit e70caeb

Please sign in to comment.
You can’t perform that action at this time.