# frozen_string_literal: true
# ----------------------------------------------------------------------------
#
# Copyright (c) 2018-2020 Aspose Pty Ltd. All rights reserved.
#
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
# ----------------------------------------------------------------------------
module AsposeEmailCloud
# API client invoker. Processes all requests and obtains actual token if necessary.
class ApiInvoker
attr_reader :api_client
def initialize(api_client)
@api_client = api_client
end
def make_request(http_request, method, return_type)
call_api(http_request, method, return_type)
rescue ApiError => e
if e.code.equal? 401
request_token
return call_api(http_request, method, return_type)
end
raise
end
private
def call_api(http_request, method, return_type)
response = @api_client.call_api(method,
http_request.resource_path,
header_params: http_request.header_params,
query_params: http_request.query_params,
form_params: http_request.form_params,
body: http_request.body,
auth_names: http_request.auth_names,
return_type: return_type)
response[0]
end
def request_token
config = @api_client.config
header_params =
{
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded'
}
base_path = @api_client.config.base_path
@api_client.config.base_path = ''
data = @api_client.call_api(:POST, '/connect/token',
header_params: header_params,
form_params: request_token_form_params(config),
return_type: 'Object',
host: @api_client.config.auth_url)
@api_client.config.base_path = base_path
@api_client.config.access_token = data[0]['access_token'.to_sym]
end
def request_token_form_params(config)
{
'grant_type': 'client_credentials',
'client_id': config.client_credentials[:client_id],
'client_secret': config.client_credentials[:client_secret]
}
end
end
end