Class: SavvyOpenrouter::Connection
- Inherits:
-
Object
- Object
- SavvyOpenrouter::Connection
- Includes:
- ApiCallRecording, ApiCallRecordingTransport, Instrumentation
- Defined in:
- lib/savvy_openrouter/connection.rb,
lib/savvy_openrouter/connection_instrumentation.rb,
lib/savvy_openrouter/connection_api_call_recording.rb,
lib/savvy_openrouter/connection_api_call_recording_transport.rb
Defined Under Namespace
Modules: ApiCallRecording, ApiCallRecordingTransport, Instrumentation
Constant Summary collapse
- DEFAULT_SUCCESS =
[200, 201, 202, 204].freeze
Instance Attribute Summary collapse
-
#api_call_logger ⇒ Object
readonly
Returns the value of attribute api_call_logger.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #delete(path, params: nil, success: DEFAULT_SUCCESS) ⇒ Object
- #flush_deferred_chat_log! ⇒ Object
- #flush_deferred_responses_log! ⇒ 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
-
#record_manual_api_call(attrs) ⇒ Object
Record a logical failure after HTTP success (e.g. invalid structured JSON).
- #stream_get(path, params: nil, &block) ⇒ Object
- #stream_post(path, body) ⇒ Object
- #with_call_context(context = {}) ⇒ Object
Constructor Details
#initialize(config) ⇒ Connection
Returns a new instance of Connection.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/savvy_openrouter/connection.rb', line 22 def initialize(config) @config = config @api_call_logger = ApiCallLogger.new(config.api_call_log) @call_context_stack = [] @pending_deferred_chat_log = nil @pending_deferred_responses_log = nil 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
#api_call_logger ⇒ Object (readonly)
Returns the value of attribute api_call_logger.
20 21 22 |
# File 'lib/savvy_openrouter/connection.rb', line 20 def api_call_logger @api_call_logger end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
20 21 22 |
# File 'lib/savvy_openrouter/connection.rb', line 20 def config @config end |
Instance Method Details
#delete(path, params: nil, success: DEFAULT_SUCCESS) ⇒ Object
44 45 46 |
# File 'lib/savvy_openrouter/connection.rb', line 44 def delete(path, params: nil, success: DEFAULT_SUCCESS) timed_json(:delete, path, params: params, success: success) end |
#flush_deferred_chat_log! ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/savvy_openrouter/connection.rb', line 180 def flush_deferred_chat_log! attrs = @pending_deferred_chat_log @pending_deferred_chat_log = nil return if attrs.nil? @api_call_logger.record(attrs) end |
#flush_deferred_responses_log! ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/savvy_openrouter/connection.rb', line 188 def flush_deferred_responses_log! attrs = @pending_deferred_responses_log @pending_deferred_responses_log = nil return if attrs.nil? @api_call_logger.record(attrs) end |
#get(path, params: nil, success: DEFAULT_SUCCESS) ⇒ Object
40 41 42 |
# File 'lib/savvy_openrouter/connection.rb', line 40 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
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 85 86 87 88 89 90 91 |
# File 'lib/savvy_openrouter/connection.rb', line 60 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
52 53 54 |
# File 'lib/savvy_openrouter/connection.rb', line 52 def patch(path, body:, success: DEFAULT_SUCCESS) timed_json(:patch, path, body: body, success: success) end |
#post(path, body:, success: DEFAULT_SUCCESS) ⇒ Object
48 49 50 |
# File 'lib/savvy_openrouter/connection.rb', line 48 def post(path, body:, success: DEFAULT_SUCCESS) timed_json(:post, path, body: body, success: success) end |
#post_raw(path, body:, success: [200]) ⇒ Object
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 119 120 121 122 123 124 125 |
# File 'lib/savvy_openrouter/connection.rb', line 93 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
56 57 58 |
# File 'lib/savvy_openrouter/connection.rb', line 56 def put(path, body:, success: DEFAULT_SUCCESS) timed_json(:put, path, body: body, success: success) end |
#record_manual_api_call(attrs) ⇒ Object
Record a logical failure after HTTP success (e.g. invalid structured JSON). Merges context with attrs.
174 175 176 177 178 |
# File 'lib/savvy_openrouter/connection.rb', line 174 def record_manual_api_call(attrs) ctx = stringify_body(@call_context_stack.last || {}) merged = ctx.merge(Configuration.stringify_keys_static(attrs)) @api_call_logger.record(merged) end |
#stream_get(path, params: nil, &block) ⇒ Object
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 156 157 158 159 160 161 162 |
# File 'lib/savvy_openrouter/connection.rb', line 127 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
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/savvy_openrouter/connection.rb', line 196 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 |
#with_call_context(context = {}) ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/savvy_openrouter/connection.rb', line 164 def with_call_context(context = {}) parent = @call_context_stack.last || {} merged = parent.merge(stringify_body(context.is_a?(Hash) ? context : {})) @call_context_stack.push(merged) yield ensure @call_context_stack.pop end |