Module: Xero::Api::Connection

Includes:
OAuth1, OAuth2
Included in:
Xero::Api
Defined in:
lib/xero/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, included, #use_oauth1_middleware?

Class Method Details

.add_authorization_middleware(strategy_name) ⇒ Object



9
10
11
# File 'lib/xero/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/xero/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
# File 'lib/xero/api/connection.rb', line 25

def authorized_multipart_connection(url)
  headers = { 'Content-Type' => 'multipart/form-data' }
  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



35
36
37
38
39
40
41
# File 'lib/xero/api/connection.rb', line 35

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

#parse_response_body(resp) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/xero/api/connection.rb', line 71

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, headers: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/xero/api/connection.rb', line 48

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

#request(method, path:, entity: nil, payload: nil, headers: nil, parse_entity: false) ⇒ Object



43
44
45
46
# File 'lib/xero/api/connection.rb', line 43

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

#response(resp, entity: nil, parse_entity: false) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/xero/api/connection.rb', line 62

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