From 9df2c9f52a1c48328fb373714d9f1d75de511511 Mon Sep 17 00:00:00 2001 From: Gene Wood Date: Fri, 13 Apr 2018 14:22:34 -0700 Subject: [PATCH 1/2] ConnectionProperties: New default property default_url_parameters Sometimes, an API expects you to repeat a get parameter for each call; for example, Microsoft's Bing Maps API expects the query key to be passed as a url parameter with each API call, as well as the output format Default url parameters are passed implicitly with each call, regardless of method type. You can override the default by passing another value for the parameter explicitly; or, you can remove the default parameter from a call by passing None instead This is a replay of 63e65800ec53b91d6b631a2010132f5f0b7cab64 from the new/bing_default_url-parameters branch --- agithub/base.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/agithub/base.py b/agithub/base.py index 8e2b12e..a2ae668 100644 --- a/agithub/base.py +++ b/agithub/base.py @@ -213,9 +213,28 @@ def _fix_headers(self, headers): headers[k] = v return headers - def urlencode(self, params): - if not params: - return '' + def urlencode(self, userParams): + ''' + URL Encode the parameters for the request + + If the api specifies default parameters, these can be overridden + explicitly; or, they can be disabled by setting param=None on a + given request + ''' + if self.prop.default_url_parameters is None: + if userParams is None: + return '' + else: + params = userParams + else: + params = self.prop.default_url_parameters.copy(); + for k,v in userParams.items(): + params[k] = v + + # user can pass None to un-set a default param for one + # request + params = dict((k, v) for k, v in params.items() if v is not None) + return '?' + urllib.parse.urlencode(params) def get_connection(self): @@ -381,7 +400,9 @@ def application_json(self): # Insert new Request media-type handlers here class ConnectionProperties(object): - __slots__ = ['api_url', 'url_prefix', 'secure_http', 'extra_headers'] + __slots__ = [ + 'api_url', 'url_prefix', 'secure_http', 'extra_headers' + , 'default_url_parameters' ] def __init__(self, **props): # Initialize attribute slots From 6fb744c7142dd668de9364a8f81c086c78609344 Mon Sep 17 00:00:00 2001 From: Gene Wood Date: Fri, 13 Apr 2018 14:24:11 -0700 Subject: [PATCH 2/2] New API: BingMaps This tracks the Bing Maps API; optionally, you can pass the query key when instantiating the API (init param is "queryKey"), rather than on each request This is a replay of 0f6124d06f9074349ee320234b8789345bb0ebd7 from the new/bing_default_url-parameters branch --- agithub/BingMaps.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 agithub/BingMaps.py diff --git a/agithub/BingMaps.py b/agithub/BingMaps.py new file mode 100644 index 0000000..308cbee --- /dev/null +++ b/agithub/BingMaps.py @@ -0,0 +1,19 @@ +# Copyright 2012-2016 Jonathan Paugh and contributors +# See COPYING for license details +from agithub.base import API, ConnectionProperties, Client + +class BingMaps(API): + ''' + Bing REST API + ''' + def __init__(self, *args, **kwargs): + props = ConnectionProperties( + api_url = 'spatial.virtualearth.net' + , url_prefix = '/REST/v1' + , secure_http = True + , default_url_parameters = { + 'key' : kwargs.pop('queryKey', None) + } + ) + self.setClient(Client(*args, **kwargs)) + self.setConnectionProperties(props)