Skip to content

Commit

Permalink
Merge pull request #25 from tungnt1203/faraday_middleware
Browse files Browse the repository at this point in the history
add middleware
  • Loading branch information
tungnt1203 authored Nov 13, 2024
2 parents 7a4c281 + d0b79dd commit fca41fb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
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

0 comments on commit fca41fb

Please sign in to comment.