Skip to content

Commit

Permalink
feat(googlesheets2): first pass at passing token
Browse files Browse the repository at this point in the history
put in conditional to prevent bug

replaced specific variable to kwargs
  • Loading branch information
testinnplayin committed Aug 31, 2020
1 parent 86e0f3f commit bb803b8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
9 changes: 8 additions & 1 deletion tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class DataConnector(ToucanConnector):
type = 'MyDB'
data_source_model = 'asd'

def _retrieve_data(self, datasource):
def _retrieve_data(self, datasource, **kwargs):
return pd.DataFrame({'A': [1, 2, 3, 4, 5]})

# without offset without limit
Expand All @@ -97,6 +97,13 @@ def _retrieve_data(self, datasource):
assert res.df.reset_index(drop=True).equals(pd.DataFrame({'A': [3, 4]}))
assert res.total_count == 5

# with secrets
res = DataConnector(name='my_name').get_slice(
{}, secrets={'access_token': 'foo', 'refresh_token': 'bar'}
)
assert res.df.reset_index(drop=True).equals(pd.DataFrame({'A': [1, 2, 3, 4, 5]}))
assert res.total_count == 5


def test_explain():
class DataConnector(ToucanConnector):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class GoogleSheets2Connector(ToucanConnector):
data_source_model: GoogleSheets2DataSource

auth_flow = 'oauth2'
access_token: str

def _retrieve_data(self, data_source: GoogleSheets2DataSource) -> pd.DataFrame:
def _retrieve_data(self, data_source: GoogleSheets2DataSource, **kwargs) -> pd.DataFrame:
pass
15 changes: 11 additions & 4 deletions toucan_connectors/toucan_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,24 @@ def retry_decorator(self):
return RetryPolicy(**kwargs)

@abstractmethod
def _retrieve_data(self, data_source: ToucanDataSource):
def _retrieve_data(self, data_source: ToucanDataSource, **kwargs):
"""Main method to retrieve a pandas dataframe"""

@decorate_func_with_retry
def get_df(
self, data_source: ToucanDataSource, permissions: Optional[dict] = None
self,
data_source: ToucanDataSource,
permissions: Optional[dict] = None,
secrets: dict = {},
) -> pd.DataFrame:
"""
Method to retrieve the data as a pandas dataframe
filtered by permissions
"""
res = self._retrieve_data(data_source)
# This is to avoid having to modify every single connectors'
# _retrieve_data function
res = self._retrieve_data(data_source, **secrets)

if permissions is not None:
permissions_query = PandasConditionTranslator.translate(permissions)
permissions_query = apply_query_parameters(permissions_query, data_source.parameters)
Expand All @@ -251,6 +257,7 @@ def get_slice(
permissions: Optional[dict] = None,
offset: int = 0,
limit: Optional[int] = None,
secrets: dict = {},
) -> DataSlice:
"""
Method to retrieve a part of the data as a pandas dataframe
Expand All @@ -260,7 +267,7 @@ def get_slice(
- limit is the number of rows to retrieve
Exemple: if offset = 5 and limit = 10 then 10 results are expected from 6th row
"""
df = self.get_df(data_source, permissions)
df = self.get_df(data_source, permissions, secrets)
if limit is not None:
return DataSlice(df[offset : offset + limit], len(df))
else:
Expand Down

0 comments on commit bb803b8

Please sign in to comment.