From c216e142e362148ff8bfeb9a5c493e69befd3158 Mon Sep 17 00:00:00 2001 From: vinicvaz Date: Fri, 21 Jun 2024 08:17:20 -0300 Subject: [PATCH] add files --- LICENSE | 21 ++++++++++++++ README.md | 3 +- config.toml | 12 ++++++++ dependencies/requirements_0.txt | 2 ++ pieces/GetGoogleSheetPiece/metadata.json | 25 ++++++++++++++++ pieces/GetGoogleSheetPiece/models.py | 24 +++++++++++++++ pieces/GetGoogleSheetPiece/piece.py | 29 +++++++++++++++++++ .../test_get_google_sheet_piece.py | 6 ++++ requirements-tests.txt | 2 ++ 9 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 LICENSE create mode 100644 config.toml create mode 100644 dependencies/requirements_0.txt create mode 100644 pieces/GetGoogleSheetPiece/metadata.json create mode 100644 pieces/GetGoogleSheetPiece/models.py create mode 100644 pieces/GetGoogleSheetPiece/piece.py create mode 100644 pieces/GetGoogleSheetPiece/test_get_google_sheet_piece.py create mode 100644 requirements-tests.txt diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..62d6fcf --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Tauffer Consulting + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 940d859..600ec57 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# google-apis-pieces +# [Pieces repository name] +[Description of this Pieces repository] diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..e90f702 --- /dev/null +++ b/config.toml @@ -0,0 +1,12 @@ +[repository] +# The name of the github organization / person owner, e.g. tauffer-consulting. +# Must be in lower-case letters +REGISTRY_NAME = "tauffer-consulting" + +# The name of this Pieces repository +REPOSITORY_NAME = "google-apis-pieces" +REPOSITORY_LABEL = "Google APIs Pieces" + +# The version of this Pieces release +# Attention: changing this will create a new release +VERSION = "0.1.0" \ No newline at end of file diff --git a/dependencies/requirements_0.txt b/dependencies/requirements_0.txt new file mode 100644 index 0000000..4b15320 --- /dev/null +++ b/dependencies/requirements_0.txt @@ -0,0 +1,2 @@ +pandas==2.2.2 +gspread==6.1.2 \ No newline at end of file diff --git a/pieces/GetGoogleSheetPiece/metadata.json b/pieces/GetGoogleSheetPiece/metadata.json new file mode 100644 index 0000000..cf3a2bd --- /dev/null +++ b/pieces/GetGoogleSheetPiece/metadata.json @@ -0,0 +1,25 @@ +{ + "name": "GetGoogleSheetPiece", + "description": "Read a google sheet file.", + "dependency": { + "requirements": "requirements_0.txt" + }, + "container_resources": { + "requests": { + "cpu": 100, + "memory": 128 + }, + "limits": { + "cpu": 500, + "memory": 512 + } + }, + "tags": [ + "Example" + ], + "style": { + "node_label": "Example Sine Piece", + "icon_class_name": "fa-solid:database", + "node_label": "Get Google Sheet" + } +} \ No newline at end of file diff --git a/pieces/GetGoogleSheetPiece/models.py b/pieces/GetGoogleSheetPiece/models.py new file mode 100644 index 0000000..3f43343 --- /dev/null +++ b/pieces/GetGoogleSheetPiece/models.py @@ -0,0 +1,24 @@ +from pydantic import BaseModel, Field + + +class InputModel(BaseModel): + spreadsheet_name: str = Field( + title="Spreadsheet Name", + description="Name of the Google Sheets document" + ) + worksheet_number: int = Field( + title="Worksheet Number", + description="Number of the worksheet in the Google Sheets document." + ) + + +class OutputModel(BaseModel): + output_data: dict = Field( + description="Sleep piece executed" + ) + +class SecretsModel(BaseModel): + service_account_data: str = Field( + title="Service Account", + description="Service account JSON data for Google Sheets API", + ) diff --git a/pieces/GetGoogleSheetPiece/piece.py b/pieces/GetGoogleSheetPiece/piece.py new file mode 100644 index 0000000..61ab56c --- /dev/null +++ b/pieces/GetGoogleSheetPiece/piece.py @@ -0,0 +1,29 @@ +from domino.base_piece import BasePiece +from .models import InputModel, OutputModel, SecretsModel +import pandas as pd +import gspread +import json +from tempfile import NamedTemporaryFile + + +class GetGoogleSheetPiece(BasePiece): + + def piece_function(self, input_data: InputModel, secrets_data: SecretsModel): + + service_account = secrets_data.service_account_data + if isinstance(service_account, str): + service_account = json.loads(secrets_data.service_account_data) + + with NamedTemporaryFile('w') as tmp: + json.dump(service_account, tmp) + tmp.seek(0) + gc = gspread.service_account(filename=tmp.name) + + # Open the Google Sheet by its title or URL + spreadsheet = gc.open(input_data.spreadsheet_name) + worksheet = spreadsheet.get_worksheet(input_data.worksheet_number) + records = worksheet.get_all_records() + df = pd.DataFrame.from_records(records) + + return OutputModel(output_data=df.to_json()) + diff --git a/pieces/GetGoogleSheetPiece/test_get_google_sheet_piece.py b/pieces/GetGoogleSheetPiece/test_get_google_sheet_piece.py new file mode 100644 index 0000000..103d814 --- /dev/null +++ b/pieces/GetGoogleSheetPiece/test_get_google_sheet_piece.py @@ -0,0 +1,6 @@ +from domino.testing import piece_dry_run +import os + + +def test_get_google_sheet_piece(): + assert True \ No newline at end of file diff --git a/requirements-tests.txt b/requirements-tests.txt new file mode 100644 index 0000000..392c731 --- /dev/null +++ b/requirements-tests.txt @@ -0,0 +1,2 @@ +pytest==7.4.3 +pytest-cov==4.1.0 \ No newline at end of file