Class: LightGptProxy::Endpoint
- Inherits:
-
Object
- Object
- LightGptProxy::Endpoint
- Defined in:
- lib/light_gpt_proxy/endpoint.rb
Constant Summary collapse
- ApplyDefaultsError =
Class.new(StandardError)
- RequestError =
Class.new(StandardError) do |_klass| def initialize(response) @response = response @status = response&.status super("Request failed with status #{response.status}: #{response.reason_phrase} => #{response.body}") end attr_reader :status, :response end
Instance Attribute Summary collapse
-
#body_schema ⇒ Object
readonly
Returns the value of attribute body_schema.
-
#header_options ⇒ Object
readonly
Returns the value of attribute header_options.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #apply_defaults(payload) ⇒ Object
- #execute_request(payload = {}) ⇒ Object
-
#initialize(name:, path:, host:, key:, method: 'get', body_schema: nil, header_options: {}) ⇒ Endpoint
constructor
A new instance of Endpoint.
- #validate_and_apply_defaults(payload) ⇒ Object
Constructor Details
#initialize(name:, path:, host:, key:, method: 'get', body_schema: nil, header_options: {}) ⇒ Endpoint
Returns a new instance of Endpoint.
22 23 24 25 26 27 28 29 30 |
# File 'lib/light_gpt_proxy/endpoint.rb', line 22 def initialize(name:, path:, host:, key:, method: 'get', body_schema: nil, header_options: {}) @name = name @host = host @path = path @method = method @body_schema = body_schema @header_options = @key = key end |
Instance Attribute Details
#body_schema ⇒ Object (readonly)
Returns the value of attribute body_schema.
20 21 22 |
# File 'lib/light_gpt_proxy/endpoint.rb', line 20 def body_schema @body_schema end |
#header_options ⇒ Object (readonly)
Returns the value of attribute header_options.
20 21 22 |
# File 'lib/light_gpt_proxy/endpoint.rb', line 20 def @header_options end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
20 21 22 |
# File 'lib/light_gpt_proxy/endpoint.rb', line 20 def host @host end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
20 21 22 |
# File 'lib/light_gpt_proxy/endpoint.rb', line 20 def key @key end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
20 21 22 |
# File 'lib/light_gpt_proxy/endpoint.rb', line 20 def method @method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
20 21 22 |
# File 'lib/light_gpt_proxy/endpoint.rb', line 20 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
20 21 22 |
# File 'lib/light_gpt_proxy/endpoint.rb', line 20 def path @path end |
Instance Method Details
#apply_defaults(payload) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/light_gpt_proxy/endpoint.rb', line 41 def apply_defaults(payload) return payload unless body_schema defaults.perform(payload) ensure_system_presence_if_needed(payload) if name == 'completions' rescue StandardError => e raise ApplyDefaultsError, e. end |
#execute_request(payload = {}) ⇒ Object
50 51 52 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 85 86 |
# File 'lib/light_gpt_proxy/endpoint.rb', line 50 def execute_request(payload = {}) connection = Faraday.new(url: "#{host}/#{path}") do |conn| conn.request :json conn.response :json, content_type: /\bjson$/ conn.adapter Faraday.default_adapter end response = case method.upcase when 'POST' connection.post do |req| req.headers = complete_headers req.body = payload end when 'PUT' connection.put do |req| req.headers = complete_headers req.body = payload end when 'PATCH' connection.patch do |req| req.headers = complete_headers req.body = payload end when 'DELETE' connection.delete do |req| req.headers = complete_headers end when 'GET' connection.get do |req| req.headers = complete_headers end else raise ArgumentError, "Unsupported HTTP method: #{method}" end handle_response(response) end |
#validate_and_apply_defaults(payload) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/light_gpt_proxy/endpoint.rb', line 32 def validate_and_apply_defaults(payload) return payload unless body_schema payload = JSON.parse(payload) if payload.is_a?(String) apply_defaults(payload) validator.validate!(payload) payload end |