Module: FmRest::V1::Connection

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

Constant Summary collapse

BASE_PATH =
"/fmi/data/v1"
DATABASES_PATH =
"#{BASE_PATH}/databases"
DEFAULT_HEADERS =
{ "User-Agent" => "fmrest-ruby/#{::FmRest::VERSION} Faraday/#{::Faraday::VERSION}" }.freeze
AUTH_CONNECTION_HEADERS =
DEFAULT_HEADERS.merge("Content-Type" => "application/json").freeze
CLARIS_ID_HTTP_AUTH_TYPE =
"FMID"
FILEMAKER_CLOUD_HOST_MATCHER =
/\.filemaker-cloud\.com\Z/.freeze

Instance Method Summary collapse

Instance Method Details

#auth_connection(settings = FmRest.default_connection_settings) ⇒ Faraday

Builds a Faraday connection to use for DAPI basic auth login

Options Hash (settings):

  • :host (String)

    The hostname for the FM server

  • :database (String)

    The FM database name

  • :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

  • :ssl (String)

    SSL settings to forward to the Faraday connection

  • :proxy (String)

    Proxy options to forward to the Faraday connection

Returns:

  • (Faraday)

    The new Faraday connection



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
# File 'lib/fmrest/v1/connection.rb', line 59

def auth_connection(settings = FmRest.default_connection_settings)
  settings = ConnectionSettings.wrap(settings)

  if is_cloud_host = cloud_host?(settings)
    FmRest.require_cloud_support
  end

  base_connection(settings, headers: AUTH_CONNECTION_HEADERS) do |conn|
    conn.use Cloud::AuthErrorHandler, settings if is_cloud_host
    conn.use RaiseErrors

    if settings.fmid_token?
      conn.request :authorization, CLARIS_ID_HTTP_AUTH_TYPE, -> { settings.fmid_token }
    elsif is_cloud_host
      conn.request :authorization, CLARIS_ID_HTTP_AUTH_TYPE, -> { Cloud::ClarisIdTokenManager.new(settings).fetch_token }
    else
      conn.request :authorization, :basic, settings.username!, settings.password!
    end

    if settings.log
      conn.response :logger, FmRest.logger, bodies: true, headers: true, log_level: settings.log_level
    end

    conn.response :json, content_type: /\bjson$/
    conn.adapter Faraday.default_adapter
  end
end

#base_connection(settings = FmRest.default_connection_settings, faraday_options = nil, &block) ⇒ Faraday

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

Parameters:

  • faraday_options (Hash) (defaults to: nil)

    additional options for Faraday object

  • settings (Hash) (defaults to: FmRest.default_connection_settings)

    a customizable set of options

Options Hash (settings):

  • :host (String)

    The hostname for the FM server

  • :database (String)

    The FM database name

  • :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

  • :ssl (String)

    SSL settings to forward to the Faraday connection

  • :proxy (String)

    Proxy options to forward to the Faraday connection

Returns:

  • (Faraday)

    The new Faraday connection



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fmrest/v1/connection.rb', line 102

def base_connection(settings = FmRest.default_connection_settings, faraday_options = nil, &block)
  settings = ConnectionSettings.wrap(settings)

  host = settings.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 || {}).dup
  faraday_options[:ssl] = settings.ssl if settings.ssl?
  faraday_options[:proxy] = settings.proxy if settings.proxy?

  database = V1.url_encode(settings.database!)

  Faraday.new(
    "#{scheme}://#{host}#{DATABASES_PATH}/#{database}/".freeze,
    faraday_options,
    &block
  )
end

#build_connection(settings = 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 (settings):

  • :host (String)

    The hostname for the FM server

  • :database (String)

    The FM database name

  • :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

  • :ssl (String)

    SSL settings to forward to the Faraday connection

  • :proxy (String)

    Proxy options to forward to the Faraday connection

Returns:

  • (Faraday)

    The new Faraday connection



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
49
50
51
52
53
# File 'lib/fmrest/v1/connection.rb', line 24

def build_connection(settings = FmRest.default_connection_settings, &block)
  settings = ConnectionSettings.wrap(settings)

  base_connection(settings, headers: DEFAULT_HEADERS) do |conn|
    conn.use RaiseErrors
    conn.use TokenSession, settings

    # 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, settings
    else
      conn.use TypeCoercer, settings
      conn.response :json, content_type: /\bjson$/
    end

    if settings.log
      conn.response :logger, FmRest.logger, bodies: true, headers: true, log_level: settings.log_level
    end

    conn.adapter Faraday.default_adapter
  end
end