gotion

package module
v0.0.0-...-9aa71bc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2021 License: MIT Imports: 10 Imported by: 0

README

gotion

⛔ WARNING: This is a work in progress! There are no gaurantees until the package reaches v1.0.0 ⛔

Go Reference Go Report Card Maintainability Test Coverage

Go client for the Official Notion API. The goal of this project is to provide type-safety to the Notion API. Instead of page/database properties being interface{} in Go, this project tries to provide safety by having custom (un)marshal methods for the types to transition from Go-like code to the Javascript-type code that is compatible with the Notion API.

There are also some helper functions in the client that will get a page and its children from the Notion API in one call. Also, since Notion has a maximum page size for paginated requests, gotion can make multiple requests to get all results from a paginated request. Never fear: gotion is rate-limited so you won't overwhelm the Notion API.

Installation

go get github.com/thedadams/gotion

Getting started

To obtain an API key, follow Notion’s getting started guide.

Code example
import (
    "context"

    "github.com/thedadams/gotion"
)

func main() {
    client := gotion.NewClient("api-key")
    
    page, err := client.GetPage(context.Background(), "page-id")
    if err != nil {
        // Handler error
    }

    for _, prop := range page.Properties {
        fmt.Printf("Prop name %s with type %s\n", prop.Name, prop.Type)
    }

    page, err = client.GetPageAndChildren(context.Background(), "page-id")
    if err != nil {
        // Handle error
    }
    // Use page
}

Status

All the basic methods (and some helpers) are implemented to enble communciation with the Notion API with one excpetion (see next section).

  • GetPage
  • GetPageAndChildren
  • UpdatePageProperties
  • CreatePage
  • GetDatabae
  • GetDatabases
  • GetDatabaseAndChildren
  • GetBlockChildren
  • AppendBlockChildren
  • QueryDatabase
  • Search
TODO
  • Add basic examples
  • Nested Compound filters
  • Github Actions
  • Unit tests
  • Integration tests

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a client used to make calls to the Notion API.

func NewClient

func NewClient(apiKey string, options ...Option) *Client

NewClient creates a new gotion client to use with the API. By default: - the client will use the most recent accepted Notion API version. - Timeout is 30 seconds - Backoff strategy is set to pester.ExponentialJitterBackoff - Rate limiter is set to 3 requests per second - MaxRetries is set to 8 - the client will retry on 429 errors.

func (*Client) AppendBlockChildren

func (c *Client) AppendBlockChildren(ctx context.Context, block *notion.Block) (*notion.Block, error)

AppendBlockChildren adds the given blocks as children of the block with the ID provided in the Notion API. All that is needed in the notion.Block are the page ID and the blocks that need to be added. Providing the current children of the block will result in duplicate blocks. On success, the notion.Block returned will be the complete block from the Notion API. On error, the notion.Block returned is the original one.

func (*Client) CreateDatabase

func (c *Client) CreateDatabase(ctx context.Context, db *notion.Database) error

CreateDatabase will send a request to create the given database in the Notion API. All that is needed in the notion.Database object are the Parent and Properties. Optionally, a Title can be set. No IDs need to be given. On success, the notion.Database will be the complete page from the Notion API. On error, the notion.Page will not be changed.

func (*Client) CreatePage

func (c *Client) CreatePage(ctx context.Context, page *notion.Page) (*notion.Page, error)

CreatePage will send a request to create the given page in the Notion API. All that is needed in the notion.Page object are the Parent, Properties, and Children. No IDs need to be given. On success, the notion.Page returned will be the complete page from the Notion API. On error, the notion.Page returned is the original one.

func (*Client) DeleteBlock

func (c *Client) DeleteBlock(ctx context.Context, id string) error

DeleteBlock deletes a block with the given id from the Notion API.

func (*Client) GetBlock

func (c *Client) GetBlock(ctx context.Context, id string) (*notion.Block, error)

GetBlock gets a block with the given id from the Notion API.

func (*Client) GetBlockChildren

func (c *Client) GetBlockChildren(ctx context.Context, id string, cursor *string, maxResults int) ([]*notion.Block, error)

GetBlockChildren gets the children of the block with the given id from the Notion API. If `maxResults < 0`, then this will get all the children of the block.

func (*Client) GetDatabase

func (c *Client) GetDatabase(ctx context.Context, id string) (*notion.Database, error)

GetDatabase gets a database with the given id from the Notion API.

func (*Client) GetDatabaseAndChildren

func (c *Client) GetDatabaseAndChildren(ctx context.Context, id string, maxResults int) (*notion.Database, error)

GetDatabaseAndChildren gets a database with the given id from the Notion API, as well as the children of the database. If `maxResults < 0`, then this gets all children.

func (*Client) GetDatabases

func (c *Client) GetDatabases(ctx context.Context, cursor *string, maxResults int) (*notion.Databases, error)

GetDatabases gets a number of databases with from the Notion API. If `maxResults < 0`, then all databases are retrieved.

func (*Client) GetPage

func (c *Client) GetPage(ctx context.Context, id string) (*notion.Page, error)

GetPage gets a page with the given id from the Notion API. To get the contents of a page, use `GetPageWithChildren` with the page id.

func (*Client) GetPageAndChildren

