11import asyncio
22import logging
3+ import re
34
45import aiohttp
56import googlemaps
6- from googlemaps . client import urlencode_params , sign_hmac
7+ from googlemaps import client as gmaps_client
78from yarl import URL
89
910from . import __version__
2728class Client :
2829 def __init__ (self , key = None , client_id = None , client_secret = None ,
2930 session = None , close_session = False , verify_ssl = True ,
30- request_timeout = 10 , loop = None ):
31+ request_timeout = 10 , channel = None , loop = None ):
3132 if loop is None :
3233 loop = asyncio .get_event_loop ()
3334
@@ -45,6 +46,16 @@ def __init__(self, key=None, client_id=None, client_secret=None,
4546 self .client_secret = client_secret
4647 self .request_timeout = request_timeout
4748
49+ if channel :
50+ if not client_id :
51+ raise ValueError ("The channel argument must be used with a "
52+ "client ID" )
53+ if not re .match ("^[a-zA-Z0-9._-]*$" , channel ):
54+ raise ValueError ("The channel argument must be an ASCII "
55+ "alphanumeric string. The period (.), underscore (_)"
56+ "and hyphen (-) characters are allowed." )
57+ self .channel = channel
58+
4859 self .close_session = close_session
4960 self .verify_ssl = verify_ssl
5061
@@ -62,7 +73,7 @@ def __init__(self, key=None, client_id=None, client_secret=None,
6273
6374 self .session = session
6475
65- self .base_url = URL ('https://maps.googleapis.com/ ' )
76+ self .base_url = URL ('https://maps.googleapis.com' )
6677 self ._user_agent = 'AsyncGoogleGeoApiClientPython/{}' .format (
6778 __version__ )
6879 self ._headers = {
@@ -72,20 +83,6 @@ def __init__(self, key=None, client_id=None, client_secret=None,
7283 def __getitem__ (self , name ):
7384 return getattr (self , name )
7485
75- def _get_params (self , path , params , accepts_clientid ):
76- params = sorted (dict (params ).items ())
77- if self .key is not None :
78- if isinstance (params , (list , tuple )):
79- params .append (('key' , self .key ))
80-
81- if accepts_clientid and self .client_id and self .client_secret :
82- params .append (("client" , self .client_id ))
83- path = "?" .join ([str (path ), urlencode_params (params )])
84- sig = sign_hmac (self .client_secret , path )
85- params .append (("signature" , sig ))
86-
87- return params
88-
8986 async def _request (
9087 self ,
9188 url ,
@@ -106,22 +103,12 @@ async def _request(
106103 base_url = self .base_url
107104
108105 base_url = URL (base_url )
109- params = self ._get_params (
110- path = '/' + url .lstrip ('/' ),
111- params = params ,
112- accepts_clientid = accepts_clientid
113- )
106+ authed_url = gmaps_client .Client ._generate_auth_url (self , '/' + url .lstrip ('/' ), params , accepts_clientid )
114107
115108 if aiohttp3 :
116109 # https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.ClientSession.request
117110 kwargs .update ({'ssl' : self .verify_ssl })
118-
119- combined_url = URL (
120- "?" .join ([str (base_url / url .lstrip ('/' )),
121- urlencode_params (params )]),
122- encoded = True
123- )
124-
111+ combined_url = URL (str (base_url ) + authed_url , encoded = True )
125112 if post_json is not None :
126113 method = 'POST'
127114 data = post_json
0 commit comments