-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow passing extra param to execQuery (for direct URL access) (#…
…14) * refactor: update the json config parsing for clarity * fix: handle data extract zip entirely in memory * feat: allow passing extra params to queryExec, return URL if fgb * test: for generating data extracts, zip and fgb formats * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: add extra error handling for extract respone json * test: add conftest to init logger * refactor: update error handling for data extract download * feat: add optional support for auth token with remote query * refactor: extra debug logging for raw data api polling * fix: handle raw-data-api status PENDING and STARTED --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
9888975
commit 0785381
Showing
5 changed files
with
323 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
# <[email protected]> | ||
|
||
import argparse | ||
import asyncio | ||
import json | ||
import logging | ||
import os | ||
|
@@ -28,13 +29,11 @@ | |
import zipfile | ||
from io import BytesIO | ||
from pathlib import Path | ||
from sys import argv | ||
from urllib.parse import urlparse | ||
|
||
import asyncpg | ||
import geojson | ||
import requests | ||
import asyncio | ||
import asyncpg | ||
from asyncpg import exceptions | ||
from geojson import Feature, FeatureCollection, Polygon | ||
from shapely import wkt | ||
from shapely.geometry import Polygon, shape | ||
|
@@ -48,39 +47,41 @@ | |
# Instantiate logger | ||
log = logging.getLogger(__name__) | ||
|
||
|
||
class DatabaseAccess(object): | ||
def __init__(self): | ||
"""This is a class to setup a database connection.""" | ||
self.pg = None | ||
self.dburi = None | ||
self.qc = None | ||
|
||
async def connect(self, | ||
dburi: str, | ||
): | ||
async def connect( | ||
self, | ||
dburi: str, | ||
): | ||
self.dburi = dict() | ||
uri = urlparse(dburi) | ||
if not uri.username: | ||
self.dburi['dbuser'] = os.getenv("PGUSER", default=None) | ||
if not self.dburi['dbuser']: | ||
log.error(f"You must specify the user name in the database URI, or set PGUSER") | ||
self.dburi["dbuser"] = os.getenv("PGUSER", default=None) | ||
if not self.dburi["dbuser"]: | ||
log.error("You must specify the user name in the database URI, or set PGUSER") | ||
else: | ||
self.dburi['dbuser'] = uri.username | ||
self.dburi["dbuser"] = uri.username | ||
if not uri.password: | ||
self.dburi['dbpass'] = os.getenv("PGPASSWORD", default=None) | ||
if not self.dburi['dbpass']: | ||
log.error(f"You must specify the user password in the database URI, or set PGPASSWORD") | ||
self.dburi["dbpass"] = os.getenv("PGPASSWORD", default=None) | ||
if not self.dburi["dbpass"]: | ||
log.error("You must specify the user password in the database URI, or set PGPASSWORD") | ||
else: | ||
self.dburi['dbpass'] = uri.password | ||
self.dburi["dbpass"] = uri.password | ||
if not uri.hostname: | ||
self.dburi['dbhost'] = os.getenv("PGHOST", default="localhost") | ||
self.dburi["dbhost"] = os.getenv("PGHOST", default="localhost") | ||
else: | ||
self.dburi['dbhost'] = uri.hostname | ||
self.dburi["dbhost"] = uri.hostname | ||
|
||
slash = uri.path.find('/') | ||
self.dburi['dbname'] = uri.path[slash + 1:] | ||
slash = uri.path.find("/") | ||
self.dburi["dbname"] = uri.path[slash + 1 :] | ||
connect = f"postgres://{self.dburi['dbuser']}:{ self.dburi['dbpass']}@{self.dburi['dbhost']}/{self.dburi['dbname']}" | ||
|
||
if self.dburi["dbname"] == "underpass": | ||
# Authentication data | ||
# self.auth = HTTPBasicAuth(self.user, self.passwd) | ||
|
@@ -292,11 +293,11 @@ async def createTable( | |
|
||
return True | ||
|
||
async def execute(self, | ||
sql: str, | ||
): | ||
""" | ||
Execute a raw SQL query and return the results. | ||
async def execute( | ||
self, | ||
sql: str, | ||
): | ||
"""Execute a raw SQL query and return the results. | ||
Args: | ||
sql (str): The SQL to execute | ||
|
@@ -441,17 +442,18 @@ def __init__( | |
# output: str = None | ||
): | ||
"""This is a client for a postgres database. | ||
Returns: | ||
(PostgresClient): An instance of this class | ||
""" | ||
super().__init__() | ||
self.qc = None | ||
|
||
async def loadConfig(self, | ||
config: str, | ||
): | ||
""" | ||
Load the JSON or YAML config file that defines the SQL query | ||
async def loadConfig( | ||
self, | ||
config: str, | ||
): | ||
"""Load the JSON or YAML config file that defines the SQL query | ||
Args: | ||
config (str): The filespec for the query config file | ||
|
@@ -534,6 +536,7 @@ async def execQuery( | |
collection = await self.queryRemote(request) | ||
return collection | ||
|
||
|
||
async def main(): | ||
"""This main function lets this class be run standalone by a bash script.""" | ||
parser = argparse.ArgumentParser( | ||
|
@@ -601,9 +604,9 @@ async def main(): | |
|
||
log.debug(f"Wrote {args.outfile}") | ||
|
||
|
||
if __name__ == "__main__": | ||
"""This is just a hook so this file can be run standalone during development.""" | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
loop.run_until_complete(main()) | ||
|
Oops, something went wrong.