This Go / Golang client helps you interact with the Stein API.
Stein is a suite of programs to help you turn any Google Sheet to a database. The core Stein API provides RESTful access to your sheets.
SteinHQ
$ go get github.com/nasrul21/go-stein
package main
import (
"time"
stein "github.com/nasrul21/go-stein"
)
func main() {
client := stein.NewClient(
"https://api.steinhq.com/v1/storages/5cca0542e52a3545102c1665", // STEIN URL
// you can set the option nil, if your stein doesn't need authentication
&stein.Option{
Username: "user", // your STEIN API username
Password: "password", // your STEIN API password
Timeout: 20 * time.Second, // set timeout for Stein Request, default: 15s
},
)
...
}
client.Read(sheetName, filter, v)
Name |
Description |
Format |
Requirement |
sheetName |
Your sheet name |
String |
Required |
filter |
Filter / Query data by column name |
map[string]interface{} |
Optional (nil) |
v |
The result will be given to v |
Struct / map[string]interface{} |
Required |
|
|
|
|
// set read option
readOption := stein.ReadOption{
// don't set a `Search` field, if you wanna get all data
Search: map[string]interface{
"ID": 134,
}
}
var result []YourStruct
status, err := client.Read("Sheet1", readOption, &result)
// if you want to get the data without filter
// status, err := client.Read("Sheet1", nil, &result)
if err != nil {
log.Fatal("Error read from stein: " + err)
return
}
fmt.Printf("HTTP Status: %d \n", status)
fmt.Printf("Result : %v \n", result)
client.Insert(sheetName, body) (status, res, err)
Name |
Description |
Format |
Requirement |
sheetName |
Your sheet's name |
String |
Required |
body |
The data you want to add |
Array of Struct |
Required |
Name |
Description |
Format |
status |
HTTP Status Response |
Number (Status Code) |
res |
Response of updated range |
InsertResponse |
err |
Error message |
error |
type Employee struct {
Name string `json:"name"`
Department string `json:"department"`
Position string `json:"position"`
}
employees := []Employee{
{
Name: "Nasrul",
Department: "Technology",
Position: "Software Engineer",
},
// add another field of you want to insert multiple rows
// {
// ...
// },
}
status, res, err := client.Insert("Sheet1", employees)
if err != nil {
log.Fatal("Error insert data to stein: ", status, err.Error())
return
}
fmt.Printf("HTTP Status: %d \n", status)
fmt.Printf("Updated Range : %v \n", res)
client.Update(sheetName, set, where) (status, res, err)
Name |
Description |
Format |
Requirement |
sheetName |
Your sheet's name |
String |
Required |
set |
The column values to set |
{column: value, ...} |
Required |
where |
The column values to search for |
{column: value, ...} |
Optional |
Name |
Description |
Format |
status |
HTTP Status Response |
Number (Status Code) |
res |
Response of updated range |
UpdateResponse |
err |
Error message |
error |
employee := Employee{
Name: "Nasrul",
Department: "Technology",
Position: "Software Engineer",
}
where := map[string]interface{}{
"ID": 156,
}
status, res, err := client.Update("Sheet1", employee, where)
if err != nil {
log.Fatal("Error update data to stein: ", status, err.Error())
return
}
fmt.Printf("HTTP Status: %d \n", status)
fmt.Printf("Updated Range : %v \n", res)
client.Delete(sheetName, condition) (status, res, err)
Name |
Description |
Format |
Requirement |
sheetName |
Your sheet's name |
String |
Required |
condition |
The column values to search for |
{column: value, ...} |
Required |
Name |
Description |
Format |
status |
HTTP Status Response |
Number (Status Code) |
res |
Response of updated range |
DeleteResponse |
err |
Error message |
error |
status, res, err := client.Delete("Sheet1", map[string]interface{}{
"ID": 156,
})
if err != nil {
log.Fatal("Error delete data: ", status, err.Error())
return
}
fmt.Printf("HTTP Status: %d \n", status)
fmt.Printf("Deleted Range : %v \n", res)
MIT licensed