lib/zuora/client.rb in zuora-ruby-0.3.0 vs lib/zuora/client.rb in zuora-ruby-0.4.0
- old
+ new
@@ -1,16 +1,10 @@
require 'faraday'
require 'faraday_middleware'
require 'nokogiri'
module Zuora
- # Unable to connect. Check username / password
- SoapConnectionError = Class.new StandardError
-
- # Non-success response
- SoapErrorResponse = Class.new StandardError
-
class Client
attr_accessor :session_token
SOAP_API_URI = '/apps/services/a/74.0'.freeze
SESSION_TOKEN_XPATH =
@@ -38,11 +32,11 @@
username: @username,
password: @password
handle_auth_response auth_response
rescue Object => e
- raise SoapConnectionError, e
+ raise Zuora::Errors::SoapConnectionError, e
end
# Fire a request
# @param [Xml] body - an object responding to .xml
# @return [Zuora::Response]
@@ -53,11 +47,15 @@
request.url SOAP_API_URI
request.headers['Content-Type'] = 'text/xml'
request.body = body.to_xml
end
- Zuora::Response.new(raw_response)
+ response = Zuora::Response.new(raw_response)
+
+ response.handle_errors(response.to_h)
+
+ response
end
# The primary interface via which users should make SOAP requests.
# client.call :create, object_name: :BillRun, data: {...}
# client.call :subscribe, account: {...}, sold_to_contact: {...}
@@ -71,12 +69,12 @@
end
private
# Generate envelope for request
- # @param [Symbol] call name - one of the supported calls (see #call)
- # @param [Callable] builder_modifier - function taking a builder
+ # @param [Symbol] call_name - one of the supported calls (see #call)
+ # @param [Callable] xml_builder_modifier - function taking a builder
# @return [Nokogiri::XML::Builder]
def envelope_for(call_name, xml_builder_modifier)
if call_name == :login
Zuora::Utils::Envelope.xml(nil, xml_builder_modifier)
else
@@ -87,27 +85,25 @@
end
# Handle auth response, setting session
# @params [Faraday::Response]
# @return [Faraday::Response]
- # @throw [SoapErrorResponse]
+ # @throw [Zuora::Errors::InvalidCredentials]
def handle_auth_response(response)
if response.raw.status == 200
@session_token = extract_session_token response
else
message = 'Unable to connect with provided credentials'
- fail SoapErrorResponse, message
+ fail Zuora::Errors::InvalidCredentials, message
end
response
end
# Extracts session token from response and sets instance variable
# for use in subsequent requests
# @param [Faraday::Response] response - response to auth request
def extract_session_token(response)
- Nokogiri::XML(response.raw.body).xpath(
- SESSION_TOKEN_XPATH, Zuora::NAMESPACES
- ).text
+ response.to_h.envelope.body.login_response.result.session
end
# Initializes a connection using api_url
# @return [Faraday::Connection]
def connection