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
-
#auth_connection(settings = FmRest.default_connection_settings) ⇒ Faraday
Builds a Faraday connection to use for DAPI basic auth login.
-
#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.
-
#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.
Instance Method Details
#auth_connection(settings = FmRest.default_connection_settings) ⇒ Faraday
Builds a Faraday connection to use for DAPI basic auth login
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
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, = 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 = ( || {}).dup [:ssl] = settings.ssl if settings.ssl? [:proxy] = settings.proxy if settings.proxy? database = V1.url_encode(settings.database!) Faraday.new( "#{scheme}://#{host}#{DATABASES_PATH}/#{database}/".freeze, , &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
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 |