open_router 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ff9756e6285ab09ccaf109eabe0a36747e488450de363ce8ce460b94eb397bc
4
- data.tar.gz: 3dcb3957fbbe02cc15d0c0806bcbd248b1b3e64e75f48e60789393285f70ed33
3
+ metadata.gz: 3f81b52efb709758f501e3e43fd3967d36a0cde6b78aefd2512b21aafa0f3f5d
4
+ data.tar.gz: 347a0b754f0ed374ab6bdb0847e8efe1f762370c4c4e7b1faf1e3a7813dece10
5
5
  SHA512:
6
- metadata.gz: 644c0c1fd8ec173af8df82df65496483fa01560a38a8bf1aa23f6473b981333b216bad020f86f14a43f09450803af144891ee696af078ca4a274eaaae6d729ba
7
- data.tar.gz: 52b2870105b521eae5bd34302d8c95232d018a407021d8dcff47eac0806fd81b6c4beea6191f3cc57e674af06de61cf9cb1f3d98934d4a86cfea31f09bb872f5
6
+ metadata.gz: 64d714806a4cda2841d1310357db4a8ed2737a064530676f484fe4213d2dfc863e6143b159915f57177f33448ca03585b953b164cb0825929d426ec2beeb30ba
7
+ data.tar.gz: a0fe64e1d6dcc790c478fa888f5b1db43edaec01001ca5f6903481042fb41a65d8808951d78bc49904797074620e8fdbb9616ffedcb8b1be2c0a1ee667b2de55
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
- ## [Unreleased]
1
+ ## [0.3.0] - 2024-05-03
2
+
3
+ - Uses Faraday's built-in JSON mode
4
+ - Added support for configuring Faraday and its middleware
5
+ - Spec creates a STDOUT logger by default (headers, bodies, errors)
6
+ - Spec filters Bearer token from logs by default
2
7
 
3
8
  ## [0.1.0] - 2024-03-19
4
9
 
5
10
  - Initial release
11
+
12
+ ## [Unreleased]
data/Gemfile CHANGED
@@ -6,13 +6,13 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "activesupport", ">= 6.0"
9
- gem "dotenv", ">= 2"
10
- gem "pry", ">= 0.14"
11
- gem "rake", "~> 13.0"
12
- gem "rspec", "~> 3.0"
13
- gem "rubocop", "~> 1.21"
14
9
 
15
10
  group :development do
11
+ gem "dotenv", ">= 2"
12
+ gem "pry", ">= 0.14"
13
+ gem "rake", "~> 13.0"
14
+ gem "rspec", "~> 3.0"
15
+ gem "rubocop", "~> 1.21"
16
16
  gem "solargraph-rails", "~> 0.2.0.pre"
17
17
  gem "sorbet"
18
18
  gem "tapioca", require: false
data/README.md CHANGED
@@ -63,6 +63,32 @@ Then you can create a client like this:
63
63
  client = OpenRouter::Client.new
