Class: Setsuzoku::Service::WebService::ApiStrategies::RestStrategy
- Inherits:
-
Setsuzoku::Service::WebService::ApiStrategy
- Object
- Setsuzoku::Service::WebService::ApiStrategy
- Setsuzoku::Service::WebService::ApiStrategies::RestStrategy
- Extended by:
- T::Helpers, T::Sig
- Defined in:
- lib/setsuzoku/service/web_service/api_strategies/rest_strategy.rb
Overview
Defines all necessary methods for handling interfacing with a REST API.
Instance Attribute Summary
Attributes included from ApiStrategy
Attributes included from HasConfigContext
Class Method Summary collapse
Instance Method Summary collapse
-
#formulate_request(request_properties = {}, request_options = {}) ⇒ Object
Create the proper request body based on the request_properties and request_options passed.
- #get_request_properties(action_name:, for_stub: false, req_params: {}, action_details: { actions: plugin.api_actions, url: plugin.api_base_url }) ⇒ Object
- #perform_external_call(request:, action_details:, **options) ⇒ Object
- #replace_dynamic_vars(full_url:, req_params: {}) ⇒ Object
- #request_class ⇒ Object
- #request_options(request_format = nil, action_details = {}) ⇒ Object
Methods inherited from Setsuzoku::Service::WebService::ApiStrategy
Methods included from ApiStrategy
#call_external_api, #final, #initialize, #parse_response
Methods included from HasConfigContext
Class Method Details
.required_instance_methods ⇒ Object
23 24 25 |
# File 'lib/setsuzoku/service/web_service/api_strategies/rest_strategy.rb', line 23 def self.required_instance_methods [] end |
Instance Method Details
#formulate_request(request_properties = {}, request_options = {}) ⇒ Object
Create the proper request body based on the request_properties and request_options passed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/setsuzoku/service/web_service/api_strategies/rest_strategy.rb', line 157 def formulate_request(request_properties = {}, = {}) request_format = request_properties.dig(:request_format).to_s params = request_properties[:req_params].merge(.except(:headers)) if request_properties[:request_method] == :get # Faraday expects get requests params to be a hash params elsif request_properties[:req_params].empty? # if the header or request format include urlencoded return the body as a hash if request_format.include?('urlencoded') params elsif %i[put patch post].include?(request_properties[:request_method]) params.to_json # Faraday doesn't support an empty hash for PUT/PATCH/POST requests else params # Faraday supports empty hashes for other request types... end elsif request_format.include?('urlencoded') # if the header or request format include urlencoded return the body as a hash params elsif request_properties[:request_format] == :xml # return either xml or json convert_hash_to_xml(params) else params.to_json end end |
#get_request_properties(action_name:, for_stub: false, req_params: {}, action_details: { actions: plugin.api_actions, url: plugin.api_base_url }) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/setsuzoku/service/web_service/api_strategies/rest_strategy.rb', line 129 def get_request_properties(action_name:, for_stub: false, req_params: {}, action_details: { actions: plugin.api_actions, url: plugin.api_base_url }) action = action_details[:actions][action_name] url = action.has_key?(:request_url) ? action[:request_url] : action_details[:url] request_method, endpoint = action.first request_method = request_method.downcase.to_sym request_format = action[:request_type] response_format = action[:response_type] stub_data = action[:stub_data] if for_stub full_url = url + endpoint formatted_full_url, req_params = replace_dynamic_vars(full_url: full_url, req_params: req_params) { request_method: request_method, endpoint: endpoint, request_format: request_format, response_format: response_format, formatted_full_url: formatted_full_url, req_params: req_params, stub_data: stub_data } end |
#perform_external_call(request:, action_details:, **options) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/setsuzoku/service/web_service/api_strategies/rest_strategy.rb', line 44 def perform_external_call(request:, action_details:, **) request_properties = get_request_properties(action_name: request.action, action_details: action_details, req_params: request.body) = self.(request_properties[:request_format], action_details[:actions][request.action]) = .delete(:authorization) full_request = formulate_request(request_properties, ) connection = Faraday.new(url: request_properties[:formatted_full_url], request: { params_encoder: Faraday::FlatParamsEncoder }) do |faraday| # Request type middle-ware if [:attachment_urls].present? || .dig(:headers, :'Content-Type')&.include?('multipart') faraday.request(:multipart) else faraday.request(:url_encoded) end # Logging faraday.response :logger, Logger.new($stdout), bodies: true if Rails.env.development? if !request.without_headers && # updated authorization with updated faraday # https://lostisland.github.io/faraday/#/middleware/included/authentication if .key?(:token) faraday.request(:authorization, 'Bearer', [:token]) elsif .key?(:basic_auth) faraday.request(:authorization, :basic, [:basic_auth][:username], [:basic_auth][:password]) end end faraday.adapter Faraday.default_adapter end # Attachments URLs = [:attachment_url] || [:attachment_urls] if .present? resp = connection.post do |req| payload = {} # create an array to just iterate over for 1 or many urls = [] if [:attachment_url] = .map do |url| image = open(url, 'rb') Faraday::UploadIO.new(image, T.must(image).content_type, File.basename(url)) end if request_properties[:request_format] == :json payload[:json] = Faraday::UploadIO.new(StringIO.new(full_request), 'application/json') else payload.merge!(full_request) end # if using the singular "attachment_url" key pull out the first item payload[[:attachment_url_key]] = [:attachment_url] ? .first : req.body = payload end else if request.without_headers connection.headers = {} elsif [:headers] connection.headers = (connection.headers || {}).merge([:headers]) end # TODO: !!!! this is wrong here because multipart is used for images as well not only text/plain # TODO: What if the request params are empty? if connection.builder.app.class <= Faraday::Multipart::Middleware full_request = request_properties[:req_params].transform_values do |v| Faraday::Multipart::ParamPart.new(v, 'text/plain') end end resp = connection.send(request_properties[:request_method], request_properties[:formatted_full_url], full_request) end resp end |
#replace_dynamic_vars(full_url:, req_params: {}) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/setsuzoku/service/web_service/api_strategies/rest_strategy.rb', line 213 def replace_dynamic_vars(full_url:, req_params: {}) # replace matching vars in the action with matching params. # scans the string for variables like {{number_sid}} and replaces it with the matching key in params # removes the params variable, as it's probably intended to be in the url only. If you encounter a need to have # it in the body and the url, then you should put it in params twice with different names. req_params = req_params.dup full_url = full_url.dup dynamic_vars = plugin.dynamic_url_params.merge(req_params).with_indifferent_access full_url.scan(/({{.*?}})/).flatten.each do |var| var_name = var.tr('{{ }}', '') next unless dynamic_vars[var_name] full_url.gsub!(var, dynamic_vars[var_name]) req_params.delete(var_name) req_params.delete(var_name.to_sym) end [full_url, req_params] end |
#request_class ⇒ Object
19 20 21 |
# File 'lib/setsuzoku/service/web_service/api_strategies/rest_strategy.rb', line 19 def request_class RestAPIRequest end |
#request_options(request_format = nil, action_details = {}) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/setsuzoku/service/web_service/api_strategies/rest_strategy.rb', line 183 def (request_format = nil, action_details = {}) = { headers: {} } [:headers] .merge!(auth_strategy.get_from_context(:auth_headers).except(:authorization)) .merge!(get_from_context(:api_headers)) .merge!(action_details[:request_options] || {}) [:authorization] = action_details[:authorization] || auth_strategy.get_from_context(:auth_headers)[:authorization] content_type = case request_format when :json 'application/json' when :xml 'application/xml' when :text, :file 'text/plain' when :pdf 'application/pdf' when :'x-www-form-urlencoded;charset=UTF-8' 'application/x-www-form-urlencoded;charset=UTF-8' else # allow any format to be passed otherwise default to application/json request_format || 'application/json' end ([:headers] ||= {})[:'Content-Type'] = content_type end |