Skip to content

Commit

Permalink
Merge 33360c5 into 375d593
Browse files Browse the repository at this point in the history
  • Loading branch information
juanignaciosl committed Feb 28, 2018
2 parents 375d593 + 33360c5 commit 33cb3c8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
24 changes: 13 additions & 11 deletions cartoframes/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from tqdm import tqdm
from appdirs import user_cache_dir

from carto.auth import APIKeyAuthClient
from carto.auth import APIKeyAuthClient, AuthAPIClient
from carto.sql import SQLClient, BatchSQLClient
from carto.exceptions import CartoException
from carto.datasets import DatasetManager
Expand Down Expand Up @@ -105,25 +105,24 @@ def __init__(self, base_url=None, api_key=None, creds=None, session=None,
self.auth_client = APIKeyAuthClient(base_url=self.creds.base_url(),
api_key=self.creds.key(),
session=session)
self.auth_api_client = AuthAPIClient(base_url=self.creds.base_url(),
api_key=self.creds.key(),
session=session)
self.sql_client = SQLClient(self.auth_client)
self.creds.username(self.auth_client.username)
self._is_authenticated()
self._is_authenticated(self.auth_api_client)
self.is_org = self._is_org_user()

self._map_templates = {}
self._srcdoc = None
self._verbose = verbose

def _is_authenticated(self):
def _is_authenticated(self, auth_api_client):
"""Checks if credentials allow for authenticated carto access"""
try:
self.sql_client.send(
'select * from information_schema.tables limit 0')
except CartoException as err:
if not auth_api_client.is_valid_api_key():
raise CartoException('Cannot authenticate user `{0}`. Check '
'credentials ({1}).'.format(
self.creds.username(),
err))
'credentials.'.format(
self.creds.username()))

def _is_org_user(self):
"""Report whether user is in a multiuser CARTO organization or not"""
Expand Down Expand Up @@ -167,6 +166,9 @@ def read(self, table_name, limit=None, index='cartodb_id',

return self.query(query, decode_geom=decode_geom)

def datasets(self):
return DatasetManager(self.auth_client).all()

def write(self, df, table_name, temp_dir=CACHE_DIR, overwrite=False,
lnglat=None, encode_geom=False, geom_col=None, **kwargs):
"""Write a DataFrame to a CARTO table.
Expand Down Expand Up @@ -1071,7 +1073,7 @@ def data_boundaries(self, boundary=None, region=None, decode_geom=False,
"""
Find all boundaries available for the world or a `region`. If
`boundary` is specified, get all available boundary polygons for the
region specified (if any). This method is espeically useful for getting
region specified (if any). This method is especially useful for getting
boundaries for a region and, with `CartoContext.data` and
`CartoContext.data_discovery`, getting tables of geometries and the
corresponding raw measures. For example, if you want to analyze
Expand Down
12 changes: 12 additions & 0 deletions cartoframes/examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""example data factory"""
from cartoframes import CartoContext

EXAMPLE_BASE_URL = 'https://cartoframes.carto.com'
EXAMPLE_API_KEY = 'default_public'
# EXAMPLE_BASE_URL = 'http://cdb.localhost.lan:8888/'
# EXAMPLE_API_KEY = 'default_public'


def examples():
"""Returns a CartoContext with a CARTO account containing example data"""
return CartoContext(base_url=EXAMPLE_BASE_URL, api_key=EXAMPLE_API_KEY)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
carto>=1.1.0
carto>=1.2.2
pyrestcli>=0.6.3
pandas>=0.19.2
webcolors==1.7
Expand Down
13 changes: 11 additions & 2 deletions test/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import pandas as pd
import IPython
from cartoframes.utils import dict_items
from cartoframes.examples import examples

WILL_SKIP = False
warnings.filterwarnings("ignore")
Expand Down Expand Up @@ -153,15 +154,17 @@ def test_cartocontext(self):
@unittest.skipIf(WILL_SKIP, 'no carto credentials, skipping this test')
def test_cartocontext_credentials(self):
"""context.CartoContext.__init__ Credentials argument"""
creds = cartoframes.Credentials(username=self.username,
creds = cartoframes.Credentials(base_url=self.baseurl,
username=self.username,
key=self.apikey)
cc = cartoframes.CartoContext(creds=creds)
self.assertIsInstance(cc, cartoframes.CartoContext)
self.assertEqual(cc.creds.username(), self.username)
self.assertEqual(cc.creds.key(), self.apikey)

# CartoContext pulls from saved credentials
saved_creds = cartoframes.Credentials(username=self.username,
saved_creds = cartoframes.Credentials(base_url=self.baseurl,
username=self.username,
key=self.apikey)
saved_creds.save()
cc_saved = cartoframes.CartoContext()
Expand Down Expand Up @@ -1107,6 +1110,12 @@ def test_column_name_collision_do_enrichement(self):

self.assertIn('_' + dup_col, data.keys())

def test_examples(self):
e = examples()
tables = e.datasets()
# TODO: add assertions for well-known datasets
self.assertTrue(tables)


class TestBatchJobStatus(unittest.TestCase, _UserUrlLoader):
"""Tests for cartoframes.BatchJobStatus"""
Expand Down

0 comments on commit 33cb3c8

Please sign in to comment.