forked from opensearch-project/opensearch-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Low-level APIs (opensearch-project#211)
- Generator for low-level methods - Generated low-level methods - Guide for low-level methods - Samples for low-level methods Signed-off-by: Theo Truong <[email protected]>
- Loading branch information
Showing
33 changed files
with
834 additions
and
10 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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# The OpenSearch Contributors require contributions made to | ||
# this file be licensed under the Apache-2.0 license or a | ||
# compatible open source license. | ||
|
||
# frozen_string_literal: true | ||
|
||
require_relative 'low_level_base_action_generator' | ||
|
||
# Generate low-level API actions via Mustache | ||
class LowLevelActionGenerator < LowLevelBaseActionGenerator | ||
self.template_file = './templates/low_level_action.mustache' | ||
|
||
def initialize(output_folder, namespace, action) | ||
super(output_folder, namespace) | ||
@action = action | ||
end | ||
|
||
def lower_cased | ||
@action.downcase | ||
end | ||
|
||
def upper_cased | ||
@action.upcase | ||
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,30 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# The OpenSearch Contributors require contributions made to | ||
# this file be licensed under the Apache-2.0 license or a | ||
# compatible open source license. | ||
|
||
# frozen_string_literal: true | ||
|
||
require_relative 'base_generator' | ||
|
||
# Generate low-level API actions via Mustache | ||
class LowLevelBaseActionGenerator < BaseGenerator | ||
self.template_file = './templates/low_level_base_action.mustache' | ||
|
||
def initialize(output_folder, namespace) | ||
super(output_folder) | ||
@namespace = namespace | ||
@action = 'request' | ||
end | ||
|
||
def namespace_module | ||
@namespace.camelize | ||
end | ||
|
||
private | ||
|
||
def output_file | ||
create_folder(*[@output_folder, @namespace].compact).join("#{@action}.rb") | ||
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,22 @@ | ||
{{{license_header}}} | ||
{{{generated_code_warning}}} | ||
|
||
# frozen_string_literal: true | ||
|
||
module OpenSearch | ||
module API | ||
module {{namespace_module}} | ||
module Actions | ||
# Make a customized {{upper_cased}} request. | ||
# | ||
# @option arguments [String] :url Relative path to the endpoint (e.g. 'cat/indices/books,movies') (*Required*) | ||
# @option arguments [Hash] :params Querystring parameters to be appended to the path | ||
# @option arguments [Hash] :headers Custom HTTP headers | ||
# @option arguments [String | Hash | Array<Hash>] :body The body of the request | ||
def {{lower_cased}}(url, headers: {}, body: nil, params: {}) | ||
request('{{upper_cased}}', url, headers: headers, body: body, params: params) | ||
end | ||
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,21 @@ | ||
{{{license_header}}} | ||
{{{generated_code_warning}}} | ||
|
||
# frozen_string_literal: true | ||
|
||
module OpenSearch | ||
module API | ||
module {{namespace_module}} | ||
module Actions | ||
private | ||
|
||
def request(method, url, headers: {}, body: nil, params: {}) | ||
body = OpenSearch::API::Utils.__bulkify(body) if body.is_a?(Array) | ||
headers.merge!('Content-Type' => 'application/x-ndjson') if body.is_a?(Array) | ||
|
||
perform_request(method, url, params, body, headers).body | ||
end | ||
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,62 @@ | ||
# Making raw HTTP requests | ||
|
||
The OpenSearch client implements many high-level REST DSLs that invoke OpenSearch APIs. However, you may find yourself in a situation that requires you to invoke an API that is not supported by the client. In this case, you can use raw HTTP requests to invoke any OpenSearch API. This guide shows you different ways to make custom API calls using the OpenSearch Ruby client. | ||
|
||
## Setup | ||
First, create a client instance with the following code to connect to a local OpenSearch cluster using default security credentials: | ||
|
||
```ruby | ||
require 'opensearch-ruby' | ||
client = OpenSearch::Client.new( | ||
hosts: ['https://localhost:9200'], | ||
user: 'admin', | ||
password: 'admin', | ||
transport_options: { ssl: { verify: false } } | ||
) | ||
``` | ||
|
||
## The http Namespace | ||
|
||
The `http` namespace provides a method of every HTTP verb (GET, POST, HEAD...). Normally, to get a summary of all indices in the cluster, you would use `client.cat.indices`. However, you can achieve the same result using `client.http.get`: | ||
|
||
```ruby | ||
puts client.http.get('_cat/indices') | ||
``` | ||
|
||
Of course, you can also pass query-string parameters, headers, and a request body to the `http` methods. For example, the following block of code creates an index named `movies` with 5 shards and 2 replicas, with explicit timeout of 30 seconds, and content-type of `application/json`: | ||
|
||
```ruby | ||
body = { settings: { number_of_shards: 5, number_of_replicas: 2 } } | ||
params = { timeout: '30s' } | ||
headers = { 'Content-Type' => 'application/json' } | ||
|
||
client.http.put('movies', body: body, params: params, headers: headers) | ||
``` | ||
|
||
If you provide the http method with a string as body, it will be sent to the server as is. However, you can also provide them with a Ruby hash or an array of hashes, and the client will convert them to a JSON string or a newline-delimited JSON (NDJSON) string respectively. For example, to create two documents in the `books` index using the `bulk` endpoint: | ||
|
||
```ruby | ||
body = [{ index: { _index: "books", _id: 1 } }, | ||
{ title: "The Lion King", year: 1994 }, | ||
{ index: { _index: "books", _id: 2 } }, | ||
{ title: "Beauty and the Beast", year: 1991 }] | ||
|
||
client.http.post('_bulk', body: body) | ||
``` | ||
|
||
Let's clean up and delete the `movies` index: | ||
|
||
```ruby | ||
client.http.delete('movies') | ||
``` | ||
|
||
The `http` namespace includes the following methods: | ||
- get | ||
- put | ||
- post | ||
- delete | ||
- head | ||
- options | ||
- patch | ||
- trace | ||
- connect |
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,28 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# The OpenSearch Contributors require contributions made to | ||
# this file be licensed under the Apache-2.0 license or a | ||
# compatible open source license. | ||
|
||
# This code was generated from OpenSearch API Spec. | ||
# Update the code generation logic instead of modifying this file directly. | ||
|
||
# frozen_string_literal: true | ||
|
||
module OpenSearch | ||
module API | ||
module Http | ||
module Actions | ||
# Make a customized CONNECT request. | ||
# | ||
# @option arguments [String] :url Relative path to the endpoint (e.g. 'cat/indices/books,movies') (*Required*) | ||
# @option arguments [Hash] :params Querystring parameters to be appended to the path | ||
# @option arguments [Hash] :headers Custom HTTP headers | ||
# @option arguments [String | Hash | Array<Hash>] :body The body of the request | ||
def connect(url, headers: {}, body: nil, params: {}) | ||
request('CONNECT', url, headers: headers, body: body, params: params) | ||
end | ||
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,28 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# The OpenSearch Contributors require contributions made to | ||
# this file be licensed under the Apache-2.0 license or a | ||
# compatible open source license. | ||
|
||
# This code was generated from OpenSearch API Spec. | ||
# Update the code generation logic instead of modifying this file directly. | ||
|
||
# frozen_string_literal: true | ||
|
||
module OpenSearch | ||
module API | ||
module Http | ||
module Actions | ||
# Make a customized DELETE request. | ||
# | ||
# @option arguments [String] :url Relative path to the endpoint (e.g. 'cat/indices/books,movies') (*Required*) | ||
# @option arguments [Hash] :params Querystring parameters to be appended to the path | ||
# @option arguments [Hash] :headers Custom HTTP headers | ||
# @option arguments [String | Hash | Array<Hash>] :body The body of the request | ||
def delete(url, headers: {}, body: nil, params: {}) | ||
request('DELETE', url, headers: headers, body: body, params: params) | ||
end | ||
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,28 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# The OpenSearch Contributors require contributions made to | ||
# this file be licensed under the Apache-2.0 license or a | ||
# compatible open source license. | ||
|
||
# This code was generated from OpenSearch API Spec. | ||
# Update the code generation logic instead of modifying this file directly. | ||
|
||
# frozen_string_literal: true | ||
|
||
module OpenSearch | ||
module API | ||
module Http | ||
module Actions | ||
# Make a customized GET request. | ||
# | ||
# @option arguments [String] :url Relative path to the endpoint (e.g. 'cat/indices/books,movies') (*Required*) | ||
# @option arguments [Hash] :params Querystring parameters to be appended to the path | ||
# @option arguments [Hash] :headers Custom HTTP headers | ||
# @option arguments [String | Hash | Array<Hash>] :body The body of the request | ||
def get(url, headers: {}, body: nil, params: {}) | ||
request('GET', url, headers: headers, body: body, params: params) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.