Class: SavvyOpenrouter::Connection
- Inherits:
-
Object
- Object
- SavvyOpenrouter::Connection
- Includes:
- Instrumentation
- Defined in:
- lib/savvy_openrouter/connection.rb,
lib/savvy_openrouter/connection_instrumentation.rb
Defined Under Namespace
Modules: Instrumentation
Constant Summary collapse
- DEFAULT_SUCCESS =
[200, 201, 202, 204].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #delete(path, params: nil, success: DEFAULT_SUCCESS) ⇒ Object
- #get(path, params: nil, success: DEFAULT_SUCCESS) ⇒ Object
- #get_raw(path, params: nil, success: [200]) ⇒ Object
-
#initialize(config) ⇒ Connection
constructor
A new instance of Connection.
- #patch(path, body:, success: DEFAULT_SUCCESS) ⇒ Object
- #post(path, body:, success: DEFAULT_SUCCESS) ⇒ Object
- #post_raw(path, body:, success: [200]) ⇒ Object
- #put(path, body:, success: DEFAULT_SUCCESS) ⇒ Object
- #stream_get(path, params: nil, &block) ⇒ Object
- #stream_post(path, body) ⇒ Object
Constructor Details
#initialize(config) ⇒ Connection
Returns a new instance of Connection.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/savvy_openrouter/connection.rb', line 18 def initialize(config) @config = config @api_call_logger = ApiCallLogger.new(config.api_call_log) base = normalize_base(config.base_url) headers = build_headers @conn = Faraday.new(url: base, headers: headers) do |faraday| faraday.request :json faraday.response :json, content_type: /\bjson/, parser_options: { symbolize_names: true } faraday.adapter Faraday.default_adapter end @raw = Faraday.new(url: base, headers: headers) do |faraday| faraday.adapter Faraday.default_adapter end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
16 17 18 |
# File 'lib/savvy_openrouter/connection.rb', line 16 def config @config end |
Instance Method Details
#delete(path, params: nil, success: DEFAULT_SUCCESS) ⇒ Object
37 38 39 |
# File 'lib/savvy_openrouter/connection.rb', line 37 def delete(path, params: nil, success: DEFAULT_SUCCESS) timed_json(:delete, path, params: params, success: success) end |
#get(path, params: nil, success: DEFAULT_SUCCESS) ⇒ Object
33 34 35 |
# File 'lib/savvy_openrouter/connection.rb', line 33 def get(path, params: nil, success: DEFAULT_SUCCESS) timed_json(:get, path, params: params, success: success) end |
#get_raw(path, params: nil, success: [200]) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/savvy_openrouter/connection.rb', line 53 def get_raw(path, params: nil, success: [200]) rel = rel_path(path) started = monotonic_ms response = @raw.get(rel) do |req| req.params.update(params) if params end duration_ms = elapsed_ms(started) record_faraday_raw( method: "GET", rel_path: rel, params: params, request_body: nil, response: response, duration_ms: duration_ms ) status = response.status return response.body.b.freeze if success.include?(status) raise_api_error(status, response.body) rescue SavvyOpenrouter::ApiError raise rescue StandardError => e record_transport_error( method: "GET", rel_path: rel, params: params, request_body: nil, duration_ms: elapsed_ms(started), error: e ) raise end |
#patch(path, body:, success: DEFAULT_SUCCESS) ⇒ Object
45 46 47 |
# File 'lib/savvy_openrouter/connection.rb', line 45 def patch(path, body:, success: DEFAULT_SUCCESS) timed_json(:patch, path, body: body, success: success) end |
#post(path, body:, success: DEFAULT_SUCCESS) ⇒ Object
41 42 43 |
# File 'lib/savvy_openrouter/connection.rb', line 41 def post(path, body:, success: DEFAULT_SUCCESS) timed_json(:post, path, body: body, success: success) end |
#post_raw(path, body:, success: [200]) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/savvy_openrouter/connection.rb', line 86 def post_raw(path, body:, success: [200]) rel = rel_path(path) started = monotonic_ms response = @raw.post(rel) do |req| req.headers["Content-Type"] = "application/json" req.body = JSON.generate(stringify_body(body)) end duration_ms = elapsed_ms(started) record_faraday_raw( method: "POST", rel_path: rel, params: nil, request_body: body, response: response, duration_ms: duration_ms ) status = response.status return response.body.b.freeze if success.include?(status) raise_api_error(status, response.body) rescue SavvyOpenrouter::ApiError raise rescue StandardError => e record_transport_error( method: "POST", rel_path: rel, params: nil, request_body: body, duration_ms: elapsed_ms(started), error: e ) raise end |
#put(path, body:, success: DEFAULT_SUCCESS) ⇒ Object
49 50 51 |
# File 'lib/savvy_openrouter/connection.rb', line 49 def put(path, body:, success: DEFAULT_SUCCESS) timed_json(:put, path, body: body, success: success) end |
#stream_get(path, params: nil, &block) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/savvy_openrouter/connection.rb', line 120 def stream_get(path, params: nil, &block) ensure_api_key! uri = join_uri(path) uri.query = URI.encode_www_form(params) if params && !params.empty? req = Net::HTTP::Get.new(uri) build_headers.each { |k, v| req[k] = v } started = monotonic_ms status, err_buf = stream_via_net_http(uri, req, &block) duration_ms = elapsed_ms(started) record_stream( method: "GET", rel_path: rel_path(path), params: params, request_body: nil, status: status, response_body: err_buf, duration_ms: duration_ms ) return if status == 200 raise_api_error(status, err_buf) if status rescue SavvyOpenrouter::ApiError raise rescue StandardError => e record_transport_error( method: "GET", rel_path: rel_path(path), params: params, request_body: nil, duration_ms: elapsed_ms(started), error: e ) raise end |
#stream_post(path, body) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/savvy_openrouter/connection.rb', line 157 def stream_post(path, body, &) ensure_api_key! uri = join_uri(path) req = Net::HTTP::Post.new(uri) build_headers.each { |k, v| req[k] = v } req["Content-Type"] = "application/json" req["Accept"] = "text/event-stream" req.body = JSON.generate(stringify_body(body)) started = monotonic_ms status, err_buf = stream_via_net_http(uri, req, &) duration_ms = elapsed_ms(started) record_stream( method: "POST", rel_path: rel_path(path), params: nil, request_body: body, status: status, response_body: err_buf, duration_ms: duration_ms ) return if status == 200 raise_api_error(status, err_buf) if status rescue SavvyOpenrouter::ApiError raise rescue StandardError => e record_transport_error( method: "POST", rel_path: rel_path(path), params: nil, request_body: body, duration_ms: elapsed_ms(started), error: e ) raise end |