lib/openc3/io/json_api_object.rb in openc3-5.7.2 vs lib/openc3/io/json_api_object.rb in openc3-5.8.0
- old
+ new
@@ -12,11 +12,11 @@
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# Modified by OpenC3, Inc.
-# All changes Copyright 2022, OpenC3, Inc.
+# All changes Copyright 2023, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.
@@ -28,11 +28,11 @@
require 'socket'
require 'json'
# require 'drb/acl'
require 'drb/drb'
require 'uri'
-require 'httpclient'
+require 'faraday'
module OpenC3
class JsonApiError < StandardError; end
@@ -99,11 +99,11 @@
end
end
# Disconnects from http server
def disconnect
- @http.reset_all() if @http
+ @http.close if @http
@http = nil
end
# Permanently disconnects from the http server
def shutdown
@@ -113,13 +113,18 @@
private
def connect
begin
- @http = HTTPClient.new
- @http.connect_timeout = @timeout
- @http.receive_timeout = nil # Allow long polling
+ # Per https://github.com/lostisland/faraday/blob/main/lib/faraday/options/env.rb
+ # :timeout - time limit for the entire request (Integer in seconds)
+ # :open_timeout - time limit for just the connection phase (e.g. handshake) (Integer in seconds)
+ # :read_timeout - time limit for the first response byte received from the server (Integer in seconds)
+ # :write_timeout - time limit for the client to send the request to the server (Integer in seconds)
+ @http = Faraday.new(request: { open_timeout: @timeout.to_i, read_timeout: nil }) do |f|
+ f.adapter :net_http # adds the adapter to the connection, defaults to `Faraday.default_adapter`
+ end
rescue => e
raise JsonApiError, e.message
end
end
@@ -214,19 +219,26 @@
# NOTE: This is a helper method and should not be called directly
def _http_request(method:, uri:, kwargs:)
case method
when 'get', :get
- return @http.get(uri, :header => kwargs[:headers], :query => kwargs[:query])
+ return @http.get(uri, kwargs[:query], kwargs[:headers])
when 'post', :post
- return @http.post(uri, :header => kwargs[:headers], :query => kwargs[:query], :body => kwargs[:data])
+ return @http.post(uri) do |req|
+ req.params = kwargs[:query]
+ req.headers = kwargs[:headers]
+ req.body = kwargs[:data]
+ end
when 'put', :put
- return @http.put(uri, :header => kwargs[:headers], :query => kwargs[:query], :body => kwargs[:data])
+ return @http.put(uri) do |req|
+ req.params = kwargs[:query]
+ req.headers = kwargs[:headers]
+ req.body = kwargs[:data]
+ end
when 'delete', :delete
- return @http.delete(uri, :header => kwargs[:headers], :query => kwargs[:query])
+ return @http.delete(uri, kwargs[:query], kwargs[:headers])
else
raise JsonApiError, "no method found: '#{method}'"
end
end
-
- end # class JsonApiObject
+ end
end