Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faraday Proxy Config #64

Closed
sirwolfgang opened this issue Feb 21, 2022 · 5 comments
Closed

Faraday Proxy Config #64

sirwolfgang opened this issue Feb 21, 2022 · 5 comments

Comments

@sirwolfgang
Copy link

Is there a way to build this and expose the proxy settings?

This is a requirement for being able to have an allowlistable IP address in a cloud environment.

@sirwolfgang
Copy link
Author

There is some work needed to get this over the line, but I think this is more or less what needs to happen. OpenAPITools/openapi-generator#11692

@sirwolfgang
Copy link
Author

I was able to get Faraday's proxy settings to work with this monkey patch.

require 'mx-platform-ruby'

module MxPlatformRuby
  class Configuration
    attr_accessor :proxy
  end
end

module MxPlatformRuby
  class ApiClient
    # Call an API with given options.
    #
    # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
    #   the data deserialized from response body (could be nil), response status code and response headers.
    def call_api(http_method, path, opts = {})
      ssl_options = {
        :ca_file => @config.ssl_ca_file,
        :verify => @config.ssl_verify,
        :verify_mode => @config.ssl_verify_mode,
        :client_cert => @config.ssl_client_cert,
        :client_key => @config.ssl_client_key
      }

      connection = Faraday.new(:url => config.base_url, :ssl => ssl_options) do |conn|

        ##########################################################################
        conn.proxy = @config.proxy if @config.proxy
        ##########################################################################

        conn.request(:basic_auth, config.username, config.password)
        @config.configure_middleware(conn)
        if opts[:header_params]["Content-Type"] == "multipart/form-data"
          conn.request :multipart
          conn.request :url_encoded
        end
        conn.adapter(Faraday.default_adapter)
      end

      begin
        response = connection.public_send(http_method.to_sym.downcase) do |req|
          build_request(http_method, path, req, opts)
        end

        if @config.debugging
          @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
        end

        unless response.success?
          if response.status == 0
            # Errors from libcurl will ebe made visible here
            fail ApiError.new(:code => 0,
                              :message => response.return_message)
          else
            fail ApiError.new(:code => response.status,
                              :response_headers => response.headers,
                              :response_body => response.body),
                 response.reason_phrase
          end
        end
      rescue Faraday::TimeoutError
        fail ApiError.new('Connection timed out')
      end

      if opts[:return_type]
        data = deserialize(response, opts[:return_type])
      else
        data = nil
      end
      return data, response.status, response.headers
    end

    # Builds the HTTP request
    #
    # @param [String] http_method HTTP method/verb (e.g. POST)
    # @param [String] path URL path (e.g. /account/new)
    # @option opts [Hash] :header_params Header parameters
    # @option opts [Hash] :query_params Query parameters
    # @option opts [Hash] :form_params Query parameters
    # @option opts [Object] :body HTTP body (JSON/XML)
    # @return [Typhoeus::Request] A Typhoeus Request
    def build_request(http_method, path, request, opts = {})
      url = build_request_url(path, opts)
      http_method = http_method.to_sym.downcase

      header_params = @default_headers.merge(opts[:header_params] || {})
      query_params = opts[:query_params] || {}
      form_params = opts[:form_params] || {}

      update_params_for_auth! header_params, query_params, opts[:auth_names]

      # req_opts = {
      #   :params_encoding => @config.params_encoding,
      #   :timeout => @config.timeout,
      #   :verbose => @config.debugging
      # }

      if [:post, :patch, :put, :delete].include?(http_method)
        req_body = build_request_body(header_params, form_params, opts[:body])
        if @config.debugging
          @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
        end
      end
      request.headers = header_params
      request.body = req_body

      ##########################################################################
      # request.options = OpenStruct.new(req_opts)

      request.options.params_encoding = @config.params_encoding if @config.params_encoding
      request.options.timeout         = @config.timeout         if @config.timeout
      request.options.verbose         = @config.debugging       if @config.debugging

      ##########################################################################

      request.url url
      request.params = query_params
      download_file(request) if opts[:return_type] == 'File'
      request
    end
  end
end

@bradon-maughan
Copy link

@sirwolfgang Thanks for reaching out and for the heads up on the proxy settings - you are correct, they cannot be configured right now. We appreciate you putting an MR up in the openapi-generatorrepo. When those changes are included in a new version, we will make sure to regenerate our ruby library!

Cheers!

@sirwolfgang
Copy link
Author

sirwolfgang commented Feb 26, 2022

@bradon-maughan Looks like this got merged in to main, and is slated for (or maybe sooner) 6.0.0 OpenAPITools/openapi-generator#11692 (comment)

@rubendinho
Copy link

Hi @mcoats13 - saw that #86 was released - does that close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants