Class: Notion::Client
- Inherits:
-
Object
- Object
- Notion::Client
- Defined in:
- lib/notion/client.rb,
lib/notion/compat/legacy.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #batch(concurrency: 3) {|jobs| ... } ⇒ Object
- #block_children(block_id:, **params) ⇒ Object
- #clear_schema_cache(data_source_id = nil) ⇒ Object
- #create_page(**params) ⇒ Object
- #database(database_id:) ⇒ Object
- #database_query(database_id:, **params) ⇒ Object
- #experimental ⇒ Object
- #import_file ⇒ Object
-
#initialize(base_url: "https://api.notion.com", **options) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
- #modern_search ⇒ Object
- #page(page_id:) ⇒ Object
- #query(id, name: nil, **params) ⇒ Object
- #request(method, path, query: {}, headers: {}, body: nil, idempotent: nil) ⇒ Object
- #resolve_data_source(database_id:, name: nil) ⇒ Object
- #schema_for(data_source_id) ⇒ Object
- #search(**params, &block) ⇒ Object
- #upload_file ⇒ Object
Constructor Details
#initialize(base_url: "https://api.notion.com", **options) ⇒ Client
Returns a new instance of Client.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/notion/client.rb', line 29 def initialize(base_url: "https://api.notion.com", **) @config = Notion.configuration.merge(**) @config.betas = @config.betas.dup.freeze @config.freeze adapter = @config.adapter adapter = nil if adapter == :net_http adapter ||= Transport::NetHTTPAdapter.new(@config, base_url: base_url) @app = Middleware::Stack.build(adapter, @config) @endpoints = {} @endpoints_mutex = Mutex.new @resolver = Resolver.new(self) @file_uploader = FileUploader.new(self) @schema_cache = {} @schema_mutex = Mutex.new end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
27 28 29 |
# File 'lib/notion/client.rb', line 27 def config @config end |
Instance Method Details
#batch(concurrency: 3) {|jobs| ... } ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/notion/client.rb', line 97 def batch(concurrency: 3) raise ArgumentError, "a batch block is required" unless block_given? jobs = Batch.new(concurrency) yield jobs jobs.run end |
#block_children(block_id:, **params) ⇒ Object
29 30 31 32 |
# File 'lib/notion/compat/legacy.rb', line 29 def block_children(block_id:, **params) deprecate(:block_children, "blocks.children") blocks.children(block_id: block_id, **params) end |
#clear_schema_cache(data_source_id = nil) ⇒ Object
123 124 125 126 127 128 |
# File 'lib/notion/client.rb', line 123 def clear_schema_cache(data_source_id = nil) config.cache&.delete("notion:schema:#{data_source_id}") if data_source_id @schema_mutex.synchronize do data_source_id ? @schema_cache.delete(data_source_id) : @schema_cache.clear end end |
#create_page(**params) ⇒ Object
24 25 26 27 |
# File 'lib/notion/compat/legacy.rb', line 24 def create_page(**params) deprecate(:create_page, "pages.create") pages.create(**params) end |
#database(database_id:) ⇒ Object
14 15 16 17 |
# File 'lib/notion/compat/legacy.rb', line 14 def database(database_id:) deprecate(:database, "databases.retrieve") databases.retrieve(database_id: database_id) end |
#database_query(database_id:, **params) ⇒ Object
9 10 11 12 |
# File 'lib/notion/compat/legacy.rb', line 9 def database_query(database_id:, **params, &) deprecate(:database_query, "query") query(database_id, **params, &) end |
#experimental ⇒ Object
105 106 107 |
# File 'lib/notion/client.rb', line 105 def experimental @endpoints_mutex.synchronize { @experimental ||= Experimental.new(self) } end |
#import_file ⇒ Object
93 94 95 |
# File 'lib/notion/client.rb', line 93 def import_file(**) @file_uploader.import(**) end |
#inspect ⇒ Object
64 65 66 |
# File 'lib/notion/client.rb', line 64 def inspect "#<#{self.class} notion_version=#{config.notion_version.inspect}>" end |
#modern_search ⇒ Object
7 8 9 |
# File 'lib/notion/compat/legacy.rb', line 7 def search(**params, &) endpoint(:search).search(**params, &) end |
#page(page_id:) ⇒ Object
19 20 21 22 |
# File 'lib/notion/compat/legacy.rb', line 19 def page(page_id:) deprecate(:page, "pages.retrieve") pages.retrieve(page_id: page_id) end |
#query(id, name: nil, **params) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/notion/client.rb', line 80 def query(id, name: nil, **params, &) return data_sources.query(data_source_id: id, **params, &) if config.notion_version == "2022-06-28" data_source_id = resolve_data_source(database_id: id, name: name) data_sources.query(data_source_id: data_source_id, **params, &) rescue ObjectNotFoundError data_sources.query(data_source_id: id, **params, &) end |
#request(method, path, query: {}, headers: {}, body: nil, idempotent: nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/notion/client.rb', line 45 def request(method, path, query: {}, headers: {}, body: nil, idempotent: nil) method = method.to_sym raise ArgumentError, "path must start with /" unless path.start_with?("/") response = @app.call( Transport::Request.new( verb: method, path: path, query: query.compact, headers: headers.transform_keys { |key| key.to_s.downcase }, body: body, idempotent: idempotent.nil? ? %i[get delete].include?(method) : idempotent ) ) raise APIError.from_response(response) unless response.status.between?(200, 299) ObjectFactory.build(response.body) end |
#resolve_data_source(database_id:, name: nil) ⇒ Object
76 77 78 |
# File 'lib/notion/client.rb', line 76 def resolve_data_source(database_id:, name: nil) @resolver.resolve(database_id, name: name) end |
#schema_for(data_source_id) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/notion/client.rb', line 109 def schema_for(data_source_id) cache_key = "notion:schema:#{data_source_id}" external = config.cache&.read(cache_key) return external if external @schema_mutex.synchronize do @schema_cache[data_source_id] ||= begin schema = data_sources.retrieve(data_source_id: data_source_id).raw.fetch("properties", {}) config.cache&.write(cache_key, schema, expires_in: 900) schema end end end |
#search(**params, &block) ⇒ Object
72 73 74 |
# File 'lib/notion/client.rb', line 72 def search(**params, &) endpoint(:search).search(**params, &) end |
#upload_file ⇒ Object
89 90 91 |
# File 'lib/notion/client.rb', line 89 def upload_file(**) @file_uploader.upload(**) end |