class Yoti::Request
Manage the API's HTTPS requests
Attributes
@return [String] the base URL
@deprecated will be removed in 2.0.0 - token is now provided with the endpoint @return [String] the URL token received from Yoti
Connect
@return [String] the API endpoint for the request
@return [String] the HTTP method used for the request The allowed methods are: GET, DELETE, POST, PUT, PATCH
@return [#to_json,String] the body sent with the request
@return [Hash] query params to add to the request
Public Class Methods
@return [RequestBuilder]
# File lib/yoti/http/request.rb, line 34 def self.builder RequestBuilder.new end
# File lib/yoti/http/request.rb, line 27 def initialize @headers = {} end
Public Instance Methods
Adds a HTTP header to the request
@param [String] header @param [String] value
# File lib/yoti/http/request.rb, line 44 def add_header(header, value) @headers[header] = value end
@return [String] the base URL
# File lib/yoti/http/request.rb, line 78 def base_url @base_url ||= Yoti.configuration.api_endpoint end
Makes a HTTP request and returns the body after signing the headers
@return [String]
# File lib/yoti/http/request.rb, line 71 def body execute.body end
Makes a HTTP request after signing the headers
@return [HTTPResponse]
# File lib/yoti/http/request.rb, line 53 def execute raise RequestError, 'The request requires a HTTP method.' unless @http_method http_res = Net::HTTP.start(uri.hostname, Yoti.configuration.api_port, use_ssl: https_uri?) do |http| signed_request = SignedRequest.new(unsigned_request, path, @payload).sign http.request(signed_request) end raise RequestError.new("Unsuccessful Yoti API call: #{http_res.message}", http_res) unless response_is_success(http_res) http_res end
Private Instance Methods
Adds payload to provided HTTP request
@param [Net::HTTPRequest] http_req
# File lib/yoti/http/request.rb, line 98 def add_payload(http_req) return if @payload.to_s.empty? if @payload.is_a?(String) http_req.body = @payload elsif @payload.respond_to?(:to_json) http_req.body = @payload.to_json end end
# File lib/yoti/http/request.rb, line 164 def https_uri? uri.scheme == 'https' end
@return [String] the path with query string
# File lib/yoti/http/request.rb, line 147 def path @path ||= begin "/#{@endpoint}/#{token}".chomp('/') + "?#{query_string}" end end
Builds query string including nonce and timestamp
@return [String]
# File lib/yoti/http/request.rb, line 173 def query_string params = { nonce: SecureRandom.uuid, timestamp: Time.now.to_i } if @query_params.nil? # @deprecated this default will be removed in 2.0.0 # Append appId when no custom query params are provided. params.merge!(appId: Yoti.configuration.client_sdk_id) else Validation.assert_is_a(Hash, @query_params, 'query_params') params.merge!(@query_params) end params.map do |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" end.join('&') end
@param [Net::HTTPResponse] http_res
@return [Boolean]
# File lib/yoti/http/request.rb, line 89 def response_is_success(http_res) http_res.code.to_i >= 200 && http_res.code.to_i < 300 end
@deprecated will be removed in 2.0.0 - token is now provided with the endpoint
@return [String] the decrypted connect token
# File lib/yoti/http/request.rb, line 158 def token return '' unless @encrypted_connect_token Yoti::SSL.decrypt_token(@encrypted_connect_token) end
@return [Net::HTTPRequest] the unsigned HTTP request
# File lib/yoti/http/request.rb, line 111 def unsigned_request case @http_method when 'GET' http_req = Net::HTTP::Get.new(uri) when 'DELETE' http_req = Net::HTTP::Delete.new(uri) when 'POST' http_req = Net::HTTP::Post.new(uri) add_payload(http_req) when 'PUT' http_req = Net::HTTP::Put.new(uri) add_payload(http_req) when 'PATCH' http_req = Net::HTTP::Patch.new(uri) add_payload(http_req) else raise RequestError, "Request method not allowed: #{@http_method}" end @headers.each do |header, value| http_req[header] = value end http_req end
@return [URI] the full request URI
# File lib/yoti/http/request.rb, line 140 def uri @uri ||= URI(base_url + path) end