1 year ago

#381683

test-img

WJA

Best way to return a `LegacyCursorResult` object in a https call?

We can run the following python code to execute a Cloud SQL (postgresql) query:

pool = sqlalchemy.create_engine(
    # Equivalent URL:
    # postgresql+pg8000://<db_user>:<db_pass>@<db_host>:<db_port>/<db_name>
    sqlalchemy.engine.url.URL.create(
        drivername="postgresql+pg8000",
        username=self.db_user,  # e.g. "my-database-user"
        password=self.db_pass,  # e.g. "my-database-password"
        host=self.db_hostname,  # e.g. "127.0.0.1"
        port=self.db_port,  # e.g. 5432
        database=self.db_name  # e.g. "my-database-name"
    )
)
pool.dialect.description_encoding = None
self.connection = pool.connect()

result = self.connection.execute(query)

I would like to deploy this function in a Cloud Function with HTTP endpoint, that then sends a request to this endpoint and returns the result.

However, as result is of the type sqlalchemy.engine.cursor.LegacyCursorResult, this is not possible as it yields:

 The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a LegacyCursorResult.

What is the best way therefore to package this result in a return (e.g. convert to JSON or bytes?) and then unpack it at the source?

python

postgresql

sqlalchemy

google-cloud-functions

google-cloud-sql

0 Answers

Your Answer

Accepted video resources