Git Product home page Git Product logo

go-notion's Introduction

go-notion

tests Go Reference Go Report Card codecov

๐Ÿšง Working In Progress

Go SDK for Notion Official API.

go get github.com/sorcererxw/go-notion

Overview

go-notion is the Golang binding for Notion official API. This package provides:

  • Easy-to-use and well-testing API wrappers.

  • Complete type definition.

You can easily and quickly build notion integrations with this package.

โš ๏ธ Notion official API is still in public beta, it's hard to guarantee forward compatibility in the future. This package will be continuously updated according to the official documentation.

Getting Started

At the beginning, you should follow the official document to create your workspace and integrations.

package main

import (
	"context"

	"github.com/sorcererxw/go-notion"
)

func main() {
	client := notion.NewClient(notion.Settings{Token: "token"})

	database, err := client.RetrieveDatabase(context.Background(), "database_id")
}

Pagination

package main

func main() {
	var cursor string
	for {
		data, nextCursor, hasMore, err := client.ListAllUsers(context.Background(), 30, cursor)
		if err != nil {
			break
		}
		if !hasMore {
			break
		}
		cursor = nextCursor
	}
}

Error Handling

go-notion declares error codes . You can compare error code to confirm which error occurred.

package main

import (
	"context"
	"fmt"

	"github.com/sorcererxw/go-notion"
)

func main() {
	user, err := client.RetrieveUser(context.Background(), "user_id")
	if err, ok := notion.AsError(err); ok {
		switch err.Code {
		case notion.ErrCodeRateLimited:
			fmt.Println("rate limited")
		}
	}
}

Reverse Proxy

If you cannot access Notion server in your region(e.g. China) directly, you can use reverse proxy to solve the problem:

package main

import "github.com/sorcererxw/go-notion"

const proxyEndpoint = "https://1.1.1.1/notion"

func main() {
  client := notion.NewClient(notion.Settings{
      Token: "token",
      Endpoint: proxyEndpoint,
  })
}

OAuth

package main

import "net/http"

func main() {
  client := notion.NewOAuthClient("client_id", "client_secret", "redirect_uri")

  mux := http.NewServeMux()
  mux.HandleFunc("/oauth", func(w http.ResponseWriter, r *http.Request) {
    code := r.URL.Query().Get("code")
    token, _ := client.ExchangeAccessToken(r.Context(), code)
    
    // store token to db ...
    
    w.WriteHeader(http.StatusOK)
  })
}

License

go-notion is distributed under MIT.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.