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 @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() requests.packages.urllib3.disable_warnings()
if method.upper() == 'GET':
query_url = "{0}?q={1}".format(sql_api_url, query) query_url = "{0}?q={1}".format(sql_api_url, query)
print("Executing query: {0}".format(query_url))
query_response = requests.get(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: if query_response.status_code != 200:
raise Exception(json.loads(query_response.text)['error']) raise Exception(json.loads(query_response.text)['error'])
return json.loads(query_response.text) return json.loads(query_response.text)

View File

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