Module: FmRest::V1::Connection

Included in:
FmRest::V1
Defined in:
lib/fmrest/v1/connection.rb

Constant Summary collapse

BASE_PATH =
"/fmi/data/v1/databases"

Instance Method Summary collapse

Instance Method Details

#base_connection(options = FmRest.default_connection_settings, &block) ⇒ Faraday

Builds a base Faraday connection with base URL constructed from connection options and passes it the given block

Options Hash (options):

  • :host (String)

    The hostname for the FM server

  • :database (String)

    The FM database name

  • :ssl (String)

    SSL options to forward to the Faraday connection

  • :proxy (String)

    Proxy options to forward to the Faraday connection



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fmrest/v1/connection.rb', line 60

def base_connection(options = FmRest.default_connection_settings, &block)
  host = options.fetch(:host)

  # Default to HTTPS
  scheme = "https"

  if host.match(/\Ahttps?:\/\//)
    uri = URI(host)
    host = uri.hostname
    host += ":#{uri.port}" if uri.port != uri.default_port
    scheme = uri.scheme
  end

  faraday_options = {}
  faraday_options[:ssl] = options[:ssl] if options.key?(:ssl)
  faraday_options[:proxy] = options[:proxy] if options.key?(:proxy)

  Faraday.new(
    "#{scheme}://#{host}#{BASE_PATH}/#{URI.escape(options.fetch(:database))}/".freeze,
    faraday_options,
    &block
  )
end

#build_connection(options = FmRest.default_connection_settings, &block) ⇒ Faraday

Builds a complete DAPI Faraday connection with middleware already configured to handle authentication, JSON parsing, logging and DAPI error handling. A block can be optionally given for additional middleware configuration

Options Hash (options):

  • :username (String)

    The username for DAPI authentication

  • :account_name (String)

    Alias of :username for compatibility with Rfm gem

  • :password (String)

    The password for DAPI authentication

  • :host (String)

    The hostname for the FM server

  • :database (String)

    The FM database name

  • :ssl (String)

    SSL options to forward to the Faraday connection

  • :proxy (String)

    Proxy options to forward to the Faraday connection



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fmrest/v1/connection.rb', line 21

def build_connection(options = FmRest.default_connection_settings, &block)
  base_connection(options) do |conn|
    conn.use RaiseErrors
    conn.use TokenSession, options

    # The EncodeJson and Multipart middlewares only encode the request
    # when the content type matches, so we can have them both here and
    # still play nice with each other, we just need to set the content
    # type to multipart/form-data when we want to submit a container
    # field
    conn.request :multipart
    conn.request :json

    # Allow overriding the default response middleware
    if block_given?
      yield conn, options
    else
      conn.use TypeCoercer, options
      conn.response :json
    end

    if options[:log]
      conn.response :logger, nil, bodies: true, headers: true
    end

    conn.adapter Faraday.default_adapter
  end
end