Smartsheet Python SDK and Pydantic Wrapper
Project description
smartsheet-pydantic
Smartsheet Python SDK wrapper, incorporating Pydantic models for type validation to guarantee successful data read/updates into Smartsheet.
Smartsheet API requires the incoming data to comply with Smartsheet column types. When the types do not align then the write call can fail. In order streamline the data validation this package piggybacks off the Pydantic BaseModel structure and their data validation capabilities as a "SmartModel".
This package also provides frameworks to create a "DataSource" class which you can declaratively define your data source and attach it to the SmartModel, so you can have a standard method of querying your datasource and create/update into Smartsheet.
Installation
requires python greater than 3.10
Standard installation using pip install, and package import.
pip install smartsheet-pydantic
# The package name uses underscore "_" instead of hyphen
import smartsheet_pydantic
Example
Defining a DataSource
# DataSource class to call a RESTful API endpoint.
class ExampleRestAPI(DataSourceAPI):
url = 'http://127.0.0.1:8000/weather_data'
# DataSource class for PostgreSQL database
class ExamplePostgres(DataSourcePostgres):
user: str = "username"
password: str = "password"
host: str = "host name"
database: str = "database name"
query: str = "query string"
columns: list[str] = [ "index", "date", "value" ]
Defining a SmartModel
from datetime import date
class PostgresData(SmartModel):
index: str
date: date
value: int | None
class Configuration
"""
unique_key: this key is used to match existing data in Smartsheet when
data is being updated. add multiple columns to make the
data unique.
key_mapping: this dictionary is used to force match the source column
with the SmartModel attribute names if they do not match.
enter none if the names are exactly the same.
"""
unique_key: list[str] = ['index']
key_mapping: dict = None
Generating the controller
The controller is what utilizes the DataSource and SmartModels to read/write/update to and from Smartsheet. We can enter all necessary data into the SheetDetail typed dictionary to more conveniently to generate a new controller.
from smartsheet_pydantic import \
SheetDetail, SmartsheetControllerFactory, SmartModel, DataSource
sheet_detail = SheetDetail(
sheet_id: int = 0123456789
description: str = "Description of the target Smartsheet"
smart_model: SmartModel = PostgresData # created in the previous example
source: DataSource = ExamplePostgres # created in the previous example
)
controller_factory = SmartsheetControllerFactory(sheet_detail)
controller = controller_factory.get_controller()
Using the controller
# Extract all data from the target Smartsheet with pre-defined data validation.
data: list[PostgresData] = controller.extract_as_smartmodel()
# Write new or update existing data into Smartsheet
data: list[PostgresData]
controller.update_rows(data)
# Using the pre-defined data source, refresh the Smartsheet. This process is
# additive, so based on the unique_key it will either update the values, or add
# new values. But it will not delete any unreferenced rows.
controller.create_or_update()
# Delete the entire content of the Smartsheet (maintains existing columns).
# Use this when there is no repercussion for deleting existing data, and you
# want to populate the sheet with fresh new data.
controller.delete_all_rows()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file smartsheet-pydantic-1.2.0.tar.gz
.
File metadata
- Download URL: smartsheet-pydantic-1.2.0.tar.gz
- Upload date:
- Size: 15.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 096651ea9b533296dae773e0610106941a32e7fbc85d5cce53ede8ab92aee054 |
|
MD5 | 827a78dc9d3508f8072d6c1e601268c6 |
|
BLAKE2b-256 | 1c553c342b05ab686071130adf5911858d468ce341186dbc7453a5209699981f |
File details
Details for the file smartsheet_pydantic-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: smartsheet_pydantic-1.2.0-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3fe5cb3767ef2b4751007d48399cadf88181ed6d70c4923dd812d8e894b947b |
|
MD5 | 140a774e8e664c072417c35ea29cb6e9 |
|
BLAKE2b-256 | e55779762f00e3d4b90577d57be5b9fc5c1b8e6d56189566f99827cdd1b5f8ff |