Code Monkey home page Code Monkey logo

notion-sdk-java's Introduction

Notion in Java

An SDK focused on making Notion API simple.

Not recommended for production environment
The basic functions of this project are still under development, and it is only recommended to try them in non-critical applications.

Project Structure

This project is composed of the following parts:

Model and Static Method

Contains all data structures and endpoint definitions used by Notion API. Most of the data structure has added static factories for use. There are also many serializers and deserializers in this package to ensure that the model meets the requirements of the Notion API.

API Client

Responsible for initializing and managing HTTP Client and JSON Processor. It also provide log output and more readable exception classes.

This is a well-configured client, developers can easily implement Notion API endpoint operations through API Client in default setting. However, using API Client requires developers to be sufficiently familiar with Notion API and its rules, or you may be confused with many methods and usage rules.

For example, page is actually a type of block, so if you want to delete a page, you should use Delete a block endpoint

Notion Cient

Notion Client will organize low-level API operations to provide an intuitive and friendly CRUD development experience.

It will package the model into an entity. Similar to the session of the ORM framework, developers do not need to care about the update and remove operations of data in most cases. Notion Client will take care of these for you.

Notice: Notion Client is still in the early stages of development.

Getting started

Setup

Gradle

compile 'tw.yukina.notion.sdk:notion-sdk-java:0.0.4'

Maven

<dependency>
  <groupId>tw.yukina.notion.sdk</groupId>
  <artifactId>notion-sdk-java</artifactId>
  <version>0.0.4</version>
</dependency>

If your development environment does not include logging library, you may need to add one:

Gradle

compile 'ch.qos.logback:logback-classic:1.2.6'

Maven

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.6</version>
</dependency>

Usage

Notion Client

In Notion Client, you don't need to explicitly call update or remove methods. Just remember to flush Notion Client at the end of the transaction.

import tw.yukina.notion.sdk.client.NotionClient;
import tw.yukina.notion.sdk.client.NotionClientImpl;
import tw.yukina.notion.sdk.model.page.Page;
import tw.yukina.notion.sdk.model.page.property.EmailProperty;

NotionClient notionClient = new NotionClientImpl(yourBotToken);
Page page = notionClient.getPageByUuid(yourPageUuid);

EmailProperty emailProperty = (EmailProperty)page.getPropertyMap().get("Email");
emailProperty.setEmail("[email protected]");

notionClient.flush();

Manually operate API Client

Use ApiClientFactory to create an API Client. The applyDefaultSetting method will handle most of the client settings.

import tw.yukina.notion.sdk.client.api.ApiClient;
import tw.yukina.notion.sdk.client.api.ApiClientFactory;

ApiClientFactory apiClientFactory = new ApiClientFactory();
apiClientFactory.setToken(yourBotToken);
apiClientFactory.applyDefaultSetting();
ApiClient apiClient = apiClientFactory.build();

Then you can directly operate the Notion API endpoint like this.

import tw.yukina.notion.sdk.model.block.Block;
import tw.yukina.notion.sdk.model.endpoint.block.ResponseBlockList;

ResponseBlockList responseBlockList = apiClient
      .retrieveBlockChildren("889530e779fa4f6096ff4c5ad4edcfac");

List<Block> blocks = responseBlockList.getBlocks();
for(Block block: blocks)System.out.println(block.toString());

Now let's add a Heading with italics and color to the page:

import tw.yukina.notion.sdk.builder.TextBuilder;
import tw.yukina.notion.sdk.model.block.heading.HeadingOneBlock;
import tw.yukina.notion.sdk.model.block.heading.HeadingBlockHelper;
import tw.yukina.notion.sdk.model.endpoint.block.RequestAppendChildrenBlockList;

TextBuilder textBuilder = new TextBuilder();
textBuilder.setContent("Hello world").setItalic().setColor(TextColor.GREEN);

HeadingOneBlock headingOneBlock = HeadingBlockHelper
      .createDefaultHeadingOne(Arrays.asList(textBuilder.build()));

RequestAppendChildrenBlockList requestBlocks = new RequestAppendChildrenBlockList();
requestBlocks.getBlocks().add(headingOneBlock);

apiClient.appendBlockChildren("889530e779fa4f6096ff4c5ad4edcfac", requestBlocks);

It will look like this after adding it.

Known issues

  • When executing in java 11 or above, an illegal reflective access warning may appear. This is caused by our code generation library cglib. In the near future, ByteBuddy will be used instead of cglib to solve this problem.

notion-sdk-java's People

Contributors

yukinamochizuki avatar

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.