2021-05-13 22:11:32 +02:00
|
|
|
package notion
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Database is a resource on the Notion platform.
|
|
|
|
// See: https://developers.notion.com/reference/database
|
|
|
|
type Database struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
CreatedTime time.Time `json:"created_time"`
|
|
|
|
LastEditedTime time.Time `json:"last_edited_time"`
|
|
|
|
Title []RichText `json:"title"`
|
|
|
|
Properties DatabaseProperties `json:"properties"`
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:34:46 +02:00
|
|
|
type DatabaseProperties map[string]DatabaseProperty
|
2021-05-13 22:11:32 +02:00
|
|
|
|
2021-05-14 18:49:00 +02:00
|
|
|
// Database property metadata types.
|
2021-05-13 22:11:32 +02:00
|
|
|
type (
|
2021-05-14 18:49:00 +02:00
|
|
|
NumberMetadata struct {
|
2021-05-13 22:11:32 +02:00
|
|
|
Format string `json:"format"`
|
|
|
|
}
|
2021-05-14 18:49:00 +02:00
|
|
|
SelectMetadata struct {
|
2021-05-13 22:11:32 +02:00
|
|
|
Options []SelectOptions `json:"options"`
|
|
|
|
}
|
2021-05-14 18:49:00 +02:00
|
|
|
FormulaMetadata struct {
|
2021-05-13 22:11:32 +02:00
|
|
|
Expression string `json:"expression"`
|
|
|
|
}
|
2021-05-14 18:49:00 +02:00
|
|
|
RelationMetadata struct {
|
2021-05-13 22:11:32 +02:00
|
|
|
DatabaseID string `json:"database_id"`
|
|
|
|
SyncedPropName *string `json:"synced_property_name"`
|
|
|
|
SyncedPropID *string `json:"synced_property_id"`
|
|
|
|
}
|
2021-05-14 18:49:00 +02:00
|
|
|
RollupMetadata struct {
|
2021-05-13 22:11:32 +02:00
|
|
|
RelationPropName string `json:"relation_property_name"`
|
|
|
|
RelationPropID string `json:"relation_property_id"`
|
|
|
|
RollupPropName string `json:"rollup_property_name"`
|
|
|
|
RollupPropID string `json:"rollup_property_id"`
|
|
|
|
Function string `json:"function"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type SelectOptions struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DatabaseProperty struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
2021-05-14 18:49:00 +02:00
|
|
|
Number *NumberMetadata `json:"number"`
|
|
|
|
Select *SelectMetadata `json:"select"`
|
|
|
|
MultiSelect *SelectMetadata `json:"multi_select"`
|
|
|
|
Formula *FormulaMetadata `json:"formula"`
|
|
|
|
Relation *RelationMetadata `json:"relation"`
|
|
|
|
Rollup *RollupMetadata `json:"rollup"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata returns the underlying property metadata, based on its `type` field.
|
|
|
|
// When type is unknown/unmapped or doesn't have additional properies, `nil` is returned.
|
|
|
|
func (prop DatabaseProperty) Metadata() interface{} {
|
|
|
|
switch prop.Type {
|
|
|
|
case "number":
|
|
|
|
return prop.Number
|
|
|
|
case "select":
|
|
|
|
return prop.Select
|
|
|
|
case "multi_select":
|
|
|
|
return prop.MultiSelect
|
|
|
|
case "formula":
|
|
|
|
return prop.Formula
|
|
|
|
case "relation":
|
|
|
|
return prop.Relation
|
|
|
|
case "rollup":
|
|
|
|
return prop.Rollup
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-13 22:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FindDatabaseByID(id string) (db Database, err error) {
|
|
|
|
req, err := c.newRequest("GET", "/databases/"+id, nil)
|
|
|
|
if err != nil {
|
2021-05-14 18:24:56 +02:00
|
|
|
return Database{}, fmt.Errorf("notion: invalid URL: %w", err)
|
2021-05-13 22:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := c.httpClient.Do(req)
|
|
|
|
if err != nil {
|
2021-05-14 18:24:56 +02:00
|
|
|
return Database{}, fmt.Errorf("notion: failed to make HTTP request: %w", err)
|
2021-05-13 22:11:32 +02:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
if res.StatusCode != 200 {
|
2021-05-14 18:24:56 +02:00
|
|
|
return Database{}, fmt.Errorf("notion: failed to find database: %w", parseErrorResponse(res))
|
2021-05-13 22:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(res.Body).Decode(&db)
|
|
|
|
if err != nil {
|
2021-05-14 18:24:56 +02:00
|
|
|
return Database{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
|
2021-05-13 22:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|