open_router 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 617d2bf77818298ec2a2b1f3c56ffd8ae344a969b2e3875c65017cbe9462d084
4
- data.tar.gz: 0a1f88cc14ed99e96f1a54629ddbfb0139626235824c15edb6aa693df149a39c
3
+ metadata.gz: 3f81b52efb709758f501e3e43fd3967d36a0cde6b78aefd2512b21aafa0f3f5d
4
+ data.tar.gz: 347a0b754f0ed374ab6bdb0847e8efe1f762370c4c4e7b1faf1e3a7813dece10
5
5
  SHA512:
6
- metadata.gz: '0787bb54df0cf1f38eba5263a58c7840a48d0aa5f4ddb1bef4eb47f95b2b4568ee934b4ab1d666d927a09e94f06a68ad5ade55f54ae6fed0d9af4ef78ec7f0e1'
7
- data.tar.gz: bd2bf9db26382fea15ab814336e937fb73f1c7fa117d404311c82489a8248254af7ea7a593e74c57ceee43b4a573cf48650887facdf19758e4558c5df94f3a53
6
+ metadata.gz: 64d714806a4cda2841d1310357db4a8ed2737a064530676f484fe4213d2dfc863e6143b159915f57177f33448ca03585b953b164cb0825929d426ec2beeb30ba
7
+ data.tar.gz: a0fe64e1d6dcc790c478fa888f5b1db43edaec01001ca5f6903481042fb41a65d8808951d78bc49904797074620e8fdbb9616ffedcb8b1be2c0a1ee667b2de55
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
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,8 +6,14 @@ 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"
9
+
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
+ gem "solargraph-rails", "~> 0.2.0.pre"
17
+ gem "sorbet"
18
+ gem "tapioca", require: false
19
+ end
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- open_router (0.2.1)
4
+ open_router (0.2.2)
5
5
  activesupport (>= 6.0)
6
6
  dotenv (>= 2)
7
7
  faraday (>= 1)
@@ -21,7 +21,9 @@ GEM
21
21
  mutex_m
22
22
  tzinfo (~> 2.0)
23
23
  ast (2.4.2)
24
+ backport (1.2.0)
24
25
  base64 (0.2.0)
26
+ benchmark (0.3.0)
25
27
  bigdecimal (3.1.7)
26
28
  coderay (1.1.3)
27
29
  concurrent-ruby (1.2.3)
@@ -29,6 +31,8 @@ GEM
29
31
  diff-lcs (1.5.0)
30
32
  dotenv (3.1.0)
31
33
  drb (2.2.1)
34
+ e2mmap (0.1.0)
35
+ erubi (1.12.0)
32
36
  faraday (2.7.10)
33
37
  faraday-net_http (>= 2.0, < 3.1)
34
38
  ruby2_keywords (>= 0.0.4)
@@ -37,23 +41,40 @@ GEM
37
41
  faraday-net_http (3.0.2)
38
42
  i18n (1.14.4)
39
43
  concurrent-ruby (~> 1.0)
44
+ jaro_winkler (1.5.6)
40
45
  json (2.6.3)
46
+ kramdown (2.4.0)
47
+ rexml
48
+ kramdown-parser-gfm (1.1.0)
49
+ kramdown (~> 2.0)
41
50
  language_server-protocol (3.17.0.3)
42
51
  method_source (1.0.0)
43
52
  minitest (5.22.3)
44
53
  multipart-post (2.3.0)
45
54
  mutex_m (0.2.0)
55
+ netrc (0.11.0)
56
+ nokogiri (1.16.4-arm64-darwin)
57
+ racc (~> 1.4)
58
+ nokogiri (1.16.4-x86_64-linux)
59
+ racc (~> 1.4)
46
60
  parallel (1.23.0)
47
61
  parser (3.2.2.3)
48
62
  ast (~> 2.4.1)
49
63
  racc
64
+ prism (0.24.0)
50
65
  pry (0.14.2)
51
66
  coderay (~> 1.1)
52
67
  method_source (~> 1.0)
53
68
  racc (1.7.1)
54
69
  rainbow (3.1.1)
55
70
  rake (13.0.6)
71
+ rbi (0.1.10)
72
+ prism (>= 0.18.0, < 0.25)
73
+ sorbet-runtime (>= 0.5.9204)
74
+ rbs (2.8.4)
56
75
  regexp_parser (2.8.1)
76
+ reverse_markdown (2.1.1)
77
+ nokogiri
57
78
  rexml (3.2.6)
58
79
  rspec (3.12.0)
