Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add middleware #25

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
minds_sdk (1.0.0)
minds_sdk (1.0.1)
faraday (~> 2.12)
ruby-openai (~> 7.3, >= 7.3.1)

Expand Down
4 changes: 2 additions & 2 deletions lib/minds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def call(env)
error_suffix = "\033[0m"

formatted_message = msg.split("\n").map do |line|
"#{' ' * 15}#{line}"
"#{' ' * 14}#{line}"
end.join("\n")

"#{error_prefix} Rest Client Error\n#{formatted_message}#{error_suffix}\n"
"#{error_prefix} Minds Client Error\n#{formatted_message}#{error_suffix}\n"
end

logger.error(e.response[:body])
Expand Down
6 changes: 4 additions & 2 deletions lib/minds/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ class Client

SENSITIVE_ATTRIBUTES = %i[@base_url @api_key].freeze
CONFIG_KEYS = %i[base_url api_key log_errors api_version].freeze
attr_reader(*CONFIG_KEYS)
attr_reader(*CONFIG_KEYS, :faraday_middleware)

def initialize(options = {})
def initialize(options = {}, &faraday_middleware)
# if key not present. Fall back to global config
CONFIG_KEYS.each do |key|
instance_variable_set "@#{key}", options[key] || Client.config.send(key)
end

@faraday_middleware = faraday_middleware
end

class << self
Expand Down
5 changes: 4 additions & 1 deletion lib/minds/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ def uri(path:)
end

def conn
Faraday.new(url: @base_url) do |builder|
connection = Faraday.new(url: @base_url) do |builder|
builder.use MiddlewareErrors if @log_errors
builder.headers["Authorization"] = "Bearer #{@api_key}"
builder.headers["Content-Type"] = "application/json"
builder.request :json
builder.response :json
builder.response :raise_error
end

@faraday_middleware&.call(connection)
connection
end
end
end