Skip to content

Commit c73d518

Browse files
Support for api_key authentication
1 parent 0c5fe4c commit c73d518

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require 'weaviate'
2222
client = Weaviate::Client.new(
2323
scheme: 'https',
2424
host: 'some-endpoint.weaviate.network', # Replace with your endpoint
25+
api_key: '', # Weaviate API key
2526
model_service: :openai, # Service that will be used to generate vectors. Possible values: :openai, :cohere, :huggingface
2627
model_service_api_key: 'xxxxxxx' # Either OpenAI, Cohere or Hugging Face API key
2728
)

bin/console

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ require "weaviate"
1212
# Pry.start
1313

1414
client = Weaviate::Client.new(
15-
scheme: "https",
15+
scheme: "http",
1616
host: ENV["WEAVIATE_HOST"],
17+
api_key: ENV["WEAVIATE_API_KEY"],
1718
model_service: :openai,
1819
model_service_api_key: ENV["MODEL_SERVICE_API_KEY"]
1920
)

lib/weaviate/client.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
module Weaviate
77
class Client
8-
attr_reader :scheme, :host, :model_service, :model_service_api_key, :adapter
8+
attr_reader :scheme, :host, :api_key, :model_service, :model_service_api_key, :adapter
99

1010
API_VERSION = "v1"
1111

@@ -18,6 +18,7 @@ class Client
1818
def initialize(
1919
scheme:,
2020
host:,
21+
api_key: nil,
2122
model_service: nil,
2223
model_service_api_key: nil,
2324
adapter: Faraday.default_adapter
@@ -26,6 +27,7 @@ def initialize(
2627

2728
@scheme = scheme
2829
@host = host
30+
@api_key = api_key
2931
@model_service = model_service
3032
@model_service_api_key = model_service_api_key
3133
@adapter = adapter
@@ -93,6 +95,9 @@ def graphql
9395

9496
def connection
9597
@connection ||= Faraday.new(url: "#{scheme}://#{host}/#{API_VERSION}/") do |faraday|
98+
if api_key
99+
faraday.request :authorization, :Bearer, api_key
100+
end
96101
faraday.request :json
97102
faraday.response :json, content_type: /\bjson$/
98103
faraday.adapter adapter

spec/weaviate/client_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,46 @@
5353
end
5454
end
5555

56+
describe "#ready?" do
57+
let(:response) { OpenStruct.new(status: 200) }
58+
59+
before do
60+
allow_any_instance_of(Faraday::Connection).to receive(:get)
61+
.with(".well-known/ready")
62+
.and_return(response)
63+
end
64+
65+
it "returns a query client" do
66+
expect(client.ready?).to eq(true)
67+
end
68+
end
69+
70+
describe "#live?" do
71+
let(:response) { OpenStruct.new(status: 200) }
72+
73+
before do
74+
allow_any_instance_of(Faraday::Connection).to receive(:get)
75+
.with(".well-known/live")
76+
.and_return(response)
77+
end
78+
79+
it "returns a query client" do
80+
expect(client.live?).to eq(true)
81+
end
82+
end
83+
84+
describe "#classifications" do
85+
it "returns a classifications client" do
86+
expect(client.classifications).to be_a(Weaviate::Classifications)
87+
end
88+
end
89+
90+
describe "#backups" do
91+
it "returns a backups client" do
92+
expect(client.backups).to be_a(Weaviate::Backups)
93+
end
94+
end
95+
5696
describe "#oidc" do
5797
let(:fixture) { JSON.parse(File.read("spec/fixtures/oidc.json")) }
5898
let(:response) { OpenStruct.new(body: fixture) }

0 commit comments

Comments
 (0)