Skip to content

Commit

Permalink
Fix issue where response was an embedded json string (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbirddog authored Dec 22, 2023
1 parent dc01e63 commit 851001e
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/connector_postgres_v2/base_command.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from typing import Any

import psycopg2 # type: ignore
Expand Down Expand Up @@ -36,7 +35,7 @@ def _execute(self, sql: str, conn_str: str, handler: Any) -> ConnectorProxyRespo
conn.close()

command_response: CommandResponseDict = {
"body": json.dumps(command_response_body),
"body": command_response_body,
"mimetype": "application/json",
}
return_response: ConnectorProxyResponseDict = {
Expand Down Expand Up @@ -66,8 +65,14 @@ def handler(conn: Any, cursor: Any) -> None:
return self._execute(sql, conn_str, handler)

def fetchall(self, sql: str, conn_str: str, values: list) -> ConnectorProxyResponseDict:
def prep_results(results: dict) -> list:
return list(map(list, results))
def prep_results(results: list) -> list:
# takes the raw results which is a list of a single item list of strings that
# look like tuples with embedded quotes:
# - [["(1,\"some vendor\")"], ["(2,\"another vendor\")"]]
# and turns it into a list of lists of strings that represent the data for each
# column. this way the individual values can be accessed directly from task data.
# - [["1", "some vender"], ["2", "another_vendor"]]
return [r[0][1:-1].replace('"', '').split(",") for r in results]
def handler(conn: Any, cursor: Any) -> list:
cursor.execute(sql, values)
return prep_results(cursor.fetchall())
Expand Down

0 comments on commit 851001e

Please sign in to comment.