59
80
  rspec-core (~> 3.12.0)
@@ -83,9 +104,56 @@ GEM
83
104
  parser (>= 3.2.1.0)
84
105
  ruby-progressbar (1.13.0)
85
106
  ruby2_keywords (0.0.5)
107
+ solargraph (0.50.0)
108
+ backport (~> 1.2)
109
+ benchmark
110
+ bundler (~> 2.0)
111
+ diff-lcs (~> 1.4)
112
+ e2mmap
113
+ jaro_winkler (~> 1.5)
114
+ kramdown (~> 2.3)
115
+ kramdown-parser-gfm (~> 1.1)
116
+ parser (~> 3.0)
117
+ rbs (~> 2.0)
118
+ reverse_markdown (~> 2.0)
119
+ rubocop (~> 1.38)
120
+ thor (~> 1.0)
121
+ tilt (~> 2.0)
122
+ yard (~> 0.9, >= 0.9.24)
123
+ solargraph-rails (0.2.2.pre.4)
124
+ activesupport
125
+ solargraph (>= 0.41.1)
126
+ sorbet (0.5.11342)
127
+ sorbet-static (= 0.5.11342)
128
+ sorbet-runtime (0.5.11342)
129
+ sorbet-static (0.5.11342-universal-darwin)
130
+ sorbet-static (0.5.11342-x86_64-linux)
131
+ sorbet-static-and-runtime (0.5.11342)
132
+ sorbet (= 0.5.11342)
133
+ sorbet-runtime (= 0.5.11342)
134
+ spoom (1.3.0)
135
+ erubi (>= 1.10.0)
136
+ prism (>= 0.19.0)
137
+ sorbet-static-and-runtime (>= 0.5.10187)
138
+ thor (>= 0.19.2)
139
+ tapioca (0.13.1)
140
+ bundler (>= 2.2.25)
141
+ netrc (>= 0.11.0)
142
+ parallel (>= 1.21.0)
143
+ rbi (>= 0.1.4, < 0.2)
144
+ sorbet-static-and-runtime (>= 0.5.11087)
145
+ spoom (>= 1.2.0)
146
+ thor (>= 1.2.0)
147
+ yard-sorbet
148
+ thor (1.3.1)
149
+ tilt (2.3.0)
86
150
  tzinfo (2.0.6)
87
151
  concurrent-ruby (~> 1.0)
88
152
  unicode-display_width (2.4.2)
153
+ yard (0.9.36)
154
+ yard-sorbet (0.8.1)
155
+ sorbet-runtime (>= 0.5)
156
+ yard (>= 0.9)
89
157
 
90
158
  PLATFORMS
91
159
  arm64-darwin-21
@@ -99,6 +167,9 @@ DEPENDENCIES
99
167
  rake (~> 13.0)
100
168
  rspec (~> 3.0)
101
169
  rubocop (~> 1.21)
170
+ solargraph-rails (~> 0.2.0.pre)
171
+ sorbet
172
+ tapioca
102
173
 
103
174
  BUNDLED WITH
104
175
  2.4.12
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.1"
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
data/open_router.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.summary = "Ruby library for OpenRouter API."
12
12
  spec.homepage = "https://github.com/OlympiaAI/open_router"
13
13
  spec.license = "MIT"
14
- spec.required_ruby_version = ">= 3.2.1"
14
+ spec.required_ruby_version = ">= 3.2.2"
15
15
 
16
16
  spec.metadata["homepage_uri"] = spec.homepage
17
17
  spec.metadata["source_code_uri"] = "https://github.com/OlympiaAI/open_router"
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.1
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-03-28 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
@@ -75,6 +75,7 @@ extra_rdoc_files: []
75
75
  files:
76
76
  - ".rspec"
77
77
  - ".rubocop.yml"
78
+ - ".ruby-version"
78
79
  - CHANGELOG.md
79
80
  - CODE_OF_CONDUCT.md
80
81
  - Gemfile
@@ -103,14 +104,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
104
  requirements:
104
105
  - - ">="
105
106
  - !ruby/object:Gem::Version
106
- version: 3.2.1
107
+ version: 3.2.2
107
108
  required_rubygems_version: !ruby/object:Gem::Requirement
108
109
  requirements:
109
110
  - - ">="
110
111
  - !ruby/object:Gem::Version
111
112
  version: '0'
112
113
  requirements: []
113
- rubygems_version: 3.4.12
114
+ rubygems_version: 3.4.10
114
115
  signing_key:
115
116
  specification_version: 4
116
117
  summary: Ruby library for OpenRouter API.