-
Notifications
You must be signed in to change notification settings - Fork 1
feat(mambu_payments): add Mambu/Numeral payments datasource #317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
christophebrun-forest
wants to merge
24
commits into
main
Choose a base branch
from
feat/mambu-payments-datasource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e54c558
feat(mambu_payments): add datasource with 7 collections
christophebrun-forest 8bc7cf2
feat(mambu_payments): add incoming_payments, direct_debit_mandates an…
christophebrun-forest 955f6ee
fix(mambu_payments): align expected_payment schema with Numeral payload
christophebrun-forest fe5aed1
feat(mambu_payments): smart actions plugins for account holders, acco…
christophebrun-forest 8b09be5
feat(mambu_payments): add events and files collections
christophebrun-forest 8860314
fix(mambu_payments): raise on unsupported filter predicates instead o…
christophebrun-forest c76ea88
feat(mambu_payments): add returns collection
christophebrun-forest 83bab8c
feat(mambu_payments): add claims collection
christophebrun-forest 269a147
refactor(mambu_payments): make connected_account read-only
christophebrun-forest 54bc207
refactor(mambu_payments): drop transaction account snapshots in favor…
christophebrun-forest 174483e
feat(mambu_payments): add reconciliations collection
christophebrun-forest ab23351
feat(mambu_payments): add payment_captures collection
christophebrun-forest cb4a675
feat(mambu_payments): add payee_verification_requests collection
christophebrun-forest 628e037
feat(mambu_payments): add account-holder relation plugins
christophebrun-forest 25d8f65
feat(mambu_payments): add external-account relation plugins
christophebrun-forest 44ed5d7
feat(mambu_payments): add internal-account relation plugins
christophebrun-forest ea57c2a
feat(mambu_payments): add payment-order relation plugins
christophebrun-forest 63cc59c
feat(mambu_payments): add incoming-payment relation plugins
christophebrun-forest a1efb99
chore(mambu_payments): drop freeze on VERSION constant
christophebrun-forest f0f56e9
refactor(mambu_payments): fix filters/count/pagination
christophebrun-forest a25416d
fix(mambu_payments): use Numeral cursor pagination
christophebrun-forest c223a56
fix(mambu_payments): drop server-side count (unsupported)
christophebrun-forest 637d001
refactor(mambu_payments): dedup link plugins, cut complexity
christophebrun-forest cc99174
ci(mambu_payments): wire datasource into CI and release
christophebrun-forest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| --format documentation | ||
| --color | ||
| --require spec_helper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| source 'https://rubygems.org' | ||
|
|
||
| gemspec | ||
|
|
||
| gem 'forest_admin_datasource_customizer' | ||
| gem 'forest_admin_datasource_toolkit' | ||
| gem 'rake', '~> 13.0' | ||
| gem 'rubocop', '1.86.1' | ||
| gem 'rubocop-performance', '1.26.1' | ||
| gem 'rubocop-rspec', '3.9.0' | ||
|
|
||
| group :development, :test do | ||
| gem 'rspec', '~> 3.0' | ||
| gem 'simplecov', '~> 0.22', require: false | ||
| gem 'webmock', '~> 3.0' | ||
| end | ||
19 changes: 19 additions & 0 deletions
19
packages/forest_admin_datasource_mambu_payments/Gemfile-test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| source 'https://rubygems.org' | ||
|
|
||
| # Specify your gem's dependencies in forest_admin_datasource_mambu_payments.gemspec | ||
| gemspec | ||
|
|
||
| gem 'rake', '~> 13.0' | ||
| gem 'rubocop', '1.86.1' | ||
| gem 'rubocop-performance', '1.26.1' | ||
| gem 'rubocop-rspec', '3.9.0' | ||
|
|
||
| group :development, :test do | ||
| gem 'forest_admin_datasource_customizer', path: '../forest_admin_datasource_customizer' | ||
| gem 'forest_admin_datasource_toolkit', path: '../forest_admin_datasource_toolkit' | ||
| gem 'rspec', '~> 3.0' | ||
| gem 'simplecov', '~> 0.22', require: false | ||
| gem 'simplecov-html', '~> 0.12.3' | ||
| gem 'simplecov_json_formatter', '~> 0.1.4' | ||
| gem 'webmock', '~> 3.0' | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| require 'bundler/gem_tasks' | ||
| require 'rspec/core/rake_task' | ||
|
|
||
| RSpec::Core::RakeTask.new(:spec) | ||
|
|
||
| task default: :spec |
37 changes: 37 additions & 0 deletions
37
...ges/forest_admin_datasource_mambu_payments/forest_admin_datasource_mambu_payments.gemspec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| lib = File.expand_path('lib', __dir__) | ||
| $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib) | ||
|
|
||
| require_relative 'lib/forest_admin_datasource_mambu_payments/version' | ||
|
|
||
| Gem::Specification.new do |spec| | ||
| spec.name = 'forest_admin_datasource_mambu_payments' | ||
| spec.version = ForestAdminDatasourceMambuPayments::VERSION | ||
| spec.authors = ['Forest Admin'] | ||
| spec.email = ['contact@forestadmin.com'] | ||
| spec.homepage = 'https://www.forestadmin.com' | ||
| spec.summary = 'Mambu Payments (Numeral) datasource for Forest Admin Ruby agent.' | ||
| spec.description = 'Surface Mambu Payments connected accounts, payment orders, ' \ | ||
| 'transactions and balances as Forest Admin collections.' | ||
| spec.license = 'GPL-3.0' | ||
| spec.required_ruby_version = '>= 3.0.0' | ||
|
|
||
| spec.metadata['homepage_uri'] = spec.homepage | ||
| spec.metadata['source_code_uri'] = 'https://github.com/ForestAdmin/agent-ruby' | ||
| spec.metadata['changelog_uri'] = 'https://github.com/ForestAdmin/agent-ruby/blob/main/CHANGELOG.md' | ||
| spec.metadata['rubygems_mfa_required'] = 'true' | ||
|
|
||
| spec.files = Dir.chdir(__dir__) do | ||
| `git ls-files -z`.split("\x0").reject do |f| | ||
| (File.expand_path(f) == __FILE__) || | ||
| f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile]) | ||
| end | ||
| end | ||
| spec.bindir = 'exe' | ||
| spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } | ||
| spec.require_paths = ['lib'] | ||
|
|
||
| spec.add_dependency 'activesupport', '>= 6.1' | ||
| spec.add_dependency 'faraday', '~> 2.0' | ||
| spec.add_dependency 'faraday-retry', '~> 2.0' | ||
| spec.add_dependency 'zeitwerk', '~> 2.3' | ||
| end |
44 changes: 44 additions & 0 deletions
44
...ages/forest_admin_datasource_mambu_payments/lib/forest_admin_datasource_mambu_payments.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| require_relative 'forest_admin_datasource_mambu_payments/version' | ||
| require 'logger' | ||
| require 'zeitwerk' | ||
| require 'faraday' | ||
| require 'faraday/retry' | ||
| require 'forest_admin_datasource_toolkit' | ||
|
|
||
| loader = Zeitwerk::Loader.for_gem | ||
| loader.setup | ||
|
|
||
| module ForestAdminDatasourceMambuPayments | ||
| class Error < StandardError; end | ||
| class ConfigurationError < Error; end | ||
| class UnsupportedOperatorError < Error; end | ||
|
|
||
| # Raised when a Numeral API call fails. Carries the HTTP status and the | ||
| # (parsed) response body so callers — smart actions in particular — can | ||
| # surface the API's own validation message instead of a generic string. | ||
| class APIError < Error | ||
| attr_reader :status, :body | ||
|
|
||
| def initialize(message, status: nil, body: nil) | ||
| super(message) | ||
| @status = status | ||
| @body = body | ||
| end | ||
| end | ||
|
|
||
| class << self | ||
| attr_writer :logger | ||
|
|
||
| def logger | ||
| @logger ||= default_logger | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def default_logger | ||
| return Rails.logger if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger | ||
|
|
||
| Logger.new($stderr).tap { |l| l.progname = 'forest_admin_datasource_mambu_payments' } | ||
| end | ||
| end | ||
| end |
166 changes: 166 additions & 0 deletions
166
...rest_admin_datasource_mambu_payments/lib/forest_admin_datasource_mambu_payments/client.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| module ForestAdminDatasourceMambuPayments | ||
| # rubocop:disable Metrics/ClassLength | ||
| class Client | ||
| include Reads | ||
| include Writes | ||
|
|
||
| MAX_PER_PAGE = 100 | ||
|
|
||
| def initialize(configuration) | ||
| @configuration = configuration | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def list_resource(path, params = {}) | ||
| must_succeed("list(#{path})") do | ||
| body = connection.get(path, normalize_params(params)).body | ||
| extract_records(body, path) | ||
| end | ||
| end | ||
|
|
||
| def get_resource(path, id) | ||
| extract_record(connection.get("#{path}/#{id}").body) | ||
| rescue Faraday::ResourceNotFound | ||
| nil | ||
| rescue Faraday::Error => e | ||
| raise api_error("get(#{path}/#{id})", e) | ||
| rescue StandardError => e | ||
| raise APIError, "Mambu Payments API call failed: get(#{path}/#{id}): #{e.class}: #{e.message}" | ||
| end | ||
|
|
||
| def post_resource(path, attributes) | ||
| must_succeed("create(#{path})") do | ||
| extract_record(connection.post(path, attributes).body) | ||
| end | ||
| end | ||
|
|
||
| def patch_resource(path, id, attributes) | ||
| must_succeed("update(#{path}/#{id})") do | ||
| extract_record(connection.patch("#{path}/#{id}", attributes).body) | ||
| end | ||
| end | ||
|
|
||
| # POST .../:id/:action — Numeral exposes side-effect endpoints (approve, | ||
| # cancel, verify) as sub-paths returning the updated resource. | ||
| def post_action_resource(path, id, action, attributes = {}) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| must_succeed("#{action}(#{path}/#{id})") do | ||
| extract_record(connection.post("#{path}/#{id}/#{action}", attributes).body) | ||
| end | ||
| end | ||
|
|
||
| # Numeral list responses are typically wrapped (e.g. { "data": [...] } or | ||
| # { "connected_accounts": [...] }) but we accept a raw array too. Falls back | ||
| # to the first array-valued field so we don't silently coerce a wrapper hash | ||
| # into an array of [key, value] pairs. | ||
| def extract_records(body, path) | ||
| return body if body.is_a?(Array) | ||
| return [] unless body.is_a?(Hash) | ||
|
|
||
| wrapped = body['data'] || body[path] || body['records'] || body['items'] | ||
| wrapped.is_a?(Array) ? wrapped : fallback_records(body, path) | ||
| end | ||
|
|
||
| # Last resort when the wrapper key is unknown: return the first array-valued | ||
| # field, logging that we guessed so an unexpected envelope is visible. | ||
| def fallback_records(body, path) | ||
| fallback = body.values.find { |v| v.is_a?(Array) } | ||
| return [] unless fallback | ||
|
|
||
| ForestAdminDatasourceMambuPayments.logger.warn( | ||
| "[forest_admin_datasource_mambu_payments] list(#{path}) used wrapper-key fallback; " \ | ||
| "body keys=#{body.keys.inspect}" | ||
| ) | ||
| fallback | ||
| end | ||
|
|
||
| def extract_record(body) | ||
| return nil if body.nil? | ||
| return body['data'] if body.is_a?(Hash) && body['data'].is_a?(Hash) | ||
|
|
||
| body | ||
| end | ||
|
|
||
| def delete_resource(path, id) | ||
| must_succeed("delete(#{path}/#{id})") do | ||
| connection.delete("#{path}/#{id}") | ||
| true | ||
| end | ||
| end | ||
|
|
||
| def normalize_params(params) | ||
| params.compact.transform_values { |v| v.is_a?(Array) ? v.join(',') : v } | ||
| end | ||
|
|
||
| def must_succeed(operation) | ||
| yield | ||
| rescue Faraday::Error => e | ||
| raise api_error(operation, e) | ||
| rescue StandardError => e | ||
| raise APIError, "Mambu Payments API call failed: #{operation}: #{e.class}: #{e.message}" | ||
| end | ||
|
|
||
| # Builds an APIError that preserves the HTTP status and the API's own error | ||
| # body. Numeral returns structured validation errors (e.g. on a 422), which | ||
| # smart actions surface to the operator instead of a generic failure string. | ||
| def api_error(operation, error) | ||
| response = error.respond_to?(:response) ? error.response : nil | ||
| status = response.is_a?(Hash) ? response[:status] : nil | ||
| body = response.is_a?(Hash) ? response[:body] : nil | ||
| detail = error_detail(status, body) || "#{error.class}: #{error.message}" | ||
| APIError.new("Mambu Payments API call failed: #{operation}: #{detail}", status: status, body: parse_body(body)) | ||
| end | ||
|
|
||
| def error_detail(status, body) | ||
| return nil unless status | ||
|
|
||
| ["HTTP #{status}", error_message(parse_body(body))].compact.join(' ').strip | ||
| end | ||
|
|
||
| # Pulls the human-readable message out of the common Numeral error shapes | ||
| # ({ "error": { "message": ... } }, { "errors": [...] }, { "message": ... }). | ||
| def error_message(parsed) | ||
| return parsed.to_s[0, 500] unless parsed.is_a?(Hash) | ||
|
|
||
| message = parsed.dig('error', 'message') || parsed['message'] || parsed['detail'] || join_errors(parsed['errors']) | ||
| (message.to_s.empty? ? parsed.to_json : message)[0, 500] | ||
|
qltysh[bot] marked this conversation as resolved.
|
||
| end | ||
|
|
||
| def join_errors(errors) | ||
| Array(errors).filter_map { |e| e.is_a?(Hash) ? (e['message'] || e['detail']) : e }.join('; ') | ||
| end | ||
|
|
||
| def parse_body(body) | ||
| return body unless body.is_a?(String) && !body.empty? | ||
|
|
||
| JSON.parse(body) | ||
| rescue JSON::ParserError | ||
| body | ||
| end | ||
|
|
||
| def best_effort(operation, default:) | ||
| yield | ||
| rescue StandardError => e | ||
| ForestAdminDatasourceMambuPayments.logger.warn( | ||
| "[forest_admin_datasource_mambu_payments] #{operation} failed; degrading: #{e.class}: #{e.message}" | ||
| ) | ||
| default | ||
| end | ||
|
|
||
| def connection | ||
| @connection ||= Faraday.new(url: @configuration.url) do |f| | ||
| f.request :json | ||
| f.request :retry, max: 3, interval: 0.2, backoff_factor: 2, | ||
| retry_statuses: [429, 502, 503, 504] | ||
| f.response :json | ||
| f.response :raise_error | ||
| f.headers['x-api-key'] = @configuration.api_key | ||
| f.headers['Accept'] = 'application/json' | ||
| f.headers['User-Agent'] = "forest_admin_datasource_mambu_payments/#{VERSION}" | ||
| f.options.open_timeout = @configuration.open_timeout | ||
| f.options.timeout = @configuration.timeout | ||
| end | ||
| end | ||
| end | ||
| # rubocop:enable Metrics/ClassLength | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found 17 lines of identical code in 2 locations (mass = 76) [qlty:identical-code]