func (c *Client) GetPageAndChildren(ctx context.Context, id string, maxResults int) (*notion.Page, error)

GetPageAndChildren gets a page with the given id from the Notion API, as well as the children of the page. If `maxResults < 0`, then this gets all children.

func (*Client) GetUser

func (c *Client) GetUser(ctx context.Context, id string) (*notion.User, error)

GetUser gets a user with the given id from the Notion API.

func (*Client) GetUsers

func (c *Client) GetUsers(ctx context.Context, cursor *string, maxResults int) ([]*notion.User, error)

GetUsers gets a user with the given id from the Notion API. If `maxResults < 0`, then this will get all users.

func (*Client) QueryDatabase

func (c *Client) QueryDatabase(ctx context.Context, id string, query *DBQuery) ([]*notion.Page, error)

QueryDatabase will query the database with the given id in the Notion API.

func (*Client) Search

func (c *Client) Search(ctx context.Context, query *SearchQuery) (*SearchResults, error)

Search allows searching the Notion API. Currently, this is limited by Notion. query -- search the titles of objects (pages and databases) sort -- can only sort by last_edited_time filter -- can only filter by object type: pages or databases.

func (*Client) UpdateBlock

func (c *Client) UpdateBlock(ctx context.Context, block *notion.Block) error

UpdateBlock updates a block in the Notion API. On success, the block will be the complete block from the Notion API. On error, the block will not be changed.

func (*Client) UpdateDatabase

func (c *Client) UpdateDatabase(ctx context.Context, db *notion.Database) error

UpdateDatabase updates the database in the Notion API. On success, the database is the complete database from the Notion API. On error, the database is not updated.

func (*Client) UpdatePageProperties

func (c *Client) UpdatePageProperties(ctx context.Context, page *notion.Page) error

UpdatePageProperties updates the page properties in the Notion API. All that is needed in the notion.Page are the page ID and Properties. A caller doesn't need to provide all properties, but only the updates ones. However, providing all properties works fine as well. On success, the notion.Page will be the complete page from the Notion API. On error, the notion.Page will be changed.

type DBQuery

type DBQuery struct {
	Filter     *notion.Filter `json:"filter,omitempty"`
	Sort       *notion.Sort   `json:"sort,omitempty"`
	Cursor     *string        `json:"cursor,omitempty"`
	MaxResults *int           `json:"page_size,omitempty"`
}

DBQuery represents the parameters needed to query a database in the Notion API.

type Option

type Option func(*Client)

An Option is a way of customizing the gotion client

func WithAPIVersion

func WithAPIVersion(v string) Option

WithAPIVersion uses the given Notion API version with the gotion client. It checks that the string provided is an accepted API version. If the string does not represent an accepted API version, then this is a no-op.

func WithBackoffStrategy

func WithBackoffStrategy(b pester.BackoffStrategy) Option

WithBackoffStrategy applies the given backoff strategy to the gotion client

func WithMaxRetries

func WithMaxRetries(n int) Option

WithMaxRetries sets the maximum number of retries for the gotion client.

func WithPesterClient

func WithPesterClient(pesterClient *pester.Client) Option

WithPesterClient uses the given pester client for the gotion client

func WithRateLimiter

func WithRateLimiter(r *rate.Limiter) Option

WithRateLimiter users the given rate limiter with the gotion client.

func WithRetryOnHTTP429

func WithRetryOnHTTP429() Option

WithRetryOnHTTP429 sets the gotion client to retry on 429 errors. The rate limiter should help with this as well.

func WithSettings

func WithSettings(s *notion.Settings) Option

WithSettings uses the given settings with the gotion client.

func WithTimeout

func WithTimeout(t time.Duration) Option

WithTimeout sets the timeout parameter for the gotion client.

func WithToken

func WithToken(t string) Option

WithToken uses the given auth token with the gotion client.

func WithUserAgent

func WithUserAgent(u string) Option

WithUserAgent uses the given user agent string with the gotion client.

func WithoutRetryOnHTTP429

func WithoutRetryOnHTTP429() Option

WithoutRetryOnHTTP429 sets the gotion client to NOT retry on 429 errors. The rate limiter should help with this as well.

type Result

type Result struct {
	NextCursor *string     `json:"next_cursor"`
	HasMore    bool        `json:"has_more"`
	Results    interface{} `json:"results"`
}

A Result is a response from the Notion API when getting more than one thing back (i.e. listing)

type SearchQuery

type SearchQuery struct {
	Query      string         `json:"query"`
	Filter     *notion.Filter `json:"filter,omitempty"`
	Sort       *notion.Sort   `json:"sort,omitempty"`
	Cursor     *string        `json:"cursor,omitempty"`
	MaxResults *int           `json:"page_size,omitempty"`
}

SearchQuery represents the body needed to search the Notion API. Currently, one can only filter based on object: page or database. Currently, one can only sort based on last_edited_time.

type SearchResults

type SearchResults struct {
	Pages     []*notion.Page
	Databases []*notion.Database
}

SearchResults represent the returned results from searching the Notion API.

func (*SearchResults) Len

func (sr *SearchResults) Len() int

Len returns the total number of results, pages plus databases

func (*SearchResults) UnmarshalJSON

func (sr *SearchResults) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the SearchResults into Pages and Databases.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL