Module: QboApi::Connection

Includes:
OAuth1, OAuth2
Included in:
QboApi
Defined in:
lib/qbo_api/connection.rb

Defined Under Namespace

Modules: OAuth1, OAuth2

Constant Summary collapse

AUTHORIZATION_MIDDLEWARES =
[]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OAuth2

#add_oauth2_authorization_middleware, #default_attributes, included, #use_oauth2_middleware?

Methods included from OAuth1

#add_oauth1_authorization_middleware, #default_attributes, #disconnect, included, #reconnect, #use_oauth1_middleware?

Class Method Details

.add_authorization_middleware(strategy_name) ⇒ Object



9
10
11
# File 'lib/qbo_api/connection.rb', line 9

def Connection.add_authorization_middleware(strategy_name)
  Connection::AUTHORIZATION_MIDDLEWARES << strategy_name
end

Instance Method Details

#authorized_json_connection(url, headers: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/qbo_api/connection.rb', line 13

def authorized_json_connection(url, headers: nil)
  headers ||= {}
  headers['Accept'] ||= 'application/json' # required "we'll only accept JSON". Can be changed to any `+json` media type.
  headers['Content-Type'] ||= 'application/json;charset=UTF-8' # required when request has a body, else harmless
  build_connection(url, headers: headers) do |conn|
    add_authorization_middleware(conn)
    add_exception_middleware(conn)
    conn.request :url_encoded
    add_connection_adapter(conn)
  end
end

#authorized_multipart_connection(url) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/qbo_api/connection.rb', line 25

def authorized_multipart_connection(url)
  headers = {
    'Content-Type' => 'multipart/form-data',
    'Accept' => 'application/json'
  }
  build_connection(url, headers: headers) do |conn|
    add_authorization_middleware(conn)
    add_exception_middleware(conn)
    conn.request :multipart
    add_connection_adapter(conn)
  end
end

#build_connection(url, headers: nil) ⇒ Object

Examples:

connection = build_connection('https://oauth.platform.intuit.com', headers: { 'Accept' => 'application/json' }) do |conn|
  conn.basic_auth(client_id, client_secret)
  conn.request :url_encoded # application/x-www-form-urlencoded
  conn.use FaradayMiddleware::ParseJson, parser_options: { symbolize_names: true }
  conn.use Faraday::Response::RaiseError
end
raw_response = connection.post {|req|
  req.body = { grant_type: :refresh_token, refresh_token: current_refresh_token }
  req.url '/oauth2/v1/tokens/bearer'
}


49
50
51
52
53
54
55
# File 'lib/qbo_api/connection.rb', line 49

def build_connection(url, headers: nil)
  Faraday.new(url: url) { |conn|
    conn.response :detailed_logger, QboApi.logger, LOG_TAG if QboApi.log
    conn.headers.update(headers) if headers
    yield conn if block_given?
  }
end

#parse_response_body(resp) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/qbo_api/connection.rb', line 84

def parse_response_body(resp)
  body = resp.body
  case resp.headers['Content-Type']
  when /json/ then JSON.parse(body)
  else body
  end
end

#raw_request(method, conn:, path:, payload: nil, params: nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/qbo_api/connection.rb', line 62

def raw_request(method, conn:, path:, payload: nil, params: nil)
  path = finalize_path(path, method: method, params: params)
  conn.public_send(method) do |req|
    case method
    when :get, :delete
      req.url path
    when :post, :put
      req.url path
      req.body = payload.to_json
    else raise ArgumentError, "Unhandled request method '#{method.inspect}'"
    end
  end
end

#request(method, path:, entity: nil, payload: nil, params: nil) ⇒ Object



57
58
59
60
# File 'lib/qbo_api/connection.rb', line 57

def request(method, path:, entity: nil, payload: nil, params: nil)
  raw_response = raw_request(method, conn: connection, path: path, params: params, payload: payload)
  response(raw_response, entity: entity)
end

#response(resp, entity: nil) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/qbo_api/connection.rb', line 76

def response(resp, entity: nil)
  data = parse_response_body(resp)
  entity ? entity_response(data, entity) : data
rescue => e
  QboApi.logger.debug { "#{LOG_TAG} response parsing error: entity=#{entity.inspect} body=#{resp.body.inspect} exception=#{e.inspect}" }
  data
end