Skip to content

Commit

Permalink
Merge pull request #26 from wchatx/ddl
Browse files Browse the repository at this point in the history
add ddl method and test, bump version
  • Loading branch information
wchatx committed Aug 26, 2019
2 parents 3394add + 022f72b commit 3b039b2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,6 +6,7 @@ __pycache__/
.idea/
requirements-dev.txt
*.csv
*.sql

# C extensions
*.so
Expand Down
41 changes: 40 additions & 1 deletion directaccess/__init__.py
Expand Up @@ -60,6 +60,18 @@ def __init__(self, api_key, retries=5, backoff_factor=1, **kwargs):
self.url = self.url + '/v1/direct-access'

def query(self, dataset, **options):
"""
Query Direct Access V1 dataset
Accepts a dataset name and a variable number of keyword arguments that correspond to the fields specified in
the 'Request Parameters' section for each dataset in the Direct Access documentation.
This method only supports the JSON output provided by the API and yields dicts for each record.
:param dataset:
:param options:
:return:
"""
url = self.url + '/' + dataset

if 'page' not in options:
Expand Down Expand Up @@ -118,12 +130,39 @@ def _get_access_token(self):

return response.json()

def ddl(self, dataset, database):
"""
Get DDL statement for dataset
:param dataset: a valid dataset name. See the Direct Access documentation for valid values
:param database: one of mssql or pg. mssql is Microsoft SQL Server, pg is PostgreSQL
:return: a DDL statement from the Direct Access service
"""
url = self.url + '/' + dataset
if database != 'pg' and database != 'mssql':
raise DAQueryException('Valid values for ddl database parameter are "pg" or "mssql"')
self.logger.info('Retrieving DDL for dataset: ' + dataset)
response = self.session.get(url, params=dict(ddl=database))
return response.content

def query(self, dataset, **options):
"""
Query Direct Access V2 dataset
Accepts a dataset name and a variable number of keyword arguments that correspond to the fields specified in
the 'Request Parameters' section for each dataset in the Direct Access documentation.
This method only supports the JSON output provided by the API and yields dicts for each record.
:param dataset:
:param options:
:return:
"""
url = self.url + '/' + dataset

while True:
if self.links:
response = self.session.get(url=self.url + self.links['next']['url'])
response = self.session.get(self.url + self.links['next']['url'])
else:
response = self.session.get(url, params=options)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -6,7 +6,7 @@
from setuptools import find_packages
from setuptools.command.install import install

VERSION = '1.2.1'
VERSION = '1.2.2'


class VerifyVersionCommand(install):
Expand Down
38 changes: 38 additions & 0 deletions tests/test_ddl.py
@@ -0,0 +1,38 @@
"""
test_ddl.py
Validate that the query method returns the expected DDL statement for Python 2.7 and Python 3.4+
"""
import os

from directaccess import DirectAccessV2

DIRECTACCESS_API_KEY = os.getenv('DIRECTACCESS_API_KEY')
DIRECTACCESS_CLIENT_ID = os.getenv('DIRECTACCESS_CLIENT_ID')
DIRECTACCESS_CLIENT_SECRET = os.getenv('DIRECTACCESS_CLIENT_SECRET')


def test_ddl():
"""
Authenticate and query Direct Access API Rigs endpoint for a MSSQL DDL statement
:return:
"""
d2 = DirectAccessV2(
api_key=DIRECTACCESS_API_KEY,
client_id=DIRECTACCESS_CLIENT_ID,
client_secret=DIRECTACCESS_CLIENT_SECRET
)

ddl = d2.ddl('rigs', database='pg')
with open('create_rigs_table.sql', mode='wb+') as f:
f.write(ddl)
f.seek(0)
for line in f:
assert line.split(b' ')[0] == b'CREATE'
break

return


if __name__ == '__main__':
test_ddl()

0 comments on commit 3b039b2

Please sign in to comment.