eson-http 0.7.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 (62) hide show
  1. data/LICENSE.md +20 -0
  2. data/README.md +111 -0
  3. data/Rakefile +31 -0
  4. data/bin/elshell +20 -0
  5. data/eson-http.gemspec +28 -0
  6. data/lib/eson-http.rb +55 -0
  7. data/lib/eson/http.rb +16 -0
  8. data/lib/eson/http/api.rb +13 -0
  9. data/lib/eson/http/client.rb +24 -0
  10. data/lib/eson/http/cluster/health.rb +15 -0
  11. data/lib/eson/http/cluster/nodes.rb +14 -0
  12. data/lib/eson/http/cluster/shutdown.rb +15 -0
  13. data/lib/eson/http/cluster/state.rb +16 -0
  14. data/lib/eson/http/cluster/stats.rb +18 -0
  15. data/lib/eson/http/core/bulk.rb +33 -0
  16. data/lib/eson/http/core/count.rb +26 -0
  17. data/lib/eson/http/core/delete.rb +14 -0
  18. data/lib/eson/http/core/delete_by_query.rb +22 -0
  19. data/lib/eson/http/core/get.rb +15 -0
  20. data/lib/eson/http/core/index.rb +26 -0
  21. data/lib/eson/http/core/mget.rb +23 -0
  22. data/lib/eson/http/core/more_like_this.rb +14 -0
  23. data/lib/eson/http/core/msearch.rb +47 -0
  24. data/lib/eson/http/core/percolate.rb +16 -0
  25. data/lib/eson/http/core/search.rb +29 -0
  26. data/lib/eson/http/core/simple_search.rb +18 -0
  27. data/lib/eson/http/indices/aliases.rb +14 -0
  28. data/lib/eson/http/indices/analyze.rb +14 -0
  29. data/lib/eson/http/indices/clear_cache.rb +18 -0
  30. data/lib/eson/http/indices/close_index.rb +14 -0
  31. data/lib/eson/http/indices/create_index.rb +14 -0
  32. data/lib/eson/http/indices/delete_index.rb +14 -0
  33. data/lib/eson/http/indices/delete_mapping.rb +17 -0
  34. data/lib/eson/http/indices/delete_template.rb +14 -0
  35. data/lib/eson/http/indices/exists.rb +14 -0
  36. data/lib/eson/http/indices/flush.rb +18 -0
  37. data/lib/eson/http/indices/get_mapping.rb +20 -0
  38. data/lib/eson/http/indices/get_settings.rb +18 -0
  39. data/lib/eson/http/indices/get_template.rb +14 -0
  40. data/lib/eson/http/indices/open_index.rb +14 -0
  41. data/lib/eson/http/indices/optimize.rb +18 -0
  42. data/lib/eson/http/indices/put_mapping.rb +16 -0
  43. data/lib/eson/http/indices/put_template.rb +14 -0
  44. data/lib/eson/http/indices/refresh.rb +18 -0
  45. data/lib/eson/http/indices/segments.rb +18 -0
  46. data/lib/eson/http/indices/snapshot.rb +18 -0
  47. data/lib/eson/http/indices/stats.rb +18 -0
  48. data/lib/eson/http/indices/status.rb +18 -0
  49. data/lib/eson/http/indices/update_settings.rb +18 -0
  50. data/lib/eson/http/request.rb +95 -0
  51. data/lib/eson/modules/response_parser.rb +22 -0
  52. data/lib/eson/modules/status_handler.rb +24 -0
  53. data/log4j.properties +18 -0
  54. data/test/http/client_test.rb +14 -0
  55. data/test/http/cluster_test.rb +58 -0
  56. data/test/http/index_test.rb +180 -0
  57. data/test/http/indices/basics_test.rb +257 -0
  58. data/test/http/query_test.rb +70 -0
  59. data/test/modules/query_plugin_test.rb +40 -0
  60. data/test/seeds/seeds.rb +30 -0
  61. data/test/test_config.rb +33 -0
  62. metadata +202 -0
