# Generated by API Builder - https://www.apibuilder.io # Service version: 0.0.2-dev # User agent: apibuilder app.apibuilder.io/mard/mard2p-api/0.0.2-dev/ruby_client require 'cgi' require 'net/http' require 'net/https' require 'uri' require 'base64' require 'date' require 'rubygems' require 'json' require 'bigdecimal' # This is a payment pending using Mobile Money API and Credit Card. This API # allows merchant to get payment information of the consumer which have its own # account and money manage by the solution. The merchant can receive its money # if only the consumer agreed [1st case]. module Payp module Mard2p module Api module V0 class Client module Constants BASE_URL = 'https://api.apibuilder.io' unless defined?(Constants::BASE_URL) NAMESPACE = 'payp.mard2p.api.v0' unless defined?(Constants::NAMESPACE) USER_AGENT = 'apibuilder app.apibuilder.io/mard/mard2p-api/0.0.2-dev/ruby_client' unless defined?(Constants::USER_AGENT) VERSION = '0.0.2-dev' unless defined?(Constants::VERSION) VERSION_MAJOR = 0 unless defined?(VERSION_MAJOR) end attr_reader :url def initialize(url, opts={}) @url = HttpClient::Preconditions.assert_class('url', url, String) @base_url = URI(url) @authorization = HttpClient::Preconditions.assert_class_or_nil('authorization', opts.delete(:authorization), HttpClient::Authorization) @default_headers = HttpClient::Preconditions.assert_class('default_headers', opts.delete(:default_headers) || {}, Hash) @http_handler = opts.delete(:http_handler) || HttpClient::DefaultHttpHandler.new HttpClient::Preconditions.assert_empty_opts(opts) HttpClient::Preconditions.check_state(url.match(/http.+/i), "URL[%s] must start with http" % url) end # Creates an instance of the client using the base url specified in the API spec. def Client.at_base_url(opts={}) Client.new(Constants::BASE_URL, opts) end def request(path=nil) HttpClient::Preconditions.assert_class_or_nil('path', path, String) request = HttpClient::Request.new(@http_handler, @base_url, path.to_s).with_header('User-Agent', Constants::USER_AGENT).with_header('X-Apidoc-Version', Constants::VERSION).with_header('X-Apidoc-Version-Major', Constants::VERSION_MAJOR) @default_headers.each do |key, value| request = request.with_header(key, value) end if @authorization request = request.with_auth(@authorization) end request end def accounts @accounts ||= ::Payp::Mard2p::Api::V0::Clients::Accounts.new(self) end def deposits @deposits ||= ::Payp::Mard2p::Api::V0::Clients::Deposits.new(self) end def payments @payments ||= ::Payp::Mard2p::Api::V0::Clients::Payments.new(self) end def transfers @transfers ||= ::Payp::Mard2p::Api::V0::Clients::Transfers.new(self) end def validations @validations ||= ::Payp::Mard2p::Api::V0::Clients::Validations.new(self) end def withdraws @withdraws ||= ::Payp::Mard2p::Api::V0::Clients::Withdraws.new(self) end end module Clients class Accounts def initialize(client) @client = HttpClient::Preconditions.assert_class('client', client, ::Payp::Mard2p::Api::V0::Client) end # Used to create an account. def post_create r = @client.request("/accounts/create").post ::Payp::Mard2p::Api::V0::Models::Account.new(r) end # Used to update an account. def put_update_by_accountid(accountid) HttpClient::Preconditions.assert_class('accountid', accountid, Integer) r = @client.request("/accounts/update/#{accountid}").put ::Payp::Mard2p::Api::V0::Models::Account.new(r) end # Delete some account. def delete_delete_by_accountid(accountid) HttpClient::Preconditions.assert_class('accountid', accountid, Integer) r = @client.request("/accounts/delete/#{accountid}").delete nil end # Find account by its id def get_by_accountid(accountid, incoming={}) HttpClient::Preconditions.assert_class('accountid', accountid, Integer) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :ownerid => HttpClient::Preconditions.assert_class('ownerid', opts.delete(:ownerid), String), :total_amount => HttpClient::Preconditions.assert_class('total_amount', opts.delete(:total_amount), Numeric), :sold => HttpClient::Preconditions.assert_class('sold', opts.delete(:sold), Numeric), :debit => HttpClient::Preconditions.assert_class('debit', opts.delete(:debit), Numeric), :currency => HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String) }.delete_if { |k, v| v.nil? } r = @client.request("/accounts/#{accountid}").with_query(query).get ::Payp::Mard2p::Api::V0::Models::Account.new(r) end # Find all the accounts. def get_list(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :accountid => HttpClient::Preconditions.assert_class('accountid', opts.delete(:accountid), Integer), :ownerid => HttpClient::Preconditions.assert_class('ownerid', opts.delete(:ownerid), String), :total_amount => HttpClient::Preconditions.assert_class('total_amount', opts.delete(:total_amount), Numeric), :currency => HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String), :sold => HttpClient::Preconditions.assert_class('sold', opts.delete(:sold), Numeric), :debit => HttpClient::Preconditions.assert_class('debit', opts.delete(:debit), Numeric) }.delete_if { |k, v| v.nil? } r = @client.request("/accounts/list").with_query(query).get r.map { |x| ::Payp::Mard2p::Api::V0::Models::Account.new(x) } end end class Deposits def initialize(client) @client = HttpClient::Preconditions.assert_class('client', client, ::Payp::Mard2p::Api::V0::Client) end # Delete some deposit. def delete_delete_by_depid(depid) HttpClient::Preconditions.assert_class('depid', depid, Integer) r = @client.request("/deposits/delete/#{depid}").delete nil end # Find deposit by its id. def get_by_depid(depid, incoming={}) HttpClient::Preconditions.assert_class('depid', depid, Integer) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :payment_method => HttpClient::Preconditions.assert_class('payment_method', opts.delete(:payment_method), String), :actorid => HttpClient::Preconditions.assert_class('actorid', opts.delete(:actorid), String), :ref_accountid => HttpClient::Preconditions.assert_class('ref_accountid', opts.delete(:ref_accountid), Integer), :operatorid => HttpClient::Preconditions.assert_class('operatorid', opts.delete(:operatorid), String), :amount => HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric), :phone => HttpClient::Preconditions.assert_class('phone', opts.delete(:phone), Integer), :pin => HttpClient::Preconditions.assert_class('pin', opts.delete(:pin), Integer), :currency => HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String), :payment_context => HttpClient::Preconditions.assert_class('payment_context', opts.delete(:payment_context), String) }.delete_if { |k, v| v.nil? } r = @client.request("/deposits/#{depid}").with_query(query).get ::Payp::Mard2p::Api::V0::Models::Payment.new(r) end # List all the deposits def get_list(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :payid => HttpClient::Preconditions.assert_class('payid', opts.delete(:payid), Integer), :payment_method => HttpClient::Preconditions.assert_class('payment_method', opts.delete(:payment_method), String), :actorid => HttpClient::Preconditions.assert_class('actorid', opts.delete(:actorid), String), :ref_accountid => HttpClient::Preconditions.assert_class('ref_accountid', opts.delete(:ref_accountid), Integer), :operatorid => HttpClient::Preconditions.assert_class('operatorid', opts.delete(:operatorid), String), :amount => HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric), :phone => HttpClient::Preconditions.assert_class('phone', opts.delete(:phone), Integer), :pin => HttpClient::Preconditions.assert_class('pin', opts.delete(:pin), Integer), :currency => HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String), :payment_context => HttpClient::Preconditions.assert_class('payment_context', opts.delete(:payment_context), String) }.delete_if { |k, v| v.nil? } r = @client.request("/deposits/list").with_query(query).get r.map { |x| ::Payp::Mard2p::Api::V0::Models::Payment.new(x) } end end class Payments def initialize(client) @client = HttpClient::Preconditions.assert_class('client', client, ::Payp::Mard2p::Api::V0::Client) end # Used to create a payment. def post_create r = @client.request("/payments/create").post ::Payp::Mard2p::Api::V0::Models::Payment.new(r) end # Used to update a payment. def put_update_by_payid(payid) HttpClient::Preconditions.assert_class('payid', payid, Integer) r = @client.request("/payments/update/#{payid}").put ::Payp::Mard2p::Api::V0::Models::Payment.new(r) end # Delete some payment. def delete_delete_by_payid(payid) HttpClient::Preconditions.assert_class('payid', payid, Integer) r = @client.request("/payments/delete/#{payid}").delete nil end # Find payment by its id. def get_by_payid(payid, incoming={}) HttpClient::Preconditions.assert_class('payid', payid, Integer) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :payment_method => HttpClient::Preconditions.assert_class('payment_method', opts.delete(:payment_method), String), :actorid => HttpClient::Preconditions.assert_class('actorid', opts.delete(:actorid), String), :ref_accountid => HttpClient::Preconditions.assert_class('ref_accountid', opts.delete(:ref_accountid), Integer), :operatorid => HttpClient::Preconditions.assert_class('operatorid', opts.delete(:operatorid), String), :amount => HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric), :phone => HttpClient::Preconditions.assert_class('phone', opts.delete(:phone), Integer), :pin => HttpClient::Preconditions.assert_class('pin', opts.delete(:pin), Integer), :currency => HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String), :payment_context => HttpClient::Preconditions.assert_class('payment_context', opts.delete(:payment_context), String) }.delete_if { |k, v| v.nil? } r = @client.request("/payments/#{payid}").with_query(query).get ::Payp::Mard2p::Api::V0::Models::Payment.new(r) end # Find payment by id accountid def get_by_ref_accountid(ref_accountid, incoming={}) HttpClient::Preconditions.assert_class('ref_accountid', ref_accountid, Integer) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :payid => HttpClient::Preconditions.assert_class('payid', opts.delete(:payid), Integer), :payment_method => HttpClient::Preconditions.assert_class('payment_method', opts.delete(:payment_method), String), :actorid => HttpClient::Preconditions.assert_class('actorid', opts.delete(:actorid), String), :operatorid => HttpClient::Preconditions.assert_class('operatorid', opts.delete(:operatorid), String), :amount => HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric), :phone => HttpClient::Preconditions.assert_class('phone', opts.delete(:phone), Integer), :pin => HttpClient::Preconditions.assert_class('pin', opts.delete(:pin), Integer), :currency => HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String), :payment_context => HttpClient::Preconditions.assert_class('payment_context', opts.delete(:payment_context), String) }.delete_if { |k, v| v.nil? } r = @client.request("/payments/#{ref_accountid}").with_query(query).get ::Payp::Mard2p::Api::V0::Models::Payment.new(r) end # List all the payments def get_list(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :payid => HttpClient::Preconditions.assert_class('payid', opts.delete(:payid), Integer), :payment_method => HttpClient::Preconditions.assert_class('payment_method', opts.delete(:payment_method), String), :actorid => HttpClient::Preconditions.assert_class('actorid', opts.delete(:actorid), String), :ref_accountid => HttpClient::Preconditions.assert_class('ref_accountid', opts.delete(:ref_accountid), Integer), :operatorid => HttpClient::Preconditions.assert_class('operatorid', opts.delete(:operatorid), String), :amount => HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric), :phone => HttpClient::Preconditions.assert_class('phone', opts.delete(:phone), Integer), :pin => HttpClient::Preconditions.assert_class('pin', opts.delete(:pin), Integer), :currency => HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String), :payment_context => HttpClient::Preconditions.assert_class('payment_context', opts.delete(:payment_context), String) }.delete_if { |k, v| v.nil? } r = @client.request("/payments/list").with_query(query).get r.map { |x| ::Payp::Mard2p::Api::V0::Models::Payment.new(x) } end end class Transfers def initialize(client) @client = HttpClient::Preconditions.assert_class('client', client, ::Payp::Mard2p::Api::V0::Client) end # Used to create a transfer resource. def post_create r = @client.request("/transfers/create").post ::Payp::Mard2p::Api::V0::Models::Transfer.new(r) end # Used to update a transfer resource. def put_update_by_transferid(transferid) HttpClient::Preconditions.assert_class('transferid', transferid, Integer) r = @client.request("/transfers/update/#{transferid}").put ::Payp::Mard2p::Api::V0::Models::Transfer.new(r) end # Delete some transfer. def delete_delete_by_transferid(transferid) HttpClient::Preconditions.assert_class('transferid', transferid, Integer) r = @client.request("/transfers/delete/#{transferid}").delete nil end # Find transfer by its id def get_by_transferid(transferid, incoming={}) HttpClient::Preconditions.assert_class('transferid', transferid, Integer) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :senderid => HttpClient::Preconditions.assert_class('senderid', opts.delete(:senderid), String), :amount => HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric), :currency => HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String), :ref_accountid_receiver => HttpClient::Preconditions.assert_class('ref_accountid_receiver', opts.delete(:ref_accountid_receiver), Integer), :ref_accountid_sender => HttpClient::Preconditions.assert_class('ref_accountid_sender', opts.delete(:ref_accountid_sender), Integer) }.delete_if { |k, v| v.nil? } r = @client.request("/transfers/#{transferid}").with_query(query).get ::Payp::Mard2p::Api::V0::Models::Account.new(r) end # Find all the transfers. def get_list(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :transferid => HttpClient::Preconditions.assert_class('transferid', opts.delete(:transferid), Integer), :senderid => HttpClient::Preconditions.assert_class('senderid', opts.delete(:senderid), String), :amount => HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric), :currency => HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String), :ref_accountid_receiver => HttpClient::Preconditions.assert_class('ref_accountid_receiver', opts.delete(:ref_accountid_receiver), Integer), :ref_accountid_sender => HttpClient::Preconditions.assert_class('ref_accountid_sender', opts.delete(:ref_accountid_sender), Integer) }.delete_if { |k, v| v.nil? } r = @client.request("/transfers/list").with_query(query).get r.map { |x| ::Payp::Mard2p::Api::V0::Models::Account.new(x) } end end class Validations def initialize(client) @client = HttpClient::Preconditions.assert_class('client', client, ::Payp::Mard2p::Api::V0::Client) end def post(value) HttpClient::Preconditions.assert_class('value', value, String) r = @client.request("/validations").with_body(value).post ::Payp::Mard2p::Api::V0::Models::Validation.new(r) end end class Withdraws def initialize(client) @client = HttpClient::Preconditions.assert_class('client', client, ::Payp::Mard2p::Api::V0::Client) end # Delete some withdraw. def delete_delete_by_wdwid(wdwid) HttpClient::Preconditions.assert_class('wdwid', wdwid, Integer) r = @client.request("/withdraws/delete/#{wdwid}").delete nil end # Find withdraw by its id. def get_by_wdwid(wdwid, incoming={}) HttpClient::Preconditions.assert_class('wdwid', wdwid, Integer) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :depid => HttpClient::Preconditions.assert_class('depid', opts.delete(:depid), Integer), :payment_method => HttpClient::Preconditions.assert_class('payment_method', opts.delete(:payment_method), String), :actorid => HttpClient::Preconditions.assert_class('actorid', opts.delete(:actorid), String), :ref_accountid => HttpClient::Preconditions.assert_class('ref_accountid', opts.delete(:ref_accountid), Integer), :operatorid => HttpClient::Preconditions.assert_class('operatorid', opts.delete(:operatorid), String), :amount => HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric), :phone => HttpClient::Preconditions.assert_class('phone', opts.delete(:phone), Integer), :pin => HttpClient::Preconditions.assert_class('pin', opts.delete(:pin), Integer), :currency => HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String), :payment_context => HttpClient::Preconditions.assert_class('payment_context', opts.delete(:payment_context), String) }.delete_if { |k, v| v.nil? } r = @client.request("/withdraws/#{wdwid}").with_query(query).get ::Payp::Mard2p::Api::V0::Models::Payment.new(r) end # List all the withdraws def get_list(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) query = { :payid => HttpClient::Preconditions.assert_class('payid', opts.delete(:payid), Integer), :payment_method => HttpClient::Preconditions.assert_class('payment_method', opts.delete(:payment_method), String), :actorid => HttpClient::Preconditions.assert_class('actorid', opts.delete(:actorid), String), :ref_accountid => HttpClient::Preconditions.assert_class('ref_accountid', opts.delete(:ref_accountid), Integer), :operatorid => HttpClient::Preconditions.assert_class('operatorid', opts.delete(:operatorid), String), :amount => HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric), :phone => HttpClient::Preconditions.assert_class('phone', opts.delete(:phone), Integer), :pin => HttpClient::Preconditions.assert_class('pin', opts.delete(:pin), Integer), :currency => HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String), :payment_context => HttpClient::Preconditions.assert_class('payment_context', opts.delete(:payment_context), String) }.delete_if { |k, v| v.nil? } r = @client.request("/withdraws/list").with_query(query).get r.map { |x| ::Payp::Mard2p::Api::V0::Models::Payment.new(x) } end end end module Models class AppSortBy attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of AppSortBy for this value, creating a new instance for an unknown value def AppSortBy.apply(value) if value.instance_of?(AppSortBy) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || AppSortBy.new(value)) end end # Returns the instance of AppSortBy for this value, or nil if not found def AppSortBy.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) AppSortBy.ALL.find { |v| v.value == value } end def AppSortBy.ALL @@all ||= [AppSortBy.created, AppSortBy.edited] end def AppSortBy.created @@_created ||= AppSortBy.new('created') end def AppSortBy.edited @@_edited ||= AppSortBy.new('edited') end def to_hash value end end class OriginalType attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of OriginalType for this value, creating a new instance for an unknown value def OriginalType.apply(value) if value.instance_of?(OriginalType) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || OriginalType.new(value)) end end # Returns the instance of OriginalType for this value, or nil if not found def OriginalType.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) OriginalType.ALL.find { |v| v.value == value } end def OriginalType.ALL @@all ||= [OriginalType.api_json] end # The original is in the api.json format def OriginalType.api_json @@_api_json ||= OriginalType.new('api_json') end def to_hash value end end class Publication attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of Publication for this value, creating a new instance for an unknown value def Publication.apply(value) if value.instance_of?(Publication) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || Publication.new(value)) end end # Returns the instance of Publication for this value, or nil if not found def Publication.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) Publication.ALL.find { |v| v.value == value } end def Publication.ALL @@all ||= [Publication.membership_requests_create, Publication.memberships_create, Publication.applications_create, Publication.versions_create] end # For organizations for which I am an administrator, email me whenever a user # applies to join the org. def Publication.membership_requests_create @@_membership_requests_create ||= Publication.new('membership_requests.create') end # For organizations for which I am a member, email me whenever a user joins the # org. def Publication.memberships_create @@_memberships_create ||= Publication.new('memberships.create') end # For organizations for which I am a member, email me whenever an application is # created. def Publication.applications_create @@_applications_create ||= Publication.new('applications.create') end # For applications that I watch, email me whenever a version is created. def Publication.versions_create @@_versions_create ||= Publication.new('versions.create') end def to_hash value end end class SortOrder attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of SortOrder for this value, creating a new instance for an unknown value def SortOrder.apply(value) if value.instance_of?(SortOrder) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || SortOrder.new(value)) end end # Returns the instance of SortOrder for this value, or nil if not found def SortOrder.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) SortOrder.ALL.find { |v| v.value == value } end def SortOrder.ALL @@all ||= [SortOrder.asc, SortOrder.desc] end def SortOrder.asc @@_asc ||= SortOrder.new('asc') end def SortOrder.desc @@_desc ||= SortOrder.new('desc') end def to_hash value end end class Visibility attr_reader :value def initialize(value) @value = HttpClient::Preconditions.assert_class('value', value, String) end # Returns the instance of Visibility for this value, creating a new instance for an unknown value def Visibility.apply(value) if value.instance_of?(Visibility) value else HttpClient::Preconditions.assert_class_or_nil('value', value, String) value.nil? ? nil : (from_string(value) || Visibility.new(value)) end end # Returns the instance of Visibility for this value, or nil if not found def Visibility.from_string(value) HttpClient::Preconditions.assert_class('value', value, String) Visibility.ALL.find { |v| v.value == value } end def Visibility.ALL @@all ||= [Visibility.public] end # Anybody, including non logged in users, can view this application def Visibility.public @@_public ||= Visibility.new('public') end def to_hash value end end # Account is defined by the app specifically to manage users transactions. class Account attr_reader :accountid, :ownerid, :total_amount, :currency, :sold, :debit def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:accountid, :ownerid, :total_amount, :currency, :sold, :debit], 'Account') @accountid = HttpClient::Preconditions.assert_class('accountid', opts.delete(:accountid), Integer) @ownerid = HttpClient::Preconditions.assert_class('ownerid', opts.delete(:ownerid), String) @total_amount = HttpClient::Preconditions.assert_class('total_amount', opts.delete(:total_amount), Numeric) @currency = HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String) @sold = HttpClient::Preconditions.assert_class('sold', opts.delete(:sold), Numeric) @debit = HttpClient::Preconditions.assert_class('debit', opts.delete(:debit), Numeric) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Account.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :accountid => accountid, :ownerid => ownerid, :total_amount => total_amount, :currency => currency, :sold => sold, :debit => debit } end end class AccountForm attr_reader :accountid, :ownerid, :total_amount, :sold, :debit, :currency def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:accountid, :ownerid, :sold, :currency], 'AccountForm') @accountid = HttpClient::Preconditions.assert_class('accountid', opts.delete(:accountid), Integer) @ownerid = HttpClient::Preconditions.assert_class('ownerid', opts.delete(:ownerid), String) @total_amount = (x = opts.delete(:total_amount); x.nil? ? nil : HttpClient::Preconditions.assert_class('total_amount', x, Numeric)) @sold = HttpClient::Preconditions.assert_class('sold', opts.delete(:sold), Numeric) @debit = (x = opts.delete(:debit); x.nil? ? nil : HttpClient::Preconditions.assert_class('debit', x, Numeric)) @currency = HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) AccountForm.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :accountid => accountid, :ownerid => ownerid, :total_amount => total_amount, :sold => sold, :debit => debit, :currency => currency } end end # Deposit describes a way a payment is handled. It depends of Payment Resource class Deposit attr_reader :depid, :paymentid def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:depid, :paymentid], 'Deposit') @depid = HttpClient::Preconditions.assert_class('depid', opts.delete(:depid), Integer) @paymentid = HttpClient::Preconditions.assert_class('paymentid', opts.delete(:paymentid), Integer) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Deposit.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :depid => depid, :paymentid => paymentid } end end # Payment is defined by the app specifically to emit users transactions. class Payment attr_reader :payid, :actorid, :amount, :ref_accountid, :payment_method, :operatorid, :phone, :pin, :currency, :payment_context def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:payid, :actorid, :amount, :ref_accountid, :payment_method, :operatorid, :phone, :pin, :currency, :payment_context], 'Payment') @payid = HttpClient::Preconditions.assert_class('payid', opts.delete(:payid), Integer) @actorid = HttpClient::Preconditions.assert_class('actorid', opts.delete(:actorid), String) @amount = HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric) @ref_accountid = HttpClient::Preconditions.assert_class('ref_accountid', opts.delete(:ref_accountid), Integer) @payment_method = HttpClient::Preconditions.assert_class('payment_method', opts.delete(:payment_method), String) @operatorid = HttpClient::Preconditions.assert_class('operatorid', opts.delete(:operatorid), String) @phone = HttpClient::Preconditions.assert_class('phone', opts.delete(:phone), Integer) @pin = HttpClient::Preconditions.assert_class('pin', opts.delete(:pin), Integer) @currency = HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String) @payment_context = HttpClient::Preconditions.assert_class('payment_context', opts.delete(:payment_context), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Payment.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :payid => payid, :actorid => actorid, :amount => amount, :ref_accountid => ref_accountid, :payment_method => payment_method, :operatorid => operatorid, :phone => phone, :pin => pin, :currency => currency, :payment_context => payment_context } end end class PaymentForm attr_reader :payid, :actorid, :amount, :ref_accountid, :payment_method, :operatorid, :phone, :pin, :currency, :payment_context def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:payid, :actorid, :amount, :ref_accountid, :payment_method, :operatorid, :phone, :pin, :currency, :payment_context], 'PaymentForm') @payid = HttpClient::Preconditions.assert_class('payid', opts.delete(:payid), Integer) @actorid = HttpClient::Preconditions.assert_class('actorid', opts.delete(:actorid), String) @amount = HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric) @ref_accountid = HttpClient::Preconditions.assert_class('ref_accountid', opts.delete(:ref_accountid), Integer) @payment_method = HttpClient::Preconditions.assert_class('payment_method', opts.delete(:payment_method), String) @operatorid = HttpClient::Preconditions.assert_class('operatorid', opts.delete(:operatorid), String) @phone = HttpClient::Preconditions.assert_class('phone', opts.delete(:phone), Integer) @pin = HttpClient::Preconditions.assert_class('pin', opts.delete(:pin), Integer) @currency = HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String) @payment_context = HttpClient::Preconditions.assert_class('payment_context', opts.delete(:payment_context), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) PaymentForm.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :payid => payid, :actorid => actorid, :amount => amount, :ref_accountid => ref_accountid, :payment_method => payment_method, :operatorid => operatorid, :phone => phone, :pin => pin, :currency => currency, :payment_context => payment_context } end end # Transfer is defined by the app specifically to manage users transactions # (sending and receiving). class Transfer attr_reader :transferid, :senderid, :amount, :ref_accountid_receiver, :ref_accountid_sender, :currency def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:transferid, :senderid, :amount, :ref_accountid_receiver, :ref_accountid_sender, :currency], 'Transfer') @transferid = HttpClient::Preconditions.assert_class('transferid', opts.delete(:transferid), Integer) @senderid = HttpClient::Preconditions.assert_class('senderid', opts.delete(:senderid), String) @amount = HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric) @ref_accountid_receiver = HttpClient::Preconditions.assert_class('ref_accountid_receiver', opts.delete(:ref_accountid_receiver), Integer) @ref_accountid_sender = HttpClient::Preconditions.assert_class('ref_accountid_sender', opts.delete(:ref_accountid_sender), Integer) @currency = HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Transfer.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :transferid => transferid, :senderid => senderid, :amount => amount, :ref_accountid_receiver => ref_accountid_receiver, :ref_accountid_sender => ref_accountid_sender, :currency => currency } end end class TransferForm attr_reader :transferid, :senderid, :amount, :ref_accountid_receiver, :ref_accountid_sender, :currency def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:transferid, :senderid, :amount, :ref_accountid_receiver, :ref_accountid_sender, :currency], 'TransferForm') @transferid = HttpClient::Preconditions.assert_class('transferid', opts.delete(:transferid), Integer) @senderid = HttpClient::Preconditions.assert_class('senderid', opts.delete(:senderid), String) @amount = HttpClient::Preconditions.assert_class('amount', opts.delete(:amount), Numeric) @ref_accountid_receiver = HttpClient::Preconditions.assert_class('ref_accountid_receiver', opts.delete(:ref_accountid_receiver), Integer) @ref_accountid_sender = HttpClient::Preconditions.assert_class('ref_accountid_sender', opts.delete(:ref_accountid_sender), Integer) @currency = HttpClient::Preconditions.assert_class('currency', opts.delete(:currency), String) end def to_json JSON.dump(to_hash) end def copy(incoming={}) TransferForm.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :transferid => transferid, :senderid => senderid, :amount => amount, :ref_accountid_receiver => ref_accountid_receiver, :ref_accountid_sender => ref_accountid_sender, :currency => currency } end end # Used only to validate json files - used as a resource where http status code # defines success class Validation attr_reader :valid, :errors def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:valid], 'Validation') @valid = HttpClient::Preconditions.assert_boolean('valid', opts.delete(:valid)) @errors = HttpClient::Preconditions.assert_class('errors', (x = opts.delete(:errors); x.nil? ? [] : x), Array).map { |v| HttpClient::Preconditions.assert_class('errors', v, String) } end def to_json JSON.dump(to_hash) end def copy(incoming={}) Validation.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :valid => valid, :errors => errors } end end # Withdraw describes a way a payment is handled. It depends of Payment Resource class Withdraw attr_reader :wdwid, :paymentid def initialize(incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) HttpClient::Preconditions.require_keys(opts, [:wdwid, :paymentid], 'Withdraw') @wdwid = HttpClient::Preconditions.assert_class('wdwid', opts.delete(:wdwid), Integer) @paymentid = HttpClient::Preconditions.assert_class('paymentid', opts.delete(:paymentid), Integer) end def to_json JSON.dump(to_hash) end def copy(incoming={}) Withdraw.new(to_hash.merge(HttpClient::Helper.symbolize_keys(incoming))) end def to_hash { :wdwid => wdwid, :paymentid => paymentid } end end end # ===== END OF SERVICE DEFINITION ===== module HttpClient class HttpHandler # Returns a client instance to use # # @param base_uri The base URI for this API # @param path the Requested full http path (including any query strings) def instance(base_uri, path) raise "Override in subclass" end end class HttpHandlerInstance # Executes a request. The provided request object will be an # instance of Net::HTTP (e.g. Net::HTTP::Get) def execute(request) raise "Override in subclass" end end class DefaultHttpHandler < HttpHandler def instance(base_uri, path) DefaultHttpHandlerInstance.new(base_uri) end end class DefaultHttpHandlerInstance < HttpHandlerInstance attr_reader :client def initialize(base_uri) @base_uri = Preconditions.assert_class('base_uri', base_uri, URI) @client = Net::HTTP.new(@base_uri.host, @base_uri.port) if @base_uri.scheme == "https" configure_ssl end end def execute(request) response = begin @client.request(request) rescue SocketError => e raise StandardError.new("Error accessing uri[#{full_uri(request.path)}]: #{e}") end case response when Net::HTTPSuccess response.body else body = response.body rescue nil raise HttpClient::ServerError.new(response.code.to_i, response.message, :body => body, :uri => full_uri(request.path).to_s) end end def full_uri(path) path.start_with?(@base_uri.to_s) ? path : File.join(@base_uri.to_s, path) end # Called to configure SSL if the base uri requires it def configure_ssl @client.use_ssl = true @client.verify_mode = OpenSSL::SSL::VERIFY_PEER @client.cert_store = OpenSSL::X509::Store.new @client.cert_store.set_default_paths end end class Request attr_reader :base_uri, :path, :full_uri def initialize(http_handler, base_uri, path) @http_handler = http_handler @base_uri = Preconditions.assert_class('base_uri', base_uri, URI) @path = Preconditions.assert_class('path', path, String) @full_uri = @base_uri.to_s + @path @params = nil @body = nil @auth = nil @headers = {} @header_keys_lower_case = [] end def with_header(name, value) Preconditions.check_not_blank('name', name, "Header name is required") Preconditions.check_not_blank('value', value, "Header value is required") Preconditions.check_state(!@headers.has_key?(name), "Duplicate header named[%s]" % name) @headers[name] = value @header_keys_lower_case << name.downcase self end def with_auth(auth) Preconditions.assert_class('auth', auth, HttpClient::Authorization) Preconditions.check_state(@auth.nil?, "auth previously set") if auth.scheme.name == AuthScheme::BASIC.name @auth = auth else raise "Auth Scheme[#{auth.scheme.name}] not supported" end self end def with_query(params) Preconditions.assert_class('params', params, Hash) Preconditions.check_state(@params.nil?, "Already have query parameters") @params = params self end # Wrapper to set Content-Type header to application/json and set # the provided json document as the body def with_json(json) @headers['Content-Type'] ||= 'application/json; charset=UTF-8' with_body(json) end def with_body(body) Preconditions.check_not_blank('body', body) @body = body self end def get(&block) do_request(Net::HTTP::Get, &block) end def delete(&block) do_request(Net::HTTP::Delete, &block) end def options(&block) do_request(Net::HTTP::Options, &block) end def post(&block) do_request(Net::HTTP::Post, &block) end def put(&block) do_request(Net::HTTP::Put, &block) end class PATCH < Net::HTTP::Put METHOD = "PATCH" end def patch(&block) do_request(PATCH, &block) end def do_request(klass) Preconditions.assert_class('klass', klass, Class) uri = @full_uri.dup if q = to_query(@params) uri += "?%s" % q end request = klass.send(:new, uri) # DEBUG curl = ['curl'] # DEBUG if klass != Net::HTTP::Get # DEBUG curl << "-X%s" % klass.name.split("::").last.upcase # DEBUG end if @body # DEBUG tmpfile = "/tmp/rest_client.tmp" # DEBUG File.open(tmpfile, "w") { |os| os << @body.to_s } # DEBUG curl << "-d@%s" % tmpfile request.body = @body end if @auth # DEBUG curl << "-u \"%s:%s\"" % [@auth.username, @auth.password] Preconditions.check_state(!@header_keys_lower_case.include?("authorization"), "Cannot specify both an Authorization header and an auth instance") user_pass = "%s:%s" % [@auth.username, @auth.password] encoded = Base64.encode64(user_pass).to_s.split("\n").map(&:strip).join request.add_field("Authorization", "Basic %s" % encoded) end @headers.each { |key, value| # DEBUG curl << "-H \"%s: %s\"" % [key, value] request.add_field(key, value) } # DEBUG curl << "'%s%s'" % [@base_uri, path] # DEBUG puts curl.join(" ") raw_response = @http_handler.instance(@base_uri, request.path).execute(request) response = raw_response.to_s == "" ? nil : JSON.parse(raw_response) if block_given? yield response else response end end private def to_query(params={}) parts = (params || {}).map { |k,v| if v.is_a?(Enumerable) v.map { |el| "%s=%s" % [k, CGI.escape(el.to_s)] } else "%s=%s" % [k, CGI.escape(v.to_s)] end } parts.empty? ? nil : parts.join("&") end end class ServerError < StandardError attr_reader :code, :details, :body, :uri def initialize(code, details, incoming={}) opts = HttpClient::Helper.symbolize_keys(incoming) @code = HttpClient::Preconditions.assert_class('code', code, Integer) @details = HttpClient::Preconditions.assert_class('details', details, String) @body = HttpClient::Preconditions.assert_class_or_nil('body', opts.delete(:body), String) @uri = HttpClient::Preconditions.assert_class_or_nil('uri', opts.delete(:uri), String) HttpClient::Preconditions.assert_empty_opts(opts) super(self.message) end def message m = "%s %s" % [@code, @details] if @body m << ": %s" % @body end m end def body_json @body ? JSON.parse(@body) : nil end end class PreconditionException < StandardError attr_reader :message def initialize(message) super(message) @message = message end end module Preconditions def Preconditions.check_argument(expression, error_message=nil) if !expression raise PreconditionException.new(error_message || "check_argument failed") end nil end def Preconditions.check_state(expression, error_message=nil) if !expression raise PreconditionException.new(error_message || "check_state failed") end nil end def Preconditions.check_not_nil(field_name, reference, error_message=nil) if reference.nil? raise PreconditionException.new(error_message || "argument for %s cannot be nil" % field_name) end reference end def Preconditions.check_not_blank(field_name, reference, error_message=nil) if reference.to_s.strip == "" raise PreconditionException.new(error_message || "argument for %s cannot be blank" % field_name) end reference end # Throws an error if opts is not empty. Useful when parsing # arguments to a function def Preconditions.assert_empty_opts(opts) if !opts.empty? raise PreconditionException.new("Invalid opts: #{opts.keys.inspect}\n#{opts.inspect}") end end # Requires that the provided hash has the specified keys. # @param fields A list of symbols def Preconditions.require_keys(hash, fields, error_prefix=nil) missing = fields.select { |f| !hash.has_key?(f) } if !missing.empty? msg = "Missing required fields: " + missing.join(", ") raise PreconditionException.new(error_prefix.empty? ? msg : "#{error_prefix}: #{msg}") end end # Asserts that value is not nill and is_?(klass). Returns # value. Common use is # # amount = Preconditions.assert_class('amount', amount, BigDecimal) def Preconditions.assert_class(field_name, value, klass) Preconditions.check_not_nil('field_name', field_name) Preconditions.check_not_nil('klass', klass) Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of class %s" % [field_name, klass.name]) Preconditions.check_state(value.is_a?(klass), "Value for #{field_name} is of type[#{value.class}] - class[#{klass}] is required. value[#{value.inspect.to_s}]") value end def Preconditions.assert_class_or_nil(field_name, value, klass) if !value.nil? Preconditions.assert_class(field_name, value, klass) end end def Preconditions.assert_boolean(field_name, value) Preconditions.check_not_nil('field_name', field_name) Preconditions.check_not_nil('value', value, "Value for %s cannot be nil. Expected an instance of TrueClass or FalseClass" % field_name) Preconditions.check_state(value.is_a?(TrueClass) || value.is_a?(FalseClass), "Value for #{field_name} is of type[#{value.class}] - class[TrueClass or FalseClass] is required. value[#{value.inspect.to_s}]") value end def Preconditions.assert_boolean_or_nil(field_name, value) if !value.nil? Preconditions.assert_boolean(field_name, value) end end def Preconditions.assert_collection_of_class(field_name, values, klass) Preconditions.assert_class(field_name, values, Array) values.each { |v| Preconditions.assert_class(field_name, v, klass) } end def Preconditions.assert_hash_of_class(field_name, hash, klass) Preconditions.assert_class(field_name, hash, Hash) values.each { |k, v| Preconditions.assert_class(field_name, v, klass) } end end class AuthScheme attr_reader :name def initialize(name) @name = HttpClient::Preconditions.check_not_blank('name', name) end BASIC = AuthScheme.new("basic") unless defined?(BASIC) end class Authorization attr_reader :scheme, :username, :password def initialize(scheme, username, opts={}) @scheme = HttpClient::Preconditions.assert_class('schema', scheme, AuthScheme) @username = HttpClient::Preconditions.check_not_blank('username', username, "username is required") @password = HttpClient::Preconditions.assert_class_or_nil('password', opts.delete(:password), String) HttpClient::Preconditions.assert_empty_opts(opts) end def Authorization.basic(username, password=nil) Authorization.new(AuthScheme::BASIC, username, :password => password) end end module Helper def Helper.symbolize_keys(hash) Preconditions.assert_class('hash', hash, Hash) new_hash = {} hash.each { |k, v| new_hash[k.to_sym] = v } new_hash end def Helper.to_big_decimal(value) value ? BigDecimal(value.to_s) : nil end def Helper.to_object(value) value ? (value.is_a?(Hash) ? value : JSON.parse(value)) : nil end def Helper.to_uuid(value) Preconditions.check_state(value.nil? || value.match(/^\w\w\w\w\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\-\w\w\w\w\w\w\w\w\w\w\w\w$/), "Invalid guid[%s]" % value) value end def Helper.to_date_iso8601(value) if value.is_a?(Date) value elsif value Date.parse(value.to_s) else nil end end def Helper.to_date_time_iso8601(value) if value.is_a?(DateTime) value elsif value DateTime.parse(value.to_s) else nil end end def Helper.date_iso8601_to_string(value) value.nil? ? nil : value.strftime('%Y-%m-%d') end def Helper.date_time_iso8601_to_string(value) value.nil? ? nil : value.strftime('%Y-%m-%dT%H:%M:%S%z') end TRUE_STRINGS = ['t', 'true', 'y', 'yes', 'on', '1', 'trueclass'] unless defined?(TRUE_STRINGS) FALSE_STRINGS = ['f', 'false', 'n', 'no', 'off', '0', 'falseclass'] unless defined?(FALSE_STRINGS) def Helper.to_boolean(field_name, value) string = value.to_s.strip.downcase if TRUE_STRINGS.include?(string) true elsif FALSE_STRINGS.include?(string) false elsif string != "" raise PreconditionException.new("Unsupported boolean value[#{string}]. For true, must be one of: #{TRUE_STRINGS.inspect}. For false, must be one of: #{FALSE_STRINGS.inspect}") else nil end end end end end end end end