Skip to content

Commit

Permalink
Add getter/setter for api key, region
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 3, 2020
1 parent 98d0971 commit e70caeb
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
33 changes: 33 additions & 0 deletions python/core/auto_generated/geocoding/qgsgooglemapsgeocoder.sip.in
Expand Up @@ -67,6 +67,39 @@ Converts a JSON result returned from the Google Maps service to a geocoder resul
void setEndpoint( const QString &endpoint ); void setEndpoint( const QString &endpoint );
%Docstring %Docstring
Sets a specific API ``endpoint`` to use for requests. This is for internal testing purposes only. 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 %End


}; };
Expand Down
20 changes: 20 additions & 0 deletions src/core/geocoding/qgsgooglemapsgeocoder.cpp
Expand Up @@ -251,3 +251,23 @@ void QgsGoogleMapsGeocoder::setEndpoint( const QString &endpoint )
{ {
mEndpoint = 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;
}
33 changes: 33 additions & 0 deletions src/core/geocoding/qgsgooglemapsgeocoder.h
Expand Up @@ -72,6 +72,39 @@ class CORE_EXPORT QgsGoogleMapsGeocoder : public QgsGeocoderInterface
*/ */
void setEndpoint( const QString &endpoint ); 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: private:


QString mApiKey; QString mApiKey;
Expand Down
15 changes: 15 additions & 0 deletions tests/src/python/test_qgsgooglemapsgeocoder.py
Expand Up @@ -59,6 +59,21 @@ def tearDownClass(cls):
QgsSettings().clear() QgsSettings().clear()
# shutil.rmtree(cls.basetestpath, True) # 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): def test_url(self):
geocoder = QgsGoogleMapsGeocoder('my key') geocoder = QgsGoogleMapsGeocoder('my key')
geocoder.setEndpoint(self.endpoint) geocoder.setEndpoint(self.endpoint)
Expand Down

0 comments on commit e70caeb

Please sign in to comment.