lib/revo/loans_api/client.rb in revo-loans_api-0.0.22 vs lib/revo/loans_api/client.rb in revo-loans_api-0.0.23

- old
+ new

@@ -10,28 +10,39 @@ class Revo::LoansApi::Client attr_reader :session_token, :loan_request_token Result = Struct.new(:success?, :response, keyword_init: true) - def initialize(base_url:, login: nil, password: nil, session_token: nil) + def initialize( + base_url:, + login: nil, + password: nil, + session_token: nil, + application_source: nil + ) @base_url = base_url @login = login @password = password @session_token = session_token + @application_source = application_source end def create_session make_request( :post, 'sessions', - user: { login: login, password: password } + params: { user: { login: login, password: password } } ).tap do |result| @session_token = result.response.dig(:user, :authentication_token) if result&.success? end end def create_loan_request(options) - response = make_request(:post, 'loan_requests', loan_request_params(options)) + response = make_request( + :post, + 'loan_requests', + params: loan_request_params(options) + ) return response unless response.success? @loan_request_token = response.response.dig(:loan_request, :token) terms = loan_request_terms return terms unless terms.success? @@ -44,11 +55,15 @@ }) end def update_loan_request(token:, options:) update_params = { loan_request: options } - response = make_request(:put, "loan_requests/#{token}", update_params) + response = make_request( + :put, + "loan_requests/#{token}", + params: update_params + ) return response unless response.success? @loan_request_token = token terms = loan_request_terms @@ -76,15 +91,24 @@ def send_loan_confirmation_message(token:) make_request(:post, "loan_requests/#{token}/client/confirmation") end def complete_loan_request(token:, code:) - make_request(:post, "loan_requests/#{token}/confirmation", code: code) + make_request( + :post, + "loan_requests/#{token}/confirmation", + params: { code: code } + ) end def create_loan(token:, term_id:) - make_request(:post, "loan_requests/#{token}/loan", term_id: term_id) + make_request( + :post, + "loan_requests/#{token}/loan", + params: { term_id: term_id }, + headers: { 'Application-Source': application_source } + ) end def finalize_loan(token:, code:, sms_info: '0', skip_confirmation: false) loan_params = { agree_processing: '1', @@ -95,25 +119,36 @@ if skip_confirmation loan_params[:skip_confirmation] = true loan_params.delete(:confirmation_code) end - make_request(:post, "loan_requests/#{token}/loan/finalization", loan: loan_params) + make_request( + :post, + "loan_requests/#{token}/loan/finalization", + params: { loan: loan_params } + ) end def confirm_loan(token:, bill:) - params = { loan: { bill: bill } } - response = make_request(:put, "loan_requests/#{token}/loan/bill", params) + response = make_request( + :put, + "loan_requests/#{token}/loan/bill", + params: { loan: { bill: bill } } + ) return response unless response.success? Result.new(success?: true, response: response.response) end # returns def orders(store_id:, filters: {}) - make_request(:get, 'orders', store_id: store_id, filters: filters) + make_request( + :get, + 'orders', + params: { store_id: store_id, filters: filters } + ) end def send_return_confirmation_code(order_id:) make_request(:post, "orders/#{order_id}/send_return_confirmation_code") end @@ -125,11 +160,11 @@ confirmation_code: code, amount: amount, store_id: store_id } } - make_request(:post, 'returns', params) + make_request(:post, 'returns', params: params) end def confirm_return(return_id:) make_request(:post, "returns/#{return_id}/confirm") end @@ -137,37 +172,57 @@ def cancel_return(return_id:) make_request(:post, "returns/#{return_id}/cancel") end def start_self_registration(token:, mobile_phone:, skip_message: false) - make_request(:post, "loan_requests/#{token}/client/self_registration", - mobile_phone: mobile_phone, skip_message: skip_message) + make_request( + :post, + "loan_requests/#{token}/client/self_registration", + params: { mobile_phone: mobile_phone, skip_message: skip_message } + ) end def check_client_code(token:, code:) - make_request(:post, "loan_requests/#{token}/client/check_code", code: code) + make_request( + :post, + "loan_requests/#{token}/client/check_code", + params: { code: code } + ) end def create_client(token:, client_params:, provider_data: {}) - make_request(:post, "loan_requests/#{token}/client", - client: client_params, provider_data: provider_data) + make_request( + :post, + "loan_requests/#{token}/client", + params: { client: client_params, provider_data: provider_data } + ) end def update_client(id:, client_params:) - make_request(:patch, "/clients/#{id}", client: client_params) + make_request(:patch, "clients/#{id}", params: { client: client_params }) end def get_client(guid:) - make_request(:get, "/clients/#{guid}") + make_request(:get, "clients/#{guid}") end def create_virtual_card(token:, term_id:) - make_request(:post, "loan_requests/#{token}/virtual_card", term_id: term_id) + make_request( + :post, + "loan_requests/#{token}/virtual_card", + params: { term_id: term_id }, + headers: { 'Application-Source': application_source } + ) end def create_card_loan(token:, term_id:) - make_request(:post, "loan_requests/#{token}/card_loan", term_id: term_id) + make_request( + :post, + "loan_requests/#{token}/card_loan", + params: { term_id: term_id }, + headers: { 'Application-Source': application_source } + ) end def send_billing_shift_confirmation_code(client_id:) make_request(:post, "clients/#{client_id}/billing_shift") end @@ -175,16 +230,23 @@ def billing_shift_info(client_id:) make_request(:get, "clients/#{client_id}/billing_shift/info") end def confirm_billing_shift(client_id:, code:, billing_chain:) - make_request(:post, "clients/#{client_id}/billing_shift/confirmation", - code: code, billing_chain: billing_chain) + make_request( + :post, + "clients/#{client_id}/billing_shift/confirmation", + params: { code: code, billing_chain: billing_chain } + ) end def increase_client_limit(client_id:, amount:) - make_request(:patch, "clients/#{client_id}/limit", amount: amount) + make_request( + :patch, + "clients/#{client_id}/limit", + params: { amount: amount } + ) end def client_loan_documents(client_id:, loan_application_id:) make_request(:get, "clients/#{client_id}/loans/#{loan_application_id}") end @@ -192,18 +254,22 @@ def get_client_additional_services(client_id:) make_request(:get, "clients/#{client_id}/additional_services") end def update_client_additional_services(client_id:, additional_services:) - make_request(:patch, "clients/#{client_id}/additional_services", additional_services) + make_request( + :patch, + "clients/#{client_id}/additional_services", + params: additional_services + ) end private API_CONTENT_TYPE = 'application/json'.freeze - attr_reader :base_url, :login, :password + attr_reader :base_url, :login, :password, :application_source def connection @connection ||= HTTP.persistent(base_url) end @@ -212,12 +278,17 @@ return result if result.success? Result.new(success?: false, response: { errors: { base: [:cant_fetch_loan_request_terms] } }) end - def make_request(method, endpoint, params = {}, &block) - headers = { Authorization: session_token }.compact - response = connection.public_send(method, url_for(endpoint), json: params, headers: headers) + def make_request(method, endpoint, params: {}, headers: {}, &block) + headers = { 'Authorization': session_token }.merge(headers).compact + response = connection.public_send( + method, + url_for(endpoint), + json: params, + headers: headers + ) handle_response(response, &block) rescue HTTP::Error => e handle_error(e) end