Here is an Notion API SDK for any JVM language users :wave:
This project aims to provide a Notion API client for any JVM language developers without hurdles. To realize the goal, its code is written in Kotlin with a nice consideration for Java compatibility.
This SDK works on Android runtime and any distributions based on OpenJDK. With regard to programming languages, this project provides out-of-the-box supports for Java (of course!), Kotlin, and the novel Scala 3! We don't have nice wrappers for some other JVM lanaguages such as Groovy and Clojure, but your code using this library should work in the lanaguges too.
Getting Started
You can start using this library just by adding notion-sdk-jvm-core
dependency to your project.
For Gradle users:
For Maven users:
As this library is in Kotlin, using in the same language is the smoothest :) Let's start with the following code, which manipulates Notion pages :wave:
Using in Java
Even when you use this SDK in Java and other languages, all the classes/methods should be accessible. If you find issues, please let us know the issue in this project's issue tracker.
Scala 3 Support
Although many classes are still Java/Kotlin objects, you can seamlessly use this SDK in Scala 3 too! Here is a simple build.sbt
example:
Save the following source code as Main.scala
. You can run the app by hitting sbt run
.
Plugins
By default, the NotionClient
utilizes only JDK's java.net.HttpURLConnection
and Gson library for JSON serialization.
For HTTP communications and logging, you can easily switch to other implementations.
Pluggable HTTP Client
As some may know, java.net.HttpURLConnection
does not support PATCH request method :cry:. Thus, the default httpClient
has to make an "illegal reflective access" to overcome the limitation for perfoming PATCH method requests (see this class for details).
If you use PATH method API calls such as PATCH https://api.notion.com/v1/pages/{page_id}
, we recommend other httpClient
implementations listed below. If you don't use PATCH method APIs at all and don't want to add any extra dependencies, the default httpClient
works fine for you.
java.net.HttpClient
in JDK 11+okhttp3.OkHttpClient
in OkHttp 5.x (still alpha)okhttp3.OkHttpClient
in OkHttp 4.xokhttp3.OkHttpClient
in OkHttp 3.x
You can switch the httpClient
in either of the following ways:
or
Pluggable Logging
You can change the logger
property of a NotionClient
instances Currently, this libarary supports its own stdout logger (default), java.util.logging
, and slf4j-api based ones. Here are the steps to switch to an slf4j-api logger. Add the following optional module along with your favorite implementation (e.g., logback-classic, slf4j-simple).
Now you can switch to your slf4j logger. As with the httpClient
example, you can use the setter method too.
Why isn't JSON serialization pluggable?
We don't support other JSON libraries yet. There are two reasons:
Necessity of polymorphic serializers for list objects
In the early development stage of this SDK, we started with kotlinx.serialization. It worked well except for the Search API responses. The results
returned by the API requires polymorphic serializers for properties: List<DatabaseProperty | PageProperty>
(this is a pseudo-code illustrating the property is a list of union type). We could not find a way to handle the pattern with the library at that time.
Easily enabling camelCased property names
We know a few novel Kotlin libraries do not support the conversions between snake_cased keys and camelCased keys. Although we do respect the opinion and see the benefit, we still prefer consistent field naming in the Java world. This is another major reason why we did not go with either of kotlinx.serialization and Moshi.
Supported Java Runtimes
- OpenJDK 8 or higher
- All Android runtime versions supported by Kotlin 1.5
As notion-sdk-jvm-httpclient
utilizes java.net.http.HttpClient
, the module works with JDK 11 and higher versions.
License
The MIT License