Class: Orchestrate::Client
- Inherits:
-
Object
- Object
- Orchestrate::Client
- Defined in:
- lib/orchestrate/client.rb
Overview
The "method client": A single entry point to an Orchestrate Application, with methods for accessing each API endpoint.
Instance Attribute Summary collapse
-
#api_key ⇒ String
readonly
The API key provided.
-
#faraday_configuration ⇒ Proc
readonly
The block used to configure faraday.
-
#host ⇒ String
readonly
The Orchestrate data center URL.
-
#http ⇒ Faraday::Connection
readonly
The Faraday HTTP connection.
Collections collapse
- #delete_collection(collection) ⇒ Object
-
#search(collection, query, options = {}) ⇒ Object
Search the items in a collection using a Lucene Query Syntax.
Key/Value collapse
- #delete(collection, key, ref = nil) ⇒ Object
- #get(collection, key, ref = nil) ⇒ Object
- #list(collection, options = {}) ⇒ Object
- #list_refs(collection, key, options = {}) ⇒ Object
-
#patch(collection, key, body, condition = nil) ⇒ Object
Manipulate values associated with a key, without retrieving the key object.
-
#patch_merge(collection, key, body, condition = nil) ⇒ Object
Merge field/value pairs into existing key, without retrieving the key object.
- #post(collection, body) ⇒ Object
- #purge(collection, key, ref = nil) ⇒ Object
- #put(collection, key, body, condition = nil) ⇒ Object (also: #put_if_unmodified)
- #put_if_absent(collection, key, body) ⇒ Object
Events collapse
- #get_event(collection, key, event_type, timestamp, ordinal) ⇒ Object
- #list_events(collection, key, event_type, options = {}) ⇒ Object
- #post_event(collection, key, event_type, body, timestamp = nil) ⇒ Object
- #purge_event(collection, key, event_type, timestamp, ordinal, ref = nil) ⇒ Object
- #put_event(collection, key, event_type, timestamp, ordinal, body, ref = nil) ⇒ Object
Graphs / Relations collapse
- #delete_relation(collection, key, kind, to_collection, to_key) ⇒ Object
- #get_relations(collection, key, *kinds) ⇒ Object
- #put_relation(collection, key, kind, to_collection, to_key) ⇒ Object
Instance Method Summary collapse
-
#in_parallel {|accumulator| ... } ⇒ Object
Performs requests in parallel.
-
#initialize(api_key, host = "https://api.orchestrate.io") {|The| ... } ⇒ Object
constructor
Instantiate a new Client for an Orchestrate application.
-
#ping ⇒ Object
Tests authentication with Orchestrate.
-
#send_request(method, path, options = {}) ⇒ Object
Performs an HTTP request against the Orchestrate API.
Constructor Details
#initialize(api_key, host = "https://api.orchestrate.io") {|The| ... } ⇒ Object
api_key -> app_url, parse api_key from auth section of url
Instantiate a new Client for an Orchestrate application.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/orchestrate/client.rb', line 28 def initialize(api_key, host="https://api.orchestrate.io", &block) @api_key = api_key @host = host @faraday_configuration = block @http = Faraday.new(@host) do |faraday| block = lambda{|f| f.adapter :net_http_persistent } unless block block.call faraday # faraday seems to want you do specify these twice. faraday.request :basic_auth, api_key, '' faraday.basic_auth api_key, '' end end |
Instance Attribute Details
#api_key ⇒ String (readonly)
Returns The API key provided.
11 12 13 |
# File 'lib/orchestrate/client.rb', line 11 def api_key @api_key end |
#faraday_configuration ⇒ Proc (readonly)
Returns The block used to configure faraday.
20 21 22 |
# File 'lib/orchestrate/client.rb', line 20 def faraday_configuration @faraday_configuration end |
#host ⇒ String (readonly)
Returns The Orchestrate data center URL.
14 15 16 |
# File 'lib/orchestrate/client.rb', line 14 def host @host end |
#http ⇒ Faraday::Connection (readonly)
Returns The Faraday HTTP connection.
17 18 19 |
# File 'lib/orchestrate/client.rb', line 17 def http @http end |
Instance Method Details
#delete(collection, key, ref = nil) ⇒ Object
previous versions of the values at this key are still available via #list_refs and #get.
217 218 219 220 221 |
# File 'lib/orchestrate/client.rb', line 217 def delete(collection, key, ref=nil) headers = {} headers['If-Match'] = API::Helpers.format_ref(ref) if ref send_request :delete, [collection, key], { headers: headers } end |
#delete_collection(collection) ⇒ Object
The Orchestrate API will return succesfully regardless of if the collection exists or not.
84 85 86 |
# File 'lib/orchestrate/client.rb', line 84 def delete_collection(collection) send_request :delete, [collection], { query: {force:true} } end |
#delete_relation(collection, key, kind, to_collection, to_key) ⇒ Object
397 398 399 400 |
# File 'lib/orchestrate/client.rb', line 397 def delete_relation(collection, key, kind, to_collection, to_key) path = [collection, key, 'relation', kind, to_collection, to_key] send_request :delete, path, { query: {purge: true} } end |
#get(collection, key, ref = nil) ⇒ Object
Retrieves the value assigned to a key.
If a ref
parameter is provided, retrieves a historical value for the
key
100 101 102 103 104 |
# File 'lib/orchestrate/client.rb', line 100 def get(collection, key, ref=nil) path = [collection, key] path.concat(['refs', ref]) if ref send_request :get, path, { response: API::ItemResponse } end |
#get_event(collection, key, event_type, timestamp, ordinal) ⇒ Object
267 268 269 270 271 |
# File 'lib/orchestrate/client.rb', line 267 def get_event(collection, key, event_type, , ordinal) = API::Helpers.() path = [collection, key, 'events', event_type, , ordinal] send_request :get, path, { response: API::ItemResponse } end |
#get_relations(collection, key, *kinds) ⇒ Object
372 373 374 375 |
# File 'lib/orchestrate/client.rb', line 372 def get_relations(collection, key, *kinds) path = [collection, key, 'relations'].concat(kinds) send_request :get, path, {response: API::CollectionResponse} end |
#in_parallel {|accumulator| ... } ⇒ Object
This method is not Thread-safe. Requests generated from the same
client in different threads while #in_parallel is running will behave
unpredictably. Use #dup
to create per-thread clients.
Performs requests in parallel. Requires using a Faraday adapter that supports parallel requests.
416 417 418 419 420 421 422 |
# File 'lib/orchestrate/client.rb', line 416 def in_parallel(&block) accumulator = {} http.in_parallel do block.call(accumulator) end accumulator end |
#list(collection, options = {}) ⇒ Object
The Orchestrate API may return an error if you include both the :start/:after or :before/:end keys. The client will not stop you from doing this.
To include all keys in a collection, do not include any :start/:after/:before/:end parameters.
List the KeyValue items in a collection. Results are sorted results lexicographically by key name and paginated.
249 250 251 252 |
# File 'lib/orchestrate/client.rb', line 249 def list(collection, ={}) API::Helpers.range_keys!('key', ) send_request :get, [collection], { query: , response: API::CollectionResponse } end |
#list_events(collection, key, event_type, options = {}) ⇒ Object
Lists events associated with a key. Results are time-ordered in reverse-chronological order, and paginated.
349 350 351 352 353 354 355 356 |
# File 'lib/orchestrate/client.rb', line 349 def list_events(collection, key, event_type, ={}) (.keys & [:start, :after, :before, :end]).each do |param| [param] = API::Helpers.([param]) end API::Helpers.range_keys!('event', ) path = [collection, key, 'events', event_type] send_request :get, path, { query: , response: API::CollectionResponse } end |
#list_refs(collection, key, options = {}) ⇒ Object
List historical refs for values of a key. Results are time-ordered newest-to-oldest and paginated.
120 121 122 |
# File 'lib/orchestrate/client.rb', line 120 def list_refs(collection, key, ={}) send_request :get, [collection, key, :refs], { query: , response: API::CollectionResponse } end |
#patch(collection, key, body, condition = nil) ⇒ Object
Manipulate values associated with a key, without retrieving the key object. Array of operations passed as body, will execute operations on the key sequentially. Patch.
181 182 183 184 185 186 187 |
# File 'lib/orchestrate/client.rb', line 181 def patch(collection, key, body, condition=nil) headers = {'Content-Type' => 'application/json-patch+json'} if condition.is_a?(String) headers['If-Match'] = API::Helpers.format_ref(condition) end send_request :patch, [collection, key], { body: body, headers: headers, response: API::ItemResponse } end |
#patch_merge(collection, key, body, condition = nil) ⇒ Object
Merge field/value pairs into existing key, without retrieving the key object. Patch. If a given field's value is nil, will remove field from existing key on merge.
201 202 203 204 205 206 207 |
# File 'lib/orchestrate/client.rb', line 201 def patch_merge(collection, key, body, condition=nil) headers = {'Content-Type' => 'application/merge-patch+json'} if condition.is_a?(String) headers['If-Match'] = API::Helpers.format_ref(condition) end send_request :patch, [collection, key], { body: body, headers: headers, response: API::ItemResponse } end |
#ping ⇒ Object
Tests authentication with Orchestrate.
56 57 58 |
# File 'lib/orchestrate/client.rb', line 56 def ping send_request :head, [] end |
#post(collection, body) ⇒ Object
129 130 131 |
# File 'lib/orchestrate/client.rb', line 129 def post(collection, body) send_request :post, [collection], { body: body, response: API::ItemResponse } end |
#post_event(collection, key, event_type, body, timestamp = nil) ⇒ Object
283 284 285 286 287 |
# File 'lib/orchestrate/client.rb', line 283 def post_event(collection, key, event_type, body, =nil) = API::Helpers.() path = [collection, key, 'events', event_type, ].compact send_request :post, path, { body: body, response: API::ItemResponse } end |
#purge(collection, key, ref = nil) ⇒ Object
229 230 231 232 233 |
# File 'lib/orchestrate/client.rb', line 229 def purge(collection, key, ref=nil) headers = {} headers['If-Match'] = API::Helpers.format_ref(ref) if ref send_request :delete, [collection, key], { query: { purge: true }, headers: headers } end |
#purge_event(collection, key, event_type, timestamp, ordinal, ref = nil) ⇒ Object
328 329 330 331 332 333 334 |
# File 'lib/orchestrate/client.rb', line 328 def purge_event(collection, key, event_type, , ordinal, ref=nil) = API::Helpers.() path = [collection, key, 'events', event_type, , ordinal] headers = {} headers['If-Match'] = API::Helpers.format_ref(ref) if ref send_request :delete, path, { query: { purge: true }, headers: headers } end |
#put(collection, key, body, condition = nil) ⇒ Object Also known as: put_if_unmodified
Updates the value associated with a key. If the key does not currently have a value, will create the value.
151 152 153 154 155 156 157 158 159 |
# File 'lib/orchestrate/client.rb', line 151 def put(collection, key, body, condition=nil) headers={} if condition.is_a?(String) headers['If-Match'] = API::Helpers.format_ref(condition) elsif condition == false headers['If-None-Match'] = '"*"' end send_request :put, [collection, key], { body: body, headers: headers, response: API::ItemResponse } end |
#put_event(collection, key, event_type, timestamp, ordinal, body, ref = nil) ⇒ Object
306 307 308 309 310 311 312 |
# File 'lib/orchestrate/client.rb', line 306 def put_event(collection, key, event_type, , ordinal, body, ref=nil) = API::Helpers.() path = [collection, key, 'events', event_type, , ordinal] headers = {} headers['If-Match'] = API::Helpers.format_ref(ref) if ref send_request :put, path, { body: body, headers: headers, response: API::ItemResponse } end |
#put_if_absent(collection, key, body) ⇒ Object
165 166 167 |
# File 'lib/orchestrate/client.rb', line 165 def put_if_absent(collection, key, body) put collection, key, body, false end |
#put_relation(collection, key, kind, to_collection, to_key) ⇒ Object
Creates a relationship between two Key/Value objects. Relations can span collections.
386 387 388 |
# File 'lib/orchestrate/client.rb', line 386 def put_relation(collection, key, kind, to_collection, to_key) send_request :put, [collection, key, 'relation', kind, to_collection, to_key], { body: {} } end |
#search(collection, query, options = {}) ⇒ Object
Search the items in a collection using a Lucene Query Syntax.
75 76 77 78 |
# File 'lib/orchestrate/client.rb', line 75 def search(collection, query, ={}) send_request :get, [collection], { query: .merge({query: query}), response: API::CollectionResponse } end |
#send_request(method, path, options = {}) ⇒ Object
Performs an HTTP request against the Orchestrate API
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/orchestrate/client.rb', line 434 def send_request(method, path, ={}) path = ['/v0'].concat(path.map{|s| URI.escape(s.to_s).gsub('/','%2F') }).join('/') query_string = .fetch(:query, {}) body = .fetch(:body, '') headers = .fetch(:headers, {}) headers['User-Agent'] = "ruby/orchestrate/#{Orchestrate::VERSION}" headers['Accept'] = 'application/json' if method == :get headers['Connection'] = 'close' if method == :head http_response = http.send(method) do |request| request.url path, query_string if [:put, :post].include?(method) headers['Content-Type'] = 'application/json' request.body = body.to_json elsif [:patch].include?(method) request.body = body.to_json end headers.each {|header, value| request[header] = value } end response_class = .fetch(:response, API::Response) response_class.new(http_response, self) end |