I have built an API on top of Postgres that is routed through a PLPGSQL function that always returns JSONB. eg:-
SELECT api('{"method", "foo"}'::JSONB)
Returns:-
{
"response_code" : 200,
"data" : "...."
}
I want to use this gateway to render vector tiles which are in PBF format. I have a function create_vector_tile(x,y,z) which returns BYTEA and this works perfectly if I connect a python script to it directly:-
query = DATABASE_CONNECTION.execute(create_vector_tile(%,%,%), [x,y,z])
ret = query.fetchone()[0]
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Content-type", "application/vnd.mapbox-vector-tile")
self.end_headers()
self.wfile.write(ret)
How do I decode this bytea if I return it via my API eg:-
RETURN json_build_object('response_code', 200, 'data', create_vector_tile(x,y,z))
Postgres automatically encodes the bytea as a string in the JSON object but I don't know how to decode this in python to get the same result as the direct call. I need to go via my API as it does authentication and logging.
encode()function