Class: Zuora::Rest::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zuora/rest/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password, sandbox = false) ⇒ Zuora::Client

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.

Parameters:

  • username (String)
  • password (String)
  • sandbox (Boolean) (defaults to: false)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Method Details

#methodFaraday::Response

Returns A response, with .headers, .status & .body.

Parameters:

  • url (String)
    • URL for HTTP POST / PUT request

  • params (Params)
    • Data to be sent in request body

Returns:

  • (Faraday::Response)

    A response, with .headers, .status & .body



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zuora/rest/client.rb', line 43

%i[get delete].each do |method|
  define_method(method) do |url|
    response = @connection.send(method) do |req|
      set_request_headers! req, url
    end

    # Handle rate limiting
    return handle_rate_limiting(method, url) if response.status == 429

    fail_or_response(response)
  end
end