Skip to content

Instantly share code, notes, and snippets.

@lsloan
Created July 26, 2016 21:33
Show Gist options
  • Save lsloan/ffa6e00f8335fd0345de55cf64aa97af to your computer and use it in GitHub Desktop.
Save lsloan/ffa6e00f8335fd0345de55cf64aa97af to your computer and use it in GitHub Desktop.
Python: This seemed like a clever way to allow my extension to requests to accept a URI containing named parameters. If the URI still contained the parameters by the time it received them, it would use the method's keyword arguments to replace them. However, being careful to pass along `**kwargs` turned out to be a problem. If the keyword argume…
def get(self, apiQueryURI, params=None, **kwargs):
"""
Append the specified query URI to the base URL, which is then sent to the REST API.
Results are returned as a requests.Response object.
:param apiQueryURI: URI for the query, to be appended to the base URL
:type apiQueryURI: str
:return: requests.Response
:rtype: requests.Response
"""
print self.methodName() + '():', apiQueryURI
apiQueryURIFormatted = apiQueryURI
if (util.stringContainsAllCharacters(apiQueryURI, '{}')):
try:
apiQueryURIFormatted = apiQueryURI.format(**kwargs)
except Exception as formattingException:
message = \
'Error while formatting query, "%s", "%s: %s"' % \
(apiQueryURI, formattingException.__class__.__name__, formattingException.message)
raise requests.RequestException(message)
return self._sendRequest(self.methodName(), apiQueryURIFormatted, params=params, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment