Add method parameter to requests

It allows using POST for large URI to prevent errors `414 Request-URI Too Large`
This commit is contained in:
manmorjim 2020-03-02 15:24:56 +01:00
parent e3e037563b
commit 5bb0e67499
2 changed files with 15 additions and 9 deletions

View File

@ -45,11 +45,15 @@ class IntegrationTestHelper:
}
@classmethod
def execute_query_raw(cls, sql_api_url, query):
def execute_query_raw(cls, sql_api_url, query, method='GET'):
requests.packages.urllib3.disable_warnings()
if method.upper() == 'GET':
query_url = "{0}?q={1}".format(sql_api_url, query)
print("Executing query: {0}".format(query_url))
query_response = requests.get(query_url)
print("Executing query GET: {0}".format(query_url))
else:
query_response = requests.post(sql_api_url, data={"q": query})
print("Executing query POST: {0}".format(sql_api_url))
if query_response.status_code != 200:
raise Exception(json.loads(query_response.text)['error'])
return json.loads(query_response.text)

View File

@ -335,7 +335,7 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp):
"{}" \
"]''::jsonb) as (cartodb_id integer, address text)', " \
"'address', null, null, null, {})".format(','.join(streets), batch_size)
response = self._run_authenticated(query)
response = self._run_authenticated(query, method='POST')
assert_equal(n, len(response['rows']))
for row in response['rows']:
assert_not_equal(row['st_x'], None)
@ -413,12 +413,14 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp):
for r, e in zip(response['rows'], expected):
self.assert_metadata(r['metadata'], e)
def _run_authenticated(self, query):
authenticated_query = "{}&api_key={}".format(query,
self.env_variables[
'api_key'])
return IntegrationTestHelper.execute_query_raw(self.sql_api_url,
authenticated_query)
def _run_authenticated(self, query, method='GET'):
api_key = self.env_variables['api_key']
url = self.sql_api_url
auth_query = "{}&api_key={}".format(query, api_key)
if method.upper() != 'GET':
auth_query = query
url = "{}?api_key={}".format(self.sql_api_url, api_key)
return IntegrationTestHelper.execute_query_raw(url, auth_query, method)
@staticmethod
def _x_y_by_cartodb_id(response):