Skip to content

Commit

Permalink
Merge pull request #375 from CartoDB/query-post-payload-and-errors
Browse files Browse the repository at this point in the history
moves table generation query to payload instead of param
  • Loading branch information
andy-esch authored Feb 6, 2018
2 parents 6c1fddc + fc64f6d commit 777ff51
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions cartoframes/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,19 +664,18 @@ def query(self, query, table_name=None, decode_geom=False):
# bug is fixed ref: support/1127
try:
self.sql_client.send('''
create table {0} as SELECT 1;
drop table {0};
CREATE TABLE {0} AS SELECT 1;
DROP TABLE {0};
'''.format(table_name))
resp = self._auth_send(
'api/v1/imports', 'POST',
params=dict(sql=query,
# collision_strategy='',
table_name=table_name),
params=dict(table_name=table_name),
json=dict(sql=query),
# collision_strategy='',
headers={'Content-Type': 'application/json'})
except CartoException:
except CartoException as err:
raise CartoException(
'Table `{0}` already exists. Delete it before creating a '
'table from this query'.format(table_name))
'Cannot create table `{0}`: {1}'.format(table_name, err))

while True:
import_job = self._check_import(resp['item_queue_id'])
Expand Down Expand Up @@ -1658,7 +1657,10 @@ def _auth_send(self, relative_path, http_method, **kwargs):
res = self.auth_client.send(relative_path, http_method, **kwargs)
if isinstance(res.content, str):
return json.loads(res.content)
return json.loads(res.content.decode('utf-8'))
try:
return json.loads(res.content.decode('utf-8'))
except json.JSONDecodeError as err:
raise CartoException(err)

def _check_query(self, query, style_cols=None):
"""Checks if query from Layer or QueryLayer is valid"""
Expand Down
2 changes: 1 addition & 1 deletion test/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def test_cartoframes_sync(self):

@unittest.skipIf(WILL_SKIP, 'no carto credentials, skipping')
def test_cartoframes_query(self):
"""cartoframes.CartoContext.query"""
"""context.CartoContext.query"""
cc = cartoframes.CartoContext(base_url=self.baseurl,
api_key=self.apikey)
cols = ('link', 'body', 'displayname', 'friendscount', 'postedtime', )
Expand Down

0 comments on commit 777ff51

Please sign in to comment.