elastic-enterprise-search 0.2.1 → 7.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. checksums.yaml +4 -4
  2. data/.ci/.gitignore +1 -0
  3. data/.ci/Dockerfile +12 -0
  4. data/.ci/certs/README.md +50 -0
  5. data/.ci/certs/ca.crt +20 -0
  6. data/.ci/certs/ca.key +27 -0
  7. data/.ci/certs/testnode.crt +21 -0
  8. data/.ci/certs/testnode.key +27 -0
  9. data/.ci/certs/testnode_no_san.crt +19 -0
  10. data/.ci/certs/testnode_no_san.key +27 -0
  11. data/.ci/functions/cleanup.sh +67 -0
  12. data/.ci/functions/imports.sh +59 -0
  13. data/.ci/functions/wait-for-container.sh +36 -0
  14. data/.ci/jobs/defaults.yml +69 -0
  15. data/.ci/jobs/elastic+enterprise-search-ruby+7.11.yml +12 -0
  16. data/.ci/jobs/elastic+enterprise-search-ruby+master.yml +12 -0
  17. data/.ci/jobs/elastic+enterprise-search-ruby+pull-request.yml +19 -0
  18. data/.ci/run-elasticsearch.sh +127 -0
  19. data/.ci/run-enterprise-search.sh +71 -0
  20. data/.ci/run-local.sh +14 -0
  21. data/.ci/run-repository.sh +47 -0
  22. data/.ci/run-tests +27 -0
  23. data/.ci/test-matrix.yml +14 -0
  24. data/.github/workflows/rubocop.yml +15 -0
  25. data/.github/workflows/testing.yml +40 -0
  26. data/.gitignore +4 -0
  27. data/.rubocop.yml +52 -0
  28. data/CONTRIBUTING.md +43 -0
  29. data/Gemfile +23 -1
  30. data/{LICENSE.txt → LICENSE} +33 -32
  31. data/README.md +29 -93
  32. data/Rakefile +48 -1
  33. data/docs/guide/app-search-api.asciidoc +186 -0
  34. data/docs/guide/connecting.asciidoc +125 -0
  35. data/docs/guide/development.asciidoc +42 -0
  36. data/docs/guide/enterprise-search-api.asciidoc +46 -0
  37. data/docs/guide/index.asciidoc +19 -0
  38. data/docs/guide/installation.asciidoc +18 -0
  39. data/docs/guide/overview.asciidoc +59 -0
  40. data/docs/guide/release_notes/710.asciidoc +4 -0
  41. data/docs/guide/release_notes/711.asciidoc +27 -0
  42. data/docs/guide/release_notes/712.asciidoc +26 -0
  43. data/docs/guide/release_notes/index.asciidoc +14 -0
  44. data/docs/guide/workplace-search-api.asciidoc +121 -0
  45. data/elastic-enterprise-search.gemspec +49 -12
  46. data/lib/elastic-enterprise-search.rb +20 -1
  47. data/lib/elastic/.rubocop.yml +6 -0
  48. data/lib/elastic/app-search/api/add_meta_engine_source.rb +51 -0
  49. data/lib/elastic/app-search/api/api_logs.rb +62 -0
  50. data/lib/elastic/app-search/api/count_analytics.rb +52 -0
  51. data/lib/elastic/app-search/api/create_curation.rb +54 -0
  52. data/lib/elastic/app-search/api/create_engine.rb +53 -0
  53. data/lib/elastic/app-search/api/create_synonym_set.rb +51 -0
  54. data/lib/elastic/app-search/api/curation.rb +53 -0
  55. data/lib/elastic/app-search/api/delete_curation.rb +53 -0
  56. data/lib/elastic/app-search/api/delete_documents.rb +51 -0
  57. data/lib/elastic/app-search/api/delete_engine.rb +50 -0
  58. data/lib/elastic/app-search/api/delete_meta_engine_source.rb +51 -0
  59. data/lib/elastic/app-search/api/delete_synonym_set.rb +53 -0
  60. data/lib/elastic/app-search/api/documents.rb +51 -0
  61. data/lib/elastic/app-search/api/engine.rb +50 -0
  62. data/lib/elastic/app-search/api/index_documents.rb +51 -0
  63. data/lib/elastic/app-search/api/list_curations.rb +52 -0
  64. data/lib/elastic/app-search/api/list_documents.rb +52 -0
  65. data/lib/elastic/app-search/api/list_engines.rb +49 -0
  66. data/lib/elastic/app-search/api/list_synonym_sets.rb +52 -0
  67. data/lib/elastic/app-search/api/log_clickthrough.rb +57 -0
  68. data/lib/elastic/app-search/api/multi_search.rb +51 -0
  69. data/lib/elastic/app-search/api/put_curation.rb +57 -0
  70. data/lib/elastic/app-search/api/put_documents.rb +51 -0
  71. data/lib/elastic/app-search/api/put_schema.rb +51 -0
  72. data/lib/elastic/app-search/api/put_search_settings.rb +51 -0
  73. data/lib/elastic/app-search/api/put_synonym_set.rb +54 -0
  74. data/lib/elastic/app-search/api/query_suggestion.rb +54 -0
  75. data/lib/elastic/app-search/api/reset_search_settings.rb +50 -0
  76. data/lib/elastic/app-search/api/schema.rb +50 -0
  77. data/lib/elastic/app-search/api/search.rb +50 -0
  78. data/lib/elastic/app-search/api/search_settings.rb +50 -0
  79. data/lib/elastic/app-search/api/synonym_set.rb +53 -0
  80. data/lib/elastic/app-search/api/top_clicks_analytics.rb +54 -0
  81. data/lib/elastic/app-search/api/top_queries_analytics.rb +53 -0
  82. data/lib/elastic/app-search/app_search.rb +82 -0
  83. data/lib/elastic/enterprise-search/api/health.rb +43 -0
  84. data/lib/elastic/enterprise-search/api/put_read_only.rb +44 -0
  85. data/lib/elastic/enterprise-search/api/read_only.rb +43 -0
  86. data/lib/elastic/enterprise-search/api/stats.rb +45 -0
  87. data/lib/elastic/enterprise-search/api/version.rb +43 -0
  88. data/lib/elastic/enterprise-search/client.rb +90 -52
  89. data/lib/elastic/enterprise-search/configuration.rb +32 -16
  90. data/lib/elastic/enterprise-search/exceptions.rb +19 -0
  91. data/lib/elastic/enterprise-search/request.rb +59 -81
  92. data/lib/elastic/enterprise-search/utils.rb +28 -1
  93. data/lib/elastic/enterprise-search/version.rb +20 -1
  94. data/lib/elastic/enterprise_search.rb +38 -0
  95. data/lib/elastic/workplace-search/api/add_user_permissions.rb +54 -0
  96. data/lib/elastic/workplace-search/api/create_analytics_event.rb +59 -0
  97. data/lib/elastic/workplace-search/api/create_external_identity.rb +51 -0
  98. data/lib/elastic/workplace-search/api/delete_documents.rb +51 -0
  99. data/lib/elastic/workplace-search/api/delete_external_identity.rb +53 -0
  100. data/lib/elastic/workplace-search/api/external_identity.rb +53 -0
  101. data/lib/elastic/workplace-search/api/index_documents.rb +52 -0
  102. data/lib/elastic/workplace-search/api/list_external_identities.rb +51 -0
  103. data/lib/elastic/workplace-search/api/list_permissions.rb +51 -0
  104. data/lib/elastic/workplace-search/api/put_external_identity.rb +54 -0
  105. data/lib/elastic/workplace-search/api/put_user_permissions.rb +54 -0
  106. data/lib/elastic/workplace-search/api/remove_user_permissions.rb +54 -0
  107. data/lib/elastic/workplace-search/api/search.rb +60 -0
  108. data/lib/elastic/workplace-search/api/user_permissions.rb +53 -0
  109. data/lib/elastic/workplace-search/workplace_search.rb +84 -0
  110. data/spec/app-search/api_count_analytics_spec.rb +34 -0
  111. data/spec/app-search/api_curations_spec.rb +97 -0
  112. data/spec/app-search/api_documents_spec.rb +102 -0
  113. data/spec/app-search/api_engines_spec.rb +67 -0
  114. data/spec/app-search/api_log_clickthrough_spec.rb +34 -0
  115. data/spec/app-search/api_logs_spec.rb +36 -0
  116. data/spec/app-search/api_meta_engines_spec.rb +72 -0
  117. data/spec/app-search/api_query_suggestion_spec.rb +39 -0
  118. data/spec/app-search/api_schema_spec.rb +48 -0
  119. data/spec/app-search/api_search_and_multi_search_spec.rb +48 -0
  120. data/spec/app-search/api_search_settings_spec.rb +76 -0
  121. data/spec/app-search/api_spec_helper.rb +14 -0
  122. data/spec/app-search/api_synonyms_spec.rb +79 -0
  123. data/spec/app-search/api_top_clicks_analytics_spec.rb +44 -0
  124. data/spec/app-search/api_top_queries_analytics_spec.rb +34 -0
  125. data/spec/app-search/client_spec.rb +88 -0
  126. data/spec/app-search/date_spec.rb +69 -0
  127. data/spec/enterprise-search/client_spec.rb +107 -0
  128. data/spec/enterprise-search/request_spec.rb +136 -0
  129. data/spec/enterprise-search/utils_spec.rb +46 -0
  130. data/spec/fixtures/vcr/app_search/add_meta_engine_source.yml +109 -0
  131. data/spec/fixtures/vcr/app_search/api_documents.yml +56 -0
  132. data/spec/fixtures/vcr/app_search/api_index_documents.yml +57 -0
  133. data/spec/fixtures/vcr/app_search/api_log_clickthrough.yml +54 -0
  134. data/spec/fixtures/vcr/app_search/api_logs.yml +70 -0
  135. data/spec/fixtures/vcr/app_search/api_put_schema.yml +109 -0
  136. data/spec/fixtures/vcr/app_search/api_put_search_settings.yml +56 -0
  137. data/spec/fixtures/vcr/app_search/api_query_suggestion.yml +59 -0
  138. data/spec/fixtures/vcr/app_search/api_reset_search_settings.yml +56 -0
  139. data/spec/fixtures/vcr/app_search/api_schema.yml +56 -0
  140. data/spec/fixtures/vcr/app_search/api_search_settings.yml +56 -0
  141. data/spec/fixtures/vcr/app_search/api_top_clicks_analytics.yml +55 -0
  142. data/spec/fixtures/vcr/app_search/api_top_clicks_analytics_query.yml +55 -0
  143. data/spec/fixtures/vcr/app_search/api_top_queries_analytics.yml +55 -0
  144. data/spec/fixtures/vcr/app_search/count_analytics.yml +55 -0
  145. data/spec/fixtures/vcr/app_search/create_and_update_document.yml +107 -0
  146. data/spec/fixtures/vcr/app_search/create_curation.yml +113 -0
  147. data/spec/fixtures/vcr/app_search/create_engine.yml +55 -0
  148. data/spec/fixtures/vcr/app_search/create_meta_engine.yml +56 -0
  149. data/spec/fixtures/vcr/app_search/create_synonym_set.yml +56 -0
  150. data/spec/fixtures/vcr/app_search/delete_curation.yml +56 -0
  151. data/spec/fixtures/vcr/app_search/delete_engine.yml +55 -0
  152. data/spec/fixtures/vcr/app_search/delete_meta_engine_source.yml +56 -0
  153. data/spec/fixtures/vcr/app_search/delete_synonym_set.yml +56 -0
  154. data/spec/fixtures/vcr/app_search/get_curation.yml +56 -0
  155. data/spec/fixtures/vcr/app_search/get_engine.yml +55 -0
  156. data/spec/fixtures/vcr/app_search/index_and_delete_document.yml +107 -0
  157. data/spec/fixtures/vcr/app_search/list_curations.yml +56 -0
  158. data/spec/fixtures/vcr/app_search/list_documents.yml +57 -0
  159. data/spec/fixtures/vcr/app_search/list_engines.yml +55 -0
  160. data/spec/fixtures/vcr/app_search/list_synonym_sets.yml +56 -0
  161. data/spec/fixtures/vcr/app_search/multi_query_search.yml +63 -0
  162. data/spec/fixtures/vcr/app_search/put_curation.yml +113 -0
  163. data/spec/fixtures/vcr/app_search/put_synonym_set.yml +56 -0
  164. data/spec/fixtures/vcr/app_search/search.yml +57 -0
  165. data/spec/fixtures/vcr/app_search/single_query_search.yml +60 -0
  166. data/spec/fixtures/vcr/app_search/synonym_set.yml +56 -0
  167. data/spec/fixtures/vcr/workplace_search/add_user_permissions.yml +53 -0
  168. data/spec/fixtures/vcr/workplace_search/clear_user_permissions.yml +53 -0
  169. data/spec/fixtures/vcr/workplace_search/create_analytics_event.yml +55 -0
  170. data/spec/fixtures/vcr/workplace_search/create_external_identity.yml +53 -0
  171. data/spec/fixtures/vcr/workplace_search/delete_documents.yml +53 -0
  172. data/spec/fixtures/vcr/workplace_search/delete_external_identity.yml +53 -0
  173. data/spec/fixtures/vcr/workplace_search/index_documents.yml +55 -0
  174. data/spec/fixtures/vcr/workplace_search/list_external_identities.yml +53 -0
  175. data/spec/fixtures/vcr/workplace_search/list_permissions.yml +53 -0
  176. data/spec/fixtures/vcr/workplace_search/oauth_request_token.yml +57 -0
  177. data/spec/fixtures/vcr/workplace_search/put_external_identity.yml +53 -0
  178. data/spec/fixtures/vcr/workplace_search/put_user_permissions.yml +103 -0
  179. data/spec/fixtures/vcr/workplace_search/remove_user_permissions.yml +53 -0
  180. data/spec/fixtures/vcr/workplace_search/retrieve_external_identity.yml +53 -0
  181. data/spec/fixtures/vcr/workplace_search/search_request.yml +60 -0
  182. data/spec/fixtures/vcr/workplace_search/user_permissions_empty.yml +53 -0
  183. data/spec/integration/enterprise_search_api_spec.rb +96 -0
  184. data/spec/spec_helper.rb +27 -14
  185. data/spec/webmock_requires.rb +4 -0
  186. data/spec/workplace-search/client_spec.rb +77 -0
  187. data/spec/workplace-search/create_analytics_event_spec.rb +59 -0
  188. data/spec/workplace-search/documents_spec.rb +80 -0
  189. data/spec/workplace-search/external_identities_spec.rb +84 -0
  190. data/spec/workplace-search/permissions_spec.rb +136 -0
  191. data/spec/workplace-search/search_spec.rb +53 -0
  192. metadata +272 -28
  193. data/.circleci/config.yml +0 -68
  194. data/.travis.yml +0 -15
  195. data/NOTICE.txt +0 -3
  196. data/lib/elastic/enterprise-search.rb +0 -7
  197. data/logo-enterprise-search.png +0 -0
  198. data/spec/client_spec.rb +0 -57
  199. data/spec/configuration_spec.rb +0 -19
  200. data/spec/fixtures/vcr/async_create_or_update_document_success.yml +0 -51
  201. data/spec/fixtures/vcr/destroy_documents_success.yml +0 -51
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Licensed to Elasticsearch B.V. under one or more contributor
4
+ # license agreements. See the NOTICE file distributed with
5
+ # this work for additional information regarding copyright
6
+ # ownership. Elasticsearch B.V. licenses this file to you under
7
+ # the Apache License, Version 2.0 (the "License"); you may
8
+ # not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+
20
+ module Elastic
21
+ module EnterpriseSearch
22
+ module Actions
23
+ # Version - Get version information for this server
24
+ # Get version information for this server
25
+ #
26
+ # @option arguments [Hash] :headers optional HTTP headers to send with the request
27
+ #
28
+ # @see https://www.elastic.co/guide/en/enterprise-search/current/management-apis.html
29
+ #
30
+ def version(arguments = {})
31
+ headers = arguments.delete(:headers) || {}
32
+
33
+ request(
34
+ :get,
35
+ 'api/ent/v1/internal/version/',
36
+ arguments,
37
+ {},
38
+ headers
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,34 +1,93 @@
1
- require 'set'
2
- require 'elastic/enterprise-search/configuration'
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ # frozen_string_literal: true
19
+
3
20
  require 'elastic/enterprise-search/request'
4
21
  require 'elastic/enterprise-search/utils'
22
+ require 'elasticsearch-transport'
5
23
 
6
24
  module Elastic
25
+ # If the version is X.X.X.pre/alpha/beta, use X.X.Xp for the meta-header:
26
+ def self.client_meta_version
27
+ regexp = /^([0-9]+\.[0-9]+\.[0-9]+)\.?([a-z0-9.-]+)?$/
28
+ match = Elastic::EnterpriseSearch::VERSION.match(regexp)
29
+ return "#{match[1]}p" if match[2]
30
+
31
+ Elastic::EnterpriseSearch::VERSION
32
+ end
33
+
34
+ META_HEADER_SERVICE_VERSION = [:ent, client_meta_version].freeze
35
+
7
36
  module EnterpriseSearch
8
- # API client for the {Elastic Enterprise Search API}[https://swiftype.com/enterprise-search].
37
+ # API client for the {Elastic Enterprise Search API}[https://www.elastic.co/enterprise-search].
38
+ # This is the main client from which the Workplace Search and App Search clients inherit.
9
39
  class Client
10
40
  DEFAULT_TIMEOUT = 15
11
41
 
12
42
  include Elastic::EnterpriseSearch::Request
43
+ include Elastic::EnterpriseSearch::Actions
44
+ include Elastic::EnterpriseSearch::Utils
45
+
46
+ def workplace_search(options = {})
47
+ @workplace_search ||= Elastic::EnterpriseSearch::WorkplaceSearch::Client.new(
48
+ host: host,
49
+ http_auth: options[:http_auth],
50
+ transport: @transport
51
+ )
52
+ end
13
53
 
14
- def self.configure(&block)
15
- Elastic::EnterpriseSearch.configure &block
54
+ def app_search(options = {})
55
+ @app_search ||= Elastic::EnterpriseSearch::AppSearch::Client.new(
56
+ host: host,
57
+ http_auth: options[:http_auth],
58
+ transport: @transport
59
+ )
16
60
  end
17
61
 
18
62
  # Create a new Elastic::EnterpriseSearch::Client client
19
63
  #
20
64
  # @param options [Hash] a hash of configuration options that will override what is set on the Elastic::EnterpriseSearch class.
21
- # @option options [String] :access_token an Access Token to use for this client
65
+ # @option options [String] :host Enterprise Search host
66
+ # @option options [Hash] :basic_auth a username and password for Basic Authentication
22
67
  # @option options [Numeric] :overall_timeout overall timeout for requests in seconds (default: 15s)
23
- # @option options [Numeric] :open_timeout the number of seconds Net::HTTP (default: 15s)
24
- # will wait while opening a connection before raising a Timeout::Error
68
+ # @option options [Numeric] :open_timeout the number of seconds Net::HTTP (default: 15s) will wait while opening a connection before raising a Timeout::Error
25
69
  # @option options [String] :proxy url of proxy to use, ex: "http://localhost:8888"
70
+ # @option options [Boolean] :log Use the default logger (disabled by default)
71
+ # @option arguments [Object] :logger An instance of a Logger-compatible object
72
+ # @option enable_meta_header [Boolean] :enable_meta_header Enable sending the meta data header to Cloud.
73
+ # (Default: true)
26
74
  def initialize(options = {})
27
75
  @options = options
76
+ @transport = transport
28
77
  end
29
78
 
30
- def access_token
31
- @options[:access_token] || Elastic::EnterpriseSearch.access_token
79
+ def transport
80
+ @options[:transport] ||
81
+ Elasticsearch::Client.new(
82
+ host: host,
83
+ log: log,
84
+ logger: logger,
85
+ request_timeout: overall_timeout,
86
+ transport_options: {
87
+ request: { open_timeout: open_timeout }
88
+ },
89
+ enable_meta_header: @options[:enable_meta_header] || true
90
+ )
32
91
  end
33
92
 
34
93
  def open_timeout
@@ -43,50 +102,29 @@ module Elastic
43
102
  (@options[:overall_timeout] || DEFAULT_TIMEOUT).to_f
44
103
  end
45
104
 
46
- # Documents have fields that can be searched or filtered.
47
- #
48
- # For more information on indexing documents, see the {Content Source documentation}[https://swiftype.com/documentation/enterprise-search/guides/content-sources].
49
- module ContentSourceDocuments
50
-
51
- # Index a batch of documents using the {Content Source API}[https://swiftype.com/documentation/enterprise-search/api/custom-sources].
52
- #
53
- # @param [String] content_source_key the unique Content Source key as found in your Content Sources dashboard
54
- # @param [Array] documents an Array of Document Hashes
55
- #
56
- # @return [Array<Hash>] an Array of Document indexing Results
57
- #
58
- # @raise [Elastic::EnterpriseSearch::InvalidDocument] when a single document is missing required fields or contains unsupported fields
59
- # @raise [Timeout::Error] when timeout expires waiting for results
60
- def index_documents(content_source_key, documents)
61
- documents = Array(documents).map! { |document| normalize_document(document) }
62
-
63
- async_create_or_update_documents(content_source_key, documents)
64
- end
65
-
66
- # Destroy a batch of documents given a list of external IDs
67
- #
68
- # @param [Array<String>] document_ids an Array of Document External IDs
69
- #
70
- # @return [Array<Hash>] an Array of Document destroy result hashes
71
- #
72
- # @raise [Timeout::Error] when timeout expires waiting for results
73
- def destroy_documents(content_source_key, document_ids)
74
- document_ids = Array(document_ids)
75
- post("ent/sources/#{content_source_key}/documents/bulk_destroy.json", document_ids)
76
- end
77
-
78
- private
79
-
80
- def async_create_or_update_documents(content_source_key, documents)
81
- post("ent/sources/#{content_source_key}/documents/bulk_create.json", documents)
82
- end
83
-
84
- def normalize_document(document)
85
- Utils.stringify_keys(document)
86
- end
105
+ def http_auth
106
+ @options[:http_auth] || { user: 'elastic', password: 'changeme' }
107
+ end
108
+
109
+ def http_auth=(http_auth)
110
+ @options[:http_auth] = http_auth
111
+ end
112
+
113
+ def log
114
+ @options[:log] || false
115
+ end
116
+
117
+ def logger
118
+ @options[:logger]
87
119
  end
88
120
 
89
- include Elastic::EnterpriseSearch::Client::ContentSourceDocuments
121
+ def host
122
+ return DEFAULT_HOST unless @options[:host]
123
+
124
+ raise URI::InvalidURIError unless @options[:host] =~ /\A#{URI::DEFAULT_PARSER.make_regexp}\z/
125
+
126
+ @options[:host]
127
+ end
90
128
  end
91
129
  end
92
130
  end
@@ -1,18 +1,35 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ # frozen_string_literal: true
19
+
1
20
  require 'uri'
2
- require 'elastic/enterprise-search/version'
21
+ require 'elastic/workplace-search/version'
3
22
 
4
23
  module Elastic
5
24
  module EnterpriseSearch
25
+ # Configuratin module
6
26
  module Configuration
7
- DEFAULT_ENDPOINT = "http://localhost:3002/api/v1/"
27
+ # TODO: Endpoint for EE
28
+ # DEFAULT_ENDPOINT = 'http://localhost:3002/api/ws/v1/'
8
29
 
9
- VALID_OPTIONS_KEYS = [
10
- :access_token,
11
- :user_agent,
12
- :endpoint
13
- ].freeze
30
+ VALID_OPTIONS_KEYS = [:access_token, :endpoint].freeze
14
31
 
15
- attr_accessor *VALID_OPTIONS_KEYS
32
+ attr_accessor(*VALID_OPTIONS_KEYS)
16
33
 
17
34
  def self.extended(base)
18
35
  base.reset
@@ -22,11 +39,10 @@ module Elastic
22
39
  def reset
23
40
  self.access_token = nil
24
41
  self.endpoint = DEFAULT_ENDPOINT
25
- self.user_agent = nil
26
42
  self
27
43
  end
28
44
 
29
- # Yields the Elastic::EnterpriseSearch::Configuration module which can be used to set configuration options.
45
+ # Yields the Elastic::WorkplaceSearch::Configuration module which can be used to set configuration options.
30
46
  #
31
47
  # @return self
32
48
  def configure
@@ -37,17 +53,17 @@ module Elastic
37
53
  # Return a hash of the configured options.
38
54
  def options
39
55
  options = {}
40
- VALID_OPTIONS_KEYS.each{ |k| options[k] = send(k) }
56
+ VALID_OPTIONS_KEYS.each { |k| options[k] = send(k) }
41
57
  options
42
58
  end
43
59
 
44
60
  # setter for endpoint that ensures it always ends in '/'
45
61
  def endpoint=(endpoint)
46
- if endpoint.end_with?('/')
47
- @endpoint = endpoint
48
- else
49
- @endpoint = "#{endpoint}/"
50
- end
62
+ @endpoint = if endpoint.end_with?('/')
63
+ endpoint
64
+ else
65
+ "#{endpoint}/"
66
+ end
51
67
  end
52
68
  end
53
69
  end
@@ -1,3 +1,22 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ # frozen_string_literal: true
19
+
1
20
  module Elastic
2
21
  module EnterpriseSearch
3
22
  class ClientException < StandardError; end
@@ -1,112 +1,90 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ # frozen_string_literal: true
19
+
1
20
  require 'net/https'
2
21
  require 'json'
3
22
  require 'elastic/enterprise-search/exceptions'
4
- require 'openssl'
23
+ require 'base64'
5
24
 
6
25
  module Elastic
7
26
  module EnterpriseSearch
8
- CLIENT_NAME = 'elastic-enterprise-search-ruby'
27
+ CLIENT_NAME = 'elastic-enteprise-search-ruby'
9
28
  CLIENT_VERSION = Elastic::EnterpriseSearch::VERSION
10
29
 
30
+ # Module included in Elastic::Enterprise::Client for http requests.
11
31
  module Request
12
- def get(path, params={})
13
- request(:get, path, params)
32
+ def get(path, params = {}, headers = {})
33
+ request(:get, path, params, headers)
14
34
  end
15
35
 
16
- def post(path, params={})
17
- request(:post, path, params)
36
+ def post(path, params = {}, body = {}, headers = {})
37
+ request(:post, path, params, body, headers)
18
38
  end
19
39
 
20
- def put(path, params={})
21
- request(:put, path, params)
40
+ def put(path, params = {}, body = {}, headers = {})
41
+ request(:put, path, params, body, headers)
22
42
  end
23
43
 
24
- def delete(path, params={})
25
- request(:delete, path, params)
44
+ def delete(path, params = {}, headers = {})
45
+ request(:delete, path, params, headers)
26
46
  end
27
47
 
28
48
  # Construct and send a request to the API.
29
49
  #
30
50
  # @raise [Timeout::Error] when the timeout expires
31
- def request(method, path, params = {})
32
- Timeout.timeout(overall_timeout) do
33
- uri = URI.parse("#{Elastic::EnterpriseSearch.endpoint}#{path}")
34
-
35
- request = build_request(method, uri, params)
36
-
37
- if proxy
38
- proxy_parts = URI.parse(proxy)
39
- http = Net::HTTP.new(uri.host, uri.port, proxy_parts.host, proxy_parts.port, proxy_parts.user, proxy_parts.password)
40
- else
41
- http = Net::HTTP.new(uri.host, uri.port)
42
- end
43
-
44
- http.open_timeout = open_timeout
45
- http.read_timeout = overall_timeout
46
-
47
- if uri.scheme == 'https'
48
- http.use_ssl = true
49
- # st_ssl_verify_none provides a means to disable SSL verification for debugging purposes. An example
50
- # is Charles, which uses a self-signed certificate in order to inspect https traffic. This will
51
- # not be part of this client's public API, this is more of a development enablement option
52
- http.verify_mode = ENV['st_ssl_verify_none'] == 'true' ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
53
- http.ca_file = File.realpath(File.join(File.dirname(__FILE__), '..', '..', 'data', 'ca-bundle.crt'))
54
- http.ssl_timeout = open_timeout
55
- end
51
+ def request(method, path, params = {}, body = {}, headers = {})
52
+ meta_headers = { authorization: decide_authorization(params), user_agent: request_user_agent }
53
+ headers = if headers.nil? || !headers.is_a?(Hash)
54
+ meta_headers
55
+ else
56
+ headers.merge(meta_headers)
57
+ end
58
+
59
+ @transport.perform_request(method.to_s.upcase, path, params, body, headers)
60
+ end
56
61
 
57
- response = http.request(request)
58
- handle_errors(response)
59
- JSON.parse(response.body) if response.body && response.body.strip != ''
60
- end
62
+ def setup_authentication_header
63
+ credentials = Base64.strict_encode64("#{http_auth[:user]}:#{http_auth[:password]}")
64
+ "Basic #{credentials}"
61
65
  end
62
66
 
63
67
  private
64
- def handle_errors(response)
65
- case response
66
- when Net::HTTPSuccess
67
- response
68
- when Net::HTTPUnauthorized
69
- raise Elastic::EnterpriseSearch::InvalidCredentials
70
- when Net::HTTPNotFound
71
- raise Elastic::EnterpriseSearch::NonExistentRecord
72
- when Net::HTTPBadRequest
73
- raise Elastic::EnterpriseSearch::BadRequest
74
- when Net::HTTPForbidden
75
- raise Elastic::EnterpriseSearch::Forbidden
76
- else
77
- raise Elastic::EnterpriseSearch::UnexpectedHTTPException, "#{response.code} #{response.body}"
68
+
69
+ def request_user_agent
70
+ ua = "#{CLIENT_NAME}/#{CLIENT_VERSION}"
71
+ meta = ["RUBY_VERSION: #{RUBY_VERSION}"]
72
+ if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
73
+ meta << "#{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} " \
74
+ "#{RbConfig::CONFIG['target_cpu']}"
78
75
  end
76
+ meta << "elasticsearch-transport: #{Elasticsearch::Transport::VERSION}"
77
+ "#{ua} (#{meta.join('; ')})"
79
78
  end
80
79
 
81
- def build_request(method, uri, params)
82
- klass = case method
83
- when :get
84
- Net::HTTP::Get
85
- when :post
86
- Net::HTTP::Post
87
- when :put
88
- Net::HTTP::Put
89
- when :delete
90
- Net::HTTP::Delete
91
- end
92
-
93
- case method
94
- when :get, :delete
95
- uri.query = URI.encode_www_form(params) if params && !params.empty?
96
- req = klass.new(uri.request_uri)
97
- when :post, :put
98
- req = klass.new(uri.request_uri)
99
- req.body = JSON.generate(params) unless params.length == 0
80
+ def decide_authorization(params)
81
+ if params[:grant_type] == 'authorization_code'
82
+ "Bearer #{params[:code]}"
83
+ elsif params[:access_token]
84
+ "Bearer #{params.delete(:access_token)}"
85
+ else
86
+ setup_authentication_header
100
87
  end
101
-
102
- req['User-Agent'] = Elastic::EnterpriseSearch.user_agent if Elastic::EnterpriseSearch.user_agent
103
- req['Content-Type'] = 'application/json'
104
- req['X-Swiftype-Client'] = CLIENT_NAME
105
- req['X-Swiftype-Client-Version'] = CLIENT_VERSION
106
- req['Authorization'] = "Bearer #{access_token}"
107
- puts req
108
-
109
- req
110
88
  end
111
89
  end
112
90
  end