Class: Dolibarr::Api::Configuration
- Inherits:
-
Object
- Object
- Dolibarr::Api::Configuration
- Defined in:
- lib/dolibarr-api/configuration.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#debugging ⇒ Object
Returns the value of attribute debugging.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#query_array_encoding ⇒ Object
Returns the value of attribute query_array_encoding.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#apply_auth(headers, query, auth_names) ⇒ Object
Applies authentication credentials to a single request's headers and query params.
- #configure_faraday(conn) ⇒ Object
- #encode_array(values) ⇒ Object
-
#initialize(base_url: nil, **options) {|_self| ... } ⇒ Configuration
constructor
A new instance of Configuration.
-
#use(middleware, *args, &block) ⇒ Object
Register a Faraday middleware to run on every request, after the built-in request middleware (so it sees the serialized body and final URL) and just before the adapter.
Constructor Details
#initialize(base_url: nil, **options) {|_self| ... } ⇒ Configuration
Returns a new instance of Configuration.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/dolibarr-api/configuration.rb', line 7 def initialize(base_url: nil, **) @base_url = base_url || 'http://dolibarr.thetuxhouse.org/api/index.php' @timeout = 60 @query_array_encoding = :repeat @debugging = false @middlewares = [] .each do |k, v| raise ArgumentError, "unknown configuration option: #{k}" unless respond_to?("#{k}=") public_send("#{k}=", v) end yield self if block_given? end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
5 6 7 |
# File 'lib/dolibarr-api/configuration.rb', line 5 def api_key @api_key end |
#base_url ⇒ Object
Returns the value of attribute base_url.
5 6 7 |
# File 'lib/dolibarr-api/configuration.rb', line 5 def base_url @base_url end |
#debugging ⇒ Object
Returns the value of attribute debugging.
5 6 7 |
# File 'lib/dolibarr-api/configuration.rb', line 5 def debugging @debugging end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/dolibarr-api/configuration.rb', line 5 def logger @logger end |
#query_array_encoding ⇒ Object
Returns the value of attribute query_array_encoding.
5 6 7 |
# File 'lib/dolibarr-api/configuration.rb', line 5 def query_array_encoding @query_array_encoding end |
#timeout ⇒ Object
Returns the value of attribute timeout.
5 6 7 |
# File 'lib/dolibarr-api/configuration.rb', line 5 def timeout @timeout end |
Instance Method Details
#apply_auth(headers, query, auth_names) ⇒ Object
Applies authentication credentials to a single request's headers and query
params. Called by Connection#call on EVERY request (not baked into the
persistent Faraday connection at build time) so credential rotation --
token refresh, a new api_key -- takes effect on the very next call.
auth_names are the security schemes the current operation actually declares (empty
for security: [] public endpoints); only those are applied, so credentials never
leak to public endpoints nor stack conflicting Authorization headers.
rubocop:disable Lint/UnusedMethodArgument -- headers/query/auth_names form the fixed
per-request auth interface (Connection#call); a given generated client may exercise
only some of them (e.g. no query-based key, or no auth method at all).
46 47 48 49 |
# File 'lib/dolibarr-api/configuration.rb', line 46 def apply_auth(headers, query, auth_names) query['api_key'] = api_key if api_key && auth_names.include?('api_key') nil end |
#configure_faraday(conn) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dolibarr-api/configuration.rb', line 21 def configure_faraday(conn) # Order matters: :multipart / :url_encoded must run before :json so form calls # (Connection#call `form:`) get encoded first -- Connection#call pins the # Content-Type to "application/json" up front on non-form calls, so these two # middlewares' own (weaker) content-type guards correctly no-op on JSON bodies. conn.request :multipart conn.request :url_encoded conn.request :json conn.response :json, content_type: /\bjson$/ conn..timeout = timeout conn.response :logger, logger if logger && debugging @middlewares.each { |mw, mw_args, mw_block| conn.use(mw, *mw_args, &mw_block) } conn.adapter Faraday.default_adapter end |
#encode_array(values) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/dolibarr-api/configuration.rb', line 60 def encode_array(values) case query_array_encoding when :csv then values.join(',') else values end end |
#use(middleware, *args, &block) ⇒ Object
Register a Faraday middleware to run on every request, after the built-in request middleware (so it sees the serialized body and final URL) and just before the adapter. Use for request signing (OVH, AWS SigV4, ...).
55 56 57 58 |
# File 'lib/dolibarr-api/configuration.rb', line 55 def use(middleware, *args, &block) @middlewares << [middleware, args, block] self end |