-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite API to match JS SDK semantics
A number of changes to conform to the standards set up in the official JS SDK. - Resources are now namespaced on the client. Rather than calling `client#get_users`, for example, you now call `client#users.list`. - Prefer `list` and `retrieve` over `get`. - Rename endpoints to resources. - Not all API interactions deal with resources. `search` is moved into its own operations/ folder.
- Loading branch information
Showing
22 changed files
with
351 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
require "httparty" | ||
require "forwardable" | ||
|
||
require "notion-sdk-ruby/version" | ||
|
||
require "notion-sdk-ruby/config" | ||
require "notion-sdk-ruby/resources/blocks" | ||
require "notion-sdk-ruby/resources/databases" | ||
require "notion-sdk-ruby/resources/pages" | ||
require "notion-sdk-ruby/resources/search" | ||
require "notion-sdk-ruby/resources/users" | ||
require "notion-sdk-ruby/resources" | ||
require "notion-sdk-ruby/operations/search" | ||
require "notion-sdk-ruby/error" | ||
require "notion-sdk-ruby/request_client" | ||
require "notion-sdk-ruby/client" | ||
|
||
module Notion | ||
@config = Config.new | ||
|
||
class << self | ||
extend Forwardable | ||
|
||
attr_reader :config | ||
|
||
def_delegators :@config, :api_token, :api_token= | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,25 @@ | ||
module Notion | ||
class Client | ||
include HTTParty | ||
include Resources | ||
|
||
base_uri "https://api.notion.com" | ||
headers "Content-Type": "application/json" | ||
include Operations::Search | ||
|
||
def initialize(token:) | ||
self.class.headers({Authorization: "Bearer #{token}"}) | ||
end | ||
|
||
private | ||
|
||
def get(*args, &block) | ||
response = self.class.get(*args, &block) | ||
raise_on_failure(response) | ||
end | ||
|
||
def post(*args, &block) | ||
response = self.class.post(*args, &block) | ||
raise_on_failure(response) | ||
Notion.api_token = token | ||
end | ||
|
||
def patch(*args, &block) | ||
response = self.class.patch(*args, &block) | ||
raise_on_failure(response) | ||
def blocks | ||
Blocks.new | ||
end | ||
|
||
def put(*args, &block) | ||
response = self.class.put(*args, &block) | ||
raise_on_failure(response) | ||
def databases | ||
Databases.new | ||
end | ||
|
||
def delete(*args, &block) | ||
response = self.class.delete(*args, &block) | ||
raise_on_failure(response) | ||
def pages | ||
Pages.new | ||
end | ||
|
||
def raise_on_failure(response) | ||
if response.success? | ||
response | ||
else | ||
raise ErrorFactory.create(response) | ||
end | ||
def users | ||
Users.new | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Notion | ||
class Config | ||
attr_accessor :api_token | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Notion | ||
module Operations | ||
module Search | ||
def search(body) | ||
RequestClient.active_client.post("/v1/search", body: body.to_json) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Notion | ||
class RequestClient | ||
include HTTParty | ||
|
||
base_uri "https://api.notion.com" | ||
headers "Content-Type": "application/json" | ||
|
||
def self.active_client | ||
RequestClient.new(Notion.config) | ||
end | ||
|
||
def initialize(config) | ||
self.class.headers Authorization: "Bearer #{config.api_token}" | ||
end | ||
|
||
def get(*args, &block) | ||
response = self.class.get(*args, &block) | ||
raise_on_failure(response) | ||
end | ||
|
||
def post(*args, &block) | ||
response = self.class.post(*args, &block) | ||
raise_on_failure(response) | ||
end | ||
|
||
def patch(*args, &block) | ||
response = self.class.patch(*args, &block) | ||
raise_on_failure(response) | ||
end | ||
|
||
def put(*args, &block) | ||
response = self.class.put(*args, &block) | ||
raise_on_failure(response) | ||
end | ||
|
||
def delete(*args, &block) | ||
response = self.class.delete(*args, &block) | ||
raise_on_failure(response) | ||
end | ||
|
||
def raise_on_failure(response) | ||
if response.success? | ||
response | ||
else | ||
raise ErrorFactory.create(response) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
module Notion | ||
module Blocks | ||
def get_block_children(id, params: {}) | ||
get("/v1/blocks/#{id}/children", query: params) | ||
class Blocks | ||
def children | ||
Children.new | ||
end | ||
end | ||
|
||
class Children | ||
def list(block_id, query: {}) | ||
RequestClient.active_client.get("/v1/blocks/#{block_id}/children", query: query) | ||
end | ||
|
||
def append_block_children(id, body) | ||
patch("/v1/blocks/#{id}/children", body: body.to_json) | ||
def append(block_id, body) | ||
RequestClient.active_client.patch("/v1/blocks/#{block_id}/children", body: body.to_json) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
module Notion | ||
module Databases | ||
def get_database(id) | ||
get("/v1/databases/#{id}") | ||
class Databases | ||
def retrieve(id) | ||
RequestClient.active_client.get("/v1/databases/#{id}") | ||
end | ||
|
||
def get_databases | ||
get("/v1/databases") | ||
def list | ||
RequestClient.active_client.get("/v1/databases") | ||
end | ||
|
||
def query_database(id, body) | ||
post("/v1/databases/#{id}/query", body: body.to_json) | ||
def query(id, body) | ||
RequestClient.active_client.post("/v1/databases/#{id}/query", body: body.to_json) | ||
end | ||
end | ||
end |
Oops, something went wrong.