Skip to content

Commit 658436e

Browse files
Additional endpoints
1 parent 6ba6abd commit 658436e

File tree

9 files changed

+151
-0
lines changed

9 files changed

+151
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ client.query.aggs(
187187
)
188188
```
189189

190+
#### Nodes
191+
```ruby
192+
client.nodes
193+
```
194+
195+
#### Health
196+
```ruby
197+
client.live?
198+
client.ready?
199+
```
200+
190201
## Development
191202

192203
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

lib/weaviate.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module Weaviate
1111
autoload :Objects, "weaviate/objects"
1212
autoload :OIDC, "weaviate/oidc"
1313
autoload :Query, "weaviate/query"
14+
autoload :Nodes, "weaviate/nodes"
15+
autoload :Health, "weaviate/health"
1416

1517
module Response
1618
autoload :Base, "weaviate/response/base"

lib/weaviate/client.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ def meta
4444
@meta.get
4545
end
4646

47+
def nodes
48+
@nodes ||= Weaviate::Nodes.new(client: self)
49+
@nodes.list
50+
end
51+
52+
def live?
53+
@health ||= Weaviate::Health.new(client: self)
54+
@health.live?
55+
end
56+
57+
def ready?
58+
@health ||= Weaviate::Health.new(client: self)
59+
@health.ready?
60+
end
61+
4762
def objects
4863
@objects ||= Weaviate::Objects.new(client: self)
4964
end

lib/weaviate/health.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module Weaviate
4+
class Health < Base
5+
PATH = ".well-known"
6+
7+
def live?
8+
response = client.connection.get("#{PATH}/live")
9+
response.status == 200
10+
end
11+
12+
def ready?
13+
response = client.connection.get("#{PATH}/ready")
14+
response.status == 200
15+
end
16+
end
17+
end

lib/weaviate/nodes.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module Weaviate
4+
class Nodes < Base
5+
PATH = "nodes"
6+
7+
def list
8+
response = client.connection.get(PATH)
9+
response.body
10+
end
11+
end
12+
end

lib/weaviate/objects.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ def create(
4040
req.body["id"] = id unless id.nil?
4141
req.body["vector"] = vector unless vector.nil?
4242
end
43+
4344
if response.success?
4445
Weaviate::Response::Object.new(response.body)
46+
else
47+
response.body
4548
end
4649
end
4750

spec/fixtures/nodes.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"nodes": [
3+
{
4+
"gitHash": "00c9d31",
5+
"name": "weaviate-0",
6+
"shards": [
7+
{
8+
"class": "Question",
9+
"name": "G1SJoaYlFnm4",
10+
"objectCount": 10
11+
}
12+
],
13+
"stats": {
14+
"objectCount": 10,
15+
"shardCount": 1
16+
},
17+
"status": "HEALTHY",
18+
"version": "1.18.1"
19+
}
20+
]
21+
}

spec/weaviate/health_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe Weaviate::Health do
6+
let(:client) {
7+
Weaviate::Client.new(
8+
scheme: "http",
9+
host: "localhost:8080"
10+
)
11+
}
12+
13+
let(:response) {
14+
OpenStruct.new(status: 200)
15+
}
16+
17+
describe "#live" do
18+
before do
19+
allow_any_instance_of(Faraday::Connection).to receive(:get)
20+
.with(".well-known/live")
21+
.and_return(response)
22+
end
23+
24+
it "return 200" do
25+
expect(client.live?).to eq(true)
26+
end
27+
end
28+
29+
describe "#ready" do
30+
before do
31+
allow_any_instance_of(Faraday::Connection).to receive(:get)
32+
.with(".well-known/ready")
33+
.and_return(response)
34+
end
35+
36+
it "return 200" do
37+
expect(client.ready?).to eq(true)
38+
end
39+
end
40+
end

spec/weaviate/nodes_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe Weaviate::Nodes do
6+
let(:client) {
7+
Weaviate::Client.new(
8+
scheme: "http",
9+
host: "localhost:8080"
10+
)
11+
}
12+
13+
let(:nodes_fixture) { JSON.parse(File.read("spec/fixtures/nodes.json")) }
14+
15+
describe "#list" do
16+
let(:response) {
17+
OpenStruct.new(body: nodes_fixture)
18+
}
19+
20+
before do
21+
allow_any_instance_of(Faraday::Connection).to receive(:get)
22+
.with("nodes")
23+
.and_return(response)
24+
end
25+
26+
it "return the nodes info" do
27+
expect(client.nodes).to be_a(Hash)
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)