Created
July 26, 2016 21:33
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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