64
64
  ```
65
65
 
66
+ #### Configure Faraday
67
+
68
+ The configuration object exposes a [`faraday`](https://github.com/lostisland/faraday-retry) method that you can pass a block to configure Faraday settings and middleware.
69
+
70
+ This example adds `faraday-retry` and a logger that redacts the api key so it doesn't get leaked to logs.
71
+
72
+ ```ruby
73
+ require 'faraday/retry'
74
+
75
+ retry_options = {
76
+ max: 2,
77
+ interval: 0.05,
78
+ interval_randomness: 0.5,
79
+ backoff_factor: 2
80
+ }
81
+
82
+ OpenRouter::Client.new(access_token: ENV["ACCESS_TOKEN"]) do |config|
83
+ config.faraday do |f|
84
+ f.request :retry, retry_options
85
+ f.response :logger, ::Logger.new($stdout), { headers: true, bodies: true, errors: true } do |logger|
86
+ logger.filter(/(Bearer) (\S+)/, '\1[REDACTED]')
87
+ end
88
+ end
89
+ end
90
+ ```
91
+
66
92
  #### Change version or timeout
67
93
 
68
94
  The default timeout for any request using this library is 120 seconds. You can change that by passing a number of seconds to the `request_timeout` when initializing the client.
@@ -41,7 +41,7 @@ module OpenRouter
41
41
  parameters[:stream] = stream if stream
42
42
  parameters.merge!(extras)
43
43
 
44
- json_post(path: "/chat/completions", parameters:).tap do |response|
44
+ post(path: "/chat/completions", parameters:).tap do |response|
45
45
  raise ServerError, "Empty response from OpenRouter. Might be worth retrying once or twice." if response.blank?
46
46
  raise ServerError, response.dig("error", "message") if response.dig("error", "message").present?
47
47
  end.with_indifferent_access
@@ -3,13 +3,13 @@
3
3
  module OpenRouter
4
4
  module HTTP
5
5
  def get(path:)
6
- to_json(conn.get(uri(path:)) do |req|
6
+ conn.get(uri(path:)) do |req|
7
7
  req.headers = headers
8
- end&.body)
8
+ end&.body
9
9
  end
10
10
 
11
- def json_post(path:, parameters:)
12
- to_json(conn.post(uri(path:)) do |req|
11
+ def post(path:, parameters:)
12
+ conn.post(uri(path:)) do |req|
13
13
  if parameters[:stream].respond_to?(:call)
14
14
  req.options.on_data = to_json_stream(user_proc: parameters[:stream])
15
15
  parameters[:stream] = true # Necessary to tell OpenRouter to stream.
@@ -17,33 +17,24 @@ module OpenRouter
17
17
 
18
18
  req.headers = headers
19
19
  req.body = parameters.to_json
20
- end&.body)
20
+ end&.body
21
21
  end
22
22
 
23
23
  def multipart_post(path:, parameters: nil)
24
- to_json(conn(multipart: true).post(uri(path:)) do |req|
24
+ conn(multipart: true).post(uri(path:)) do |req|
25
25
  req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
26
26
  req.body = multipart_parameters(parameters)
27
- end&.body)
27
+ end&.body
28
28
  end
29
29
 
30
30
  def delete(path:)
31
- to_json(conn.delete(uri(path:)) do |req|
31
+ conn.delete(uri(path:)) do |req|
32
32
  req.headers = headers
33
- end&.body)
33
+ end&.body
34
34
  end
35
35
 
36
36
  private
37
37
 
38
- def to_json(string)
39
- return unless string
40
-
41
- JSON.parse(string)
42
- rescue JSON::ParserError
43
- # Convert a multiline string of JSON objects to a JSON array.
44
- JSON.parse(string.gsub("}\n{", "},{").prepend("[").concat("]"))
45
- end
46
-
47
38
  # Given a proc, returns an outer proc that can be used to iterate over a JSON stream of chunks.
48
39
  # For each chunk, the inner user_proc is called giving it the JSON object. The JSON object could
49
40
  # be a data object or an error object as described in the OpenRouter API documentation.
@@ -66,6 +57,11 @@ module OpenRouter
66
57
  Faraday.new do |f|
67
58
  f.options[:timeout] = OpenRouter.configuration.request_timeout
68
59
  f.request(:multipart) if multipart
60
+ f.use MiddlewareErrors if @log_errors
61
+ f.response :raise_error
62
+ f.response :json
63
+
64
+ OpenRouter.configuration.faraday_config&.call(f)
69
65
  end
70
66
  end
71
67
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenRouter
4
- VERSION = "0.2.2"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/open_router.rb CHANGED
@@ -13,18 +13,19 @@ module OpenRouter
13
13
 
14
14
  class Configuration
15
15
  attr_writer :access_token
16
- attr_accessor :api_version, :extra_headers, :request_timeout, :uri_base
16
+ attr_accessor :api_version, :extra_headers, :faraday_config, :log_errors, :request_timeout, :uri_base
17
17
 
18
18
  DEFAULT_API_VERSION = "v1"
19
19
  DEFAULT_REQUEST_TIMEOUT = 120
20
20
  DEFAULT_URI_BASE = "https://openrouter.ai/api"
21
21
 
22
22
  def initialize
23
- @access_token = nil
24
- @uri_base = DEFAULT_URI_BASE
25
- @api_version = DEFAULT_API_VERSION
26
- @request_timeout = DEFAULT_REQUEST_TIMEOUT
27
- @extra_headers = {}
23
+ self.access_token = nil
24
+ self.api_version = DEFAULT_API_VERSION
25
+ self.extra_headers = {}
26
+ self.log_errors = false
27
+ self.request_timeout = DEFAULT_REQUEST_TIMEOUT
28
+ self.uri_base = DEFAULT_URI_BASE
28
29
  end
29
30
 
30
31
  def access_token
@@ -33,6 +34,10 @@ module OpenRouter
33
34
  raise ConfigurationError, "OpenRouter access token missing!"
34
35
  end
35
36
 
37
+ def faraday(&block)
38
+ self.faraday_config = block
39
+ end
40
+
36
41
  def site_name=(value)
37
42
  @extra_headers["X-Title"] = value
38
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_router
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Obie Fernandez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-15 00:00:00.000000000 Z
11
+ date: 2024-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport