class Adyen::REST::Client
The Client
class acts as a client to Adyen's REST
webservice.
@!attribute environment [r]
The adyen environment to interact with. Either <tt>'live'</tt> or <tt>'test'</tt>. @return [String]
Constants
- ENDPOINT
- ENDPOINT_MERCHANT_SPECIFIC
Attributes
Public Class Methods
@param environment [String] The Adyen
environment to interface with. Either
<tt>'live'</tt> or <tt>'test'</tt>.
@param username [String] The webservice username, e.g. ws@Company.Account
@param password [String] The password associated with the username @param merchant [String]
# File lib/adyen/rest/client.rb 30 def initialize(environment, username, password, merchant = nil) 31 @environment, @username, @password, @merchant = environment, username, password, merchant 32 end
Public Instance Methods
Closes the client.
-
This will terminate the HTTP connection.
-
After calling this method, the behavior of any further method calls against this client instance is undefined.
@return [void]
# File lib/adyen/rest/client.rb 41 def close 42 @http.finish if @http && @http.started? 43 @http = nil 44 end
Executes an API
request, and returns a reponse of the given type.
@param request [Adyen::REST::Request] The API
request to execute.
<tt>validate!</tt> will be called on the this object before the request is made.
@param response_type [Class] The response type to use. Use either
<tt>Adyen::REST::Response</tt> or a subclass.
@return [Adyen::REST::Response] A response instance of the provided type @see execute_http_request
The execute_http_request
takes care
of executing the underlying HTTP request.
# File lib/adyen/rest/client.rb 83 def execute_request(request) 84 request.validate! 85 http_response = execute_http_request(request) 86 request.build_response(http_response) 87 end
The underlying Net::HTTP
instance that is used to execute HTTP request against the API
.
You can use this to set options on the Net::HTTP instance, like read_timeout
. Many of these options will only work if you set them before the HTTP connection is opened, i.e. before doing the first API
call.
@return [Net::HTTP] The underlying Net::HTTP instance the client uses to perform HTTP request.
# File lib/adyen/rest/client.rb 67 def http 68 @http ||= Net::HTTP.new(endpoint.host, endpoint.port).tap do |http| 69 http.use_ssl = endpoint.scheme == 'https' 70 end 71 end
Runs a client session against the Adyen
REST
service for the duration of the block, and closes the connection afterwards.
@yield The provided block will be called in which you can interact with the API
using
the provided client. The client will be closed after the block returns.
@return [void]
# File lib/adyen/rest/client.rb 52 def session 53 yield(self) 54 ensure 55 close 56 end
Protected Instance Methods
The endpoint URI for this client. @return [URI] The endpoint to use for the environment.
# File lib/adyen/rest/client.rb 116 def endpoint 117 @endpoint ||= if merchant && environment.to_sym == :live 118 URI(ENDPOINT_MERCHANT_SPECIFIC % [merchant]) 119 else 120 URI(ENDPOINT % [environment]) 121 end 122 end
Executes a HTTP request against Adyen's REST
webservice. @param request [Adyen::REST::Request] The request to execute. @return [Net::HTTPResponse] The response from the server. @raise [Adyen::REST::Error] if the HTTP response code was not 200. @see http
Use the http
method to set options on the underlying
<tt>Net::HTTP</tt> object, like timeouts.
# File lib/adyen/rest/client.rb 97 def execute_http_request(request) 98 http_request = Net::HTTP::Post.new(endpoint.path) 99 http_request.basic_auth(@username, @password) 100 http_request.set_form_data(request.form_data) 101 102 case response = http.request(http_request) 103 when Net::HTTPOK 104 return response 105 when Net::HTTPInternalServerError 106 raise Adyen::REST::ResponseError.new(response.body) 107 when Net::HTTPUnauthorized 108 raise Adyen::REST::Error.new("Webservice credentials are incorrect") 109 else 110 raise Adyen::REST::Error.new("Unexpected HTTP response: #{response.code}") 111 end 112 end