@@ -0,0 +1,18 @@
1
+ module Eson
2
+ module HTTP
3
+ module GetSettings
4
+ include Shared::GetSettings
5
+ extend API
6
+
7
+ request_method :get
8
+
9
+ def path
10
+ if index
11
+ "/{-list|,|indices}/_settings"
12
+ else
13
+ "/_settings"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ module Eson
2
+ module HTTP
3
+ module GetTemplate
4
+ include Shared::GetTemplate
5
+ extend API
6
+
7
+ request_method :get
8
+
9
+ def path
10
+ "/_template/{name}"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Eson
2
+ module HTTP
3
+ module OpenIndex
4
+ include Shared::OpenIndex
5
+ extend API
6
+
7
+ request_method :post
8
+
9
+ def path
10
+ "/{-list|,|indices}/_open"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module Eson
2
+ module HTTP
3
+ module Optimize
4
+ include Shared::Optimize
5
+ extend API
6
+
7
+ request_method :post
8
+
9
+ def path
10
+ if index
11
+ "/{-list|,|indices}/_refresh"
12
+ else
13
+ "/_refresh"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module Eson
2
+ module HTTP
3
+ module PutMapping
4
+ include Shared::PutMapping
5
+ extend API
6
+
7
+ request_method :put
8
+
9
+ def path
10
+ if type && !indices.empty?
11
+ "/{-list|,|indices}/{type}/_mapping"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module Eson
2
+ module HTTP
3
+ module PutTemplate
4
+ include Shared::PutTemplate
5
+ extend API
6
+
7
+ request_method :put
8
+
9
+ def path
10
+ "/_template/{name}"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module Eson
2
+ module HTTP
3
+ module Refresh
4
+ include Shared::Refresh
5
+ extend API
6
+
7
+ request_method :post
8
+
9
+ def path
10
+ if index
11
+ "/{-list|,|indices}/_refresh"
12
+ else
13
+ "/_refresh"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Eson
2
+ module HTTP
3
+ module Segments
4
+ include Shared::Segments
5
+ extend API
6
+
7
+ request_method :get
8
+
9
+ def path
10
+ unless indices.empty?
11
+ "/{-list|,|indices}/_segments"
12
+ else
13
+ "/_segments"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Eson
2
+ module HTTP
3
+ module Snapshot
4
+ include Shared::Snapshot
5
+ extend API
6
+
7
+ request_method :post
8
+
9
+ def path
10
+ unless indices.empty?
11
+ "/{-list|,|indices}/_gateway/snapshot"
12
+ else
13
+ "/_gateway/snapshot"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Eson
2
+ module HTTP
3
+ module IndexStats
4
+ include Shared::IndexStats
5
+ extend API
6
+
7
+ request_method :get
8
+
9
+ def path
10
+ unless indices.empty?
11
+ "/{-list|,|indices}/_stats"
12
+ else
13
+ "/_stats"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Eson
2
+ module HTTP
3
+ module Status
4
+ include Shared::Status
5
+ extend API
6
+
7
+ request_method :get
8
+
9
+ def path
10
+ if !indices.empty?
11
+ "/{-list|,|indices}/_status"
12
+ else
13
+ "/_status"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Eson
2
+ module HTTP
3
+ module UpdateSettings
4
+ include Shared::UpdateSettings
5
+ extend API
6
+
7
+ request_method :put
8
+
9
+ def path
10
+ unless indices.empty?
11
+ "/{-list|,|indices}/_settings"
12
+ else
13
+ "/_settings"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,95 @@
1
+ require 'faraday'
2
+
3
+ module Eson
4
+ module HTTP
5
+ class Request < Eson::Request
6
+ # This middleware circumvents faradays detection of
7
+ # :get as special and emits a generic request - which
8
+ # can have a body...
9
+ # This is mostly due to ElasticSearchs reliance of
10
+ # Bodys on get and delete.
11
+ class GetHackMiddleware < Faraday::Middleware
12
+ def call(env)
13
+ env[:method] = "get" if env[:method] == :get
14
+
15
+ @app.call env
16
+ end
17
+ end
18
+
19
+ attr_accessor :request_method
20
+
21
+ def base_resource
22
+ conn = Faraday.new(:url => client.node) do |builder|
23
+ builder.use Faraday::Response::Logger, Eson::HTTP.logger
24
+ builder.use GetHackMiddleware
25
+
26
+ builder.adapter :net_http
27
+ end
28
+
29
+ conn.basic_auth(*client.auth) if client.auth?
30
+ conn
31
+ end
32
+
33
+ def call
34
+ resource = base_resource
35
+
36
+ response = (
37
+ case request_method
38
+ when :get
39
+ resource.get fill do |req|
40
+ (req.body = source) if source
41
+ end
42
+ when :head
43
+ resource.head fill
44
+ when :delete
45
+ resource.delete(fill) do |req|
46
+ req.body = source if source
47
+ end
48
+ when :post
49
+ resource.post(fill, source)
50
+ when :put
51
+ resource.put(fill, source)
52
+ end
53
+ )
54
+
55
+ response
56
+ end
57
+
58
+ def fill
59
+ template = Addressable::Template.new(path)
60
+
61
+ template_keys = template.keys.map(&:to_sym)
62
+ expansions = {}
63
+ query_values = {}
64
+
65
+ url_params.each do |p|
66
+ val = self.send(p)
67
+
68
+ if template_keys.include? p
69
+ expansions[p] = val if val
70
+ else
71
+ query_values[p] = val if val
72
+ end
73
+ end
74
+
75
+ uri = template.expand(expansions)
76
+ uri.query_values = query_values unless query_values.empty?
77
+ uri
78
+ end
79
+
80
+ def url_params
81
+ if self.respond_to? :source_param
82
+ source_params = Array(source_param)
83
+ else
84
+ source_params = []
85
+ end
86
+
87
+ (parameters - source_params)
88
+ end
89
+
90
+ def request_method
91
+ @request_method || :get
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,22 @@
1
+ module Eson
2
+ module ResponseParser
3
+ class JSONParseError < Eson::Error
4
+ attr_accessor :source
5
+ end
6
+
7
+ def call(*args)
8
+ parse(super)
9
+ end
10
+
11
+ def parse(response)
12
+ begin
13
+ MultiJson.decode(response.body) if response.body
14
+ rescue MultiJson::DecodeError => e
15
+ error = JSONParseError.new(e.message, response)
16
+ error.source = response.body
17
+ raise error
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ module Eson
2
+ module StatusHandler
3
+ def call(*args)
4
+ handle(super)
5
+ end
6
+
7
+ def handle(response)
8
+ case response.status
9
+ when 404
10
+ if /IndexMissingException/.match(response.body)
11
+ raise Eson::IndexNotFoundError.new(response.body, response)
12
+ else
13
+ raise Eson::NotFoundError.new(response, response)
14
+ end
15
+ else
16
+ if response.status >= 400
17
+ raise Eson::Error.new(response[:error], response)
18
+ else
19
+ response
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
data/log4j.properties ADDED
@@ -0,0 +1,18 @@
1
+ log4j.rootLogger=INFO, out
2
+ log4j.logger.jgroups=WARN
3
+
4
+ #log4j.logger.discovery=TRACE
5
+ #log4j.logger.cluster=TRACE
6
+ #log4j.logger.indices.cluster=DEBUG
7
+ #log4j.logger.index=TRACE
8
+ #log4j.logger.index.engine=DEBUG
9
+ #log4j.logger.index.shard=TRACE
10
+ #log4j.logger.index.cache=DEBUG
11
+ #log4j.logger.http=TRACE
12
+ #log4j.logger.monitor.jvm=DEBUG
13
+ #log4j.logger.cluster.action.shard=TRACE
14
+ #log4j.logger.index.gateway=TRACE
15
+
16
+ log4j.appender.out=org.apache.log4j.ConsoleAppender
17
+ log4j.appender.out.layout=org.apache.log4j.PatternLayout
18
+ log4j.appender.out.layout.ConversionPattern=[%d{ABSOLUTE}][%-5p][%-25c] %m%n
@@ -0,0 +1,14 @@
1
+ require './test/test_config'
2
+
3
+ context "HTTP client" do
4
+ helper(:node) { Node::External.instance }
5
+
6
+ helper(:client) do
7
+ Eson::HTTP::Client.new(:server => "http://#{node.ip}:#{node.port}",
8
+ :logger => 'test/test.log',
9
+ :auto_call => false)
10
+ end
11
+
12
+ asserts("too many params") { client.index.params = {:foo => :bar} }.raises(NoMethodError)
13
+
14
+ end
@@ -0,0 +1,58 @@
1
+ require './test/test_config'
2
+
3
+ context 'HTTP client cluster api' do
4
+ helper(:node) { Node::External.instance }
5
+
6
+ helper(:client) do
7
+ Eson::HTTP::Client.new(:server => "http://#{node.ip}:#{node.port}",
8
+ :logger => 'test/test.log')
9
+ end
10
+
11
+ context "cluster health" do
12
+ setup do
13
+ client.health
14
+ end
15
+
16
+ asserts("returns cluster info") { topic["cluster_name"]}.equals("elasticsearch")
17
+ end
18
+
19
+ context "cluster state" do
20
+ setup do
21
+ client.state
22
+ end
23
+
24
+ asserts("returns metadata") { topic["metadata"] }
25
+ end
26
+
27
+ context "cluster state with filtered metadata" do
28
+ setup do
29
+ client.state :filter_metadata => true
30
+ end
31
+
32
+ asserts("metadata") { topic["metadata"] }.equals(nil)
33
+ end
34
+
35
+ context "cluster nodes info" do
36
+ setup do
37
+ client.nodes
38
+ end
39
+
40
+ asserts("returns nodes info") { topic["nodes"] }
41
+ end
42
+
43
+ context "filtered cluster nodes" do
44
+ setup do
45
+ client.nodes :nodes => "non_existant_node"
46
+ end
47
+
48
+ asserts("returns no nodes info") { topic["nodes"] }.empty
49
+ end
50
+
51
+ context "stats" do
52
+ setup do
53
+ client.stats
54
+ end
55
+
56
+ asserts("returns nodes info") { topic["nodes"] }
57
+ end
58
+ end