class Zuora::Rest::Client

Attributes

connection[R]

Public Class Methods

new(username, password, sandbox = false) click to toggle source

Creates a connection instance. Makes an initial HTTP request to fetch session token. Subsequent requests made with .get, .post, and .put contain the authenticated session id in their headers. @param [String] username @param [String] password @param [Boolean] sandbox @return [Zuora::Client] with .connection, .put, .post

# File lib/zuora/rest/client.rb, line 21
def initialize(username, password, sandbox = false)
  base_url = api_url sandbox
  conn = connection base_url

  response = conn.post do |req|
    set_auth_request_headers! req, username, password
  end

  case response.status
  when 200
    @auth_cookie = response.headers['set-cookie'].split(' ')[0]
    @connection = conn
  when 429
    sleep(Zuora::RETRY_WAITING_PERIOD)
    return initialize(username, password, sandbox)
  else
    raise Zuora::Rest::ConnectionError, response.body['reasons']
  end
end

Private Instance Methods

api_url(sandbox) click to toggle source

@param [Boolean] sandbox - Use the sandbox url? @return [String] url

# File lib/zuora/rest/client.rb, line 145
def api_url(sandbox)
  sandbox ? Zuora::Rest::SANDBOX_URL : Zuora::Rest::API_URL
end
fail_or_response(response) click to toggle source

@param [Faraday::Response] response @throw [ErrorResponse] if unsuccessful @return [Faraday::Response]

# File lib/zuora/rest/client.rb, line 92
def fail_or_response(response)
  if response.status != 200
    raise(ErrorResponse.new("HTTP Status #{response.status}", response))
  elsif !response.body['success']
    errors = 'Not successful.'

    if response.body['reasons']
      reasons = response.body['reasons'].map do |reason|
        "Error #{reason['code']}: #{reason['message']}"
      end
      errors += ' ' + reasons.join(', ')
    end

    raise(ErrorResponse.new(errors, response))
  end
  response
end
handle_rate_limiting(method, url, params = nil) click to toggle source

@param [String] method @param [String] url @param [Hash] params

# File lib/zuora/rest/client.rb, line 80
def handle_rate_limiting(method, url, params = nil)
  sleep(Zuora::RETRY_WAITING_PERIOD)
  if params.present?
    send(method, url, params)
  else
    send(method, url)
  end
end
set_auth_request_headers!(req, username, password) click to toggle source

@param [Faraday::Request] req - Faraday::Request builder @param [String] username @param [String] password

# File lib/zuora/rest/client.rb, line 113
def set_auth_request_headers!(req, username, password)
  req.url '/rest/v1/connections'
  req.headers['apiAccessKeyId'] = username
  req.headers['apiSecretAccessKey'] = password
  req.headers['Content-Type'] = 'application/json'
end
set_request_headers!(request, url) click to toggle source

@param [Faraday::Request] request - Faraday Request builder @param [String] url - Relative URL for HTTP request

# File lib/zuora/rest/client.rb, line 122
def set_request_headers!(request, url)
  request.url url
  request.headers['Content-Type'] = 'application/json'
  request.headers['Cookie'] = @auth_cookie

  if ENV['ZUORA_API_VERSION'].present?
    request.headers['zuora-version'] = ENV['ZUORA_API_VERSION']
  end
end