From 7f79ffa4b48cfa3362ef49dc4d039e0ded2b99d3 Mon Sep 17 00:00:00 2001 From: Ivan Shamatov Date: Sat, 20 Nov 2021 13:49:46 +0300 Subject: [PATCH] Preparing clients for partner api (#25) --- README.md | 8 ++++---- lib/yookassa.rb | 22 ++++++++++------------ lib/yookassa/client.rb | 31 +++++++++++++------------------ lib/yookassa/payments.rb | 21 +++++++-------------- lib/yookassa/receipts.rb | 17 +++++------------ lib/yookassa/refunds.rb | 17 +++++------------ lib/yookassa/stores.rb | 15 ++++----------- lib/yookassa/webhooks.rb | 17 +++++------------ spec/yookassa/payments_spec.rb | 3 +-- spec/yookassa/receipts_spec.rb | 3 +-- spec/yookassa/refunds_spec.rb | 3 +-- spec/yookassa_spec.rb | 17 ----------------- 12 files changed, 56 insertions(+), 118 deletions(-) diff --git a/README.md b/README.md index 43ab345..a2e6fbf 100644 --- a/README.md +++ b/README.md @@ -44,8 +44,8 @@ There are some cases, when you need to connect to different Yookassa accounts (s If that is not your case, and you still have multiple shop_ids and api_keys, and need to handle all of them under one application, then you need to instantiate clients inline ```ruby -client1 = Yookassa::Client.new(shop_id: 'shop_1', api_key: '123') -client2 = Yookassa::Client.new(shop_id: 'shop_2', api_key: '456') +client1 = Yookassa::Payments.new(shop_id: 'shop_1', api_key: '123') +client2 = Yookassa::Payments.new(shop_id: 'shop_2', api_key: '456') ``` ### Making Payments @@ -68,8 +68,8 @@ payment = Yookassa.payments.create(payment: payload) # or -client = Yookassa::Client.new(shop_id: 'shop_1', api_key: '123') -payment = client.payments.create(payment: payload) +payments = Yookassa::Payments.new(shop_id: 'shop_1', api_key: '123') +payment = payments.create(payment: payload) ``` #### Other payment requests diff --git a/lib/yookassa.rb b/lib/yookassa.rb index e3c2416..a4d8510 100644 --- a/lib/yookassa.rb +++ b/lib/yookassa.rb @@ -4,11 +4,11 @@ require "dry-struct" require "forwardable" require "yookassa/version" require "yookassa/config" -require "yookassa/client" +require "yookassa/payments" +require "yookassa/refunds" +require "yookassa/receipts" module Yookassa - ConfigError = Class.new(StandardError) - class << self extend Forwardable @@ -20,18 +20,16 @@ module Yookassa @config ||= Config.new end - def client - raise ConfigError, "Specify `shop_id` and `api_key` settings in a `.configure` block" if @config.nil? - - @client ||= Client.new(shop_id: @config.shop_id, api_key: @config.api_key) + def payments + @payments ||= Payments.new end - def partner_api - @partner_api ||= PartnerAPI.new + def refunds + @refunds ||= Refunds.new end - def_delegators :client, :payments, :refunds, :receipts - - def_delegators :partner_api, :stores, :webhooks + def receipts + @receipts ||= Receipts.new + end end end diff --git a/lib/yookassa/client.rb b/lib/yookassa/client.rb index db0de33..b1a5463 100644 --- a/lib/yookassa/client.rb +++ b/lib/yookassa/client.rb @@ -1,9 +1,6 @@ # frozen_string_literal: true require "http" -require_relative "./payments" -require_relative "./refunds" -require_relative "./receipts" require_relative "./entity/error" module Yookassa @@ -12,21 +9,21 @@ module Yookassa attr_reader :http - def initialize(shop_id:, api_key:) - @http = HTTP.basic_auth(user: shop_id, pass: api_key).headers(accept: "application/json") + def initialize(shop_id: Yookassa.config.shop_id, api_key: Yookassa.config.api_key, oauth_token: nil) + @http = HTTP.headers(accept: "application/json") + + if shop_id && api_key + @http.basic_auth(user: shop_id, pass: api_key) + elsif oauth_token + @http.headers("Authorization" => "Bearer #{oauth_token}") + else + message = "Specify `shop_id` and `api_key` settings in a `.configure` block " \ + "or pass `oauth_token` to a client" + raise ConfigError, message + end end - def payments - @payments ||= Payments.new(self) - end - - def refunds - @refunds ||= Refunds.new(self) - end - - def receipts - @receipts ||= Receipts.new(self) - end + private def get(endpoint, query: {}) api_call { http.get("#{API_URL}#{endpoint}", params: query) } @@ -36,8 +33,6 @@ module Yookassa api_call { http.headers("Idempotence-Key" => idempotency_key).post("#{API_URL}#{endpoint}", json: payload) } end - private - def api_call response = yield if block_given? body = JSON.parse(response.body.to_s, symbolize_names: true) diff --git a/lib/yookassa/payments.rb b/lib/yookassa/payments.rb index 0792941..987817a 100644 --- a/lib/yookassa/payments.rb +++ b/lib/yookassa/payments.rb @@ -1,41 +1,34 @@ # frozen_string_literal: true +require_relative "./client" require_relative "./entity/payment" require_relative "./entity/collection" module Yookassa - class Payments - def initialize(api) - @api = api - end - + class Payments < Client def find(payment_id:) - data = api.get("payments/#{payment_id}") + data = get("payments/#{payment_id}") Entity::Payment.new(**data) end def create(payment:, idempotency_key: SecureRandom.hex(10)) - data = api.post("payments", payload: payment, idempotency_key: idempotency_key) + data = post("payments", payload: payment, idempotency_key: idempotency_key) Entity::Payment.new(**data.merge(idempotency_key: idempotency_key)) end def capture(payment_id:, idempotency_key: SecureRandom.hex(10)) - data = api.post("payments/#{payment_id}/capture", idempotency_key: idempotency_key) + data = post("payments/#{payment_id}/capture", idempotency_key: idempotency_key) Entity::Payment.new(**data.merge(idempotency_key: idempotency_key)) end def cancel(payment_id:, idempotency_key: SecureRandom.hex(10)) - data = api.post("payments/#{payment_id}/cancel", idempotency_key: idempotency_key) + data = post("payments/#{payment_id}/cancel", idempotency_key: idempotency_key) Entity::Payment.new(**data.merge(idempotency_key: idempotency_key)) end def list(filters: {}) - data = api.get("payments", query: filters) + data = get("payments", query: filters) Entity::PaymentCollection.new(**data) end - - private - - attr_reader :api end end diff --git a/lib/yookassa/receipts.rb b/lib/yookassa/receipts.rb index 9dcb83d..b423911 100644 --- a/lib/yookassa/receipts.rb +++ b/lib/yookassa/receipts.rb @@ -1,31 +1,24 @@ # frozen_string_literal: true +require_relative "./client" require_relative "./entity/receipt" require_relative "./entity/collection" module Yookassa - class Receipts - def initialize(api) - @api = api - end - + class Receipts < Client def find(receipt_id:) - data = api.get("receipts/#{receipt_id}") + data = get("receipts/#{receipt_id}") Entity::Receipt.new(**data) end def create(payload:, idempotency_key: SecureRandom.hex(10)) - data = api.post("receipts", payload: payload, idempotency_key: idempotency_key) + data = post("receipts", payload: payload, idempotency_key: idempotency_key) Entity::Receipt.new(**data.merge(idempotency_key: idempotency_key)) end def list(filters: {}) - data = api.get("receipts", query: filters) + data = get("receipts", query: filters) Entity::ReceiptCollection.new(**data) end - - private - - attr_reader :api end end diff --git a/lib/yookassa/refunds.rb b/lib/yookassa/refunds.rb index db7c899..6fd18ca 100644 --- a/lib/yookassa/refunds.rb +++ b/lib/yookassa/refunds.rb @@ -1,31 +1,24 @@ # frozen_string_literal: true +require_relative "./client" require_relative "./entity/refund" require_relative "./entity/collection" module Yookassa - class Refunds - def initialize(api) - @api = api - end - + class Refunds < Client def find(payment_id:) - data = api.get("refunds/#{payment_id}") + data = get("refunds/#{payment_id}") Entity::Refund.new(**data) end def create(payload:, idempotency_key: SecureRandom.hex(10)) - data = api.post("refunds", payload: payload, idempotency_key: idempotency_key) + data = post("refunds", payload: payload, idempotency_key: idempotency_key) Entity::Refund.new(**data.merge(idempotency_key: idempotency_key)) end def list(filters: {}) - data = api.get("refunds", query: filters) + data = get("refunds", query: filters) Entity::RefundCollection.new(**data) end - - private - - attr_reader :api end end diff --git a/lib/yookassa/stores.rb b/lib/yookassa/stores.rb index 883edf0..9b9791a 100644 --- a/lib/yookassa/stores.rb +++ b/lib/yookassa/stores.rb @@ -1,20 +1,13 @@ # frozen_string_literal: true +require_relative "./client" require_relative "./entity/store_info" module Yookassa - class Stores - def initialize(partner_api) - @partner_api = partner_api - end - - def me - data = partner_api.get("me") + class Stores < Client + def info + data = get("me") Entity::StoreInfo.new(**data) end - - private - - attr_reader :partner_api end end diff --git a/lib/yookassa/webhooks.rb b/lib/yookassa/webhooks.rb index dea6f8b..0b8b6f7 100644 --- a/lib/yookassa/webhooks.rb +++ b/lib/yookassa/webhooks.rb @@ -1,31 +1,24 @@ # frozen_string_literal: true +require_relative "./client" require_relative "./entity/webhook" require_relative "./entity/collection" module Yookassa - class Webhooks - def initialize(partner_api) - @partner_api = partner_api - end - + class Webhooks < Client def create(payload:, idempotency_key: SecureRandom.hex(10)) - data = partner_api.post("webhooks", payload: payload, idempotency_key: idempotency_key) + data = post("webhooks", payload: payload, idempotency_key: idempotency_key) Entity::Webhook.new(**data.merge(idempotency_key: idempotency_key)) end def list - data = partner_api.get("webhooks") + data = get("webhooks") Entity::WebhookCollection.new(**data) end def delete(webhook_id:) - partner_api.delete("webhooks/#{webhook_id}") + delete("webhooks/#{webhook_id}") true end - - private - - attr_reader :partner_api end end diff --git a/spec/yookassa/payments_spec.rb b/spec/yookassa/payments_spec.rb index 79f69a7..4e52e09 100644 --- a/spec/yookassa/payments_spec.rb +++ b/spec/yookassa/payments_spec.rb @@ -2,10 +2,9 @@ RSpec.describe Yookassa::Payments do let(:config) { { shop_id: "SHOP_ID", api_key: "API_KEY" } } - let(:client) { Yookassa::Client.new(**config) } + let(:payment) { described_class.new(**config) } let(:idempotency_key) { SecureRandom.hex(1) } - let(:payment) { client.payments } let(:body) { File.read("spec/fixtures/payment_response.json") } before { stub_request(:any, //).to_return(body: body, headers: { "Content-Type" => "application/json" }) } diff --git a/spec/yookassa/receipts_spec.rb b/spec/yookassa/receipts_spec.rb index af17c4b..eda0688 100644 --- a/spec/yookassa/receipts_spec.rb +++ b/spec/yookassa/receipts_spec.rb @@ -2,9 +2,8 @@ RSpec.describe Yookassa::Receipts do let(:config) { { shop_id: "SHOP_ID", api_key: "API_KEY" } } - let(:client) { Yookassa::Client.new(**config) } + let(:receipt) { described_class.new(**config) } let(:idempotency_key) { SecureRandom.hex(1) } - let(:receipt) { client.receipts } let(:body) { File.read("spec/fixtures/receipt_response.json") } before { stub_request(:any, //).to_return(body: body, headers: { "Content-Type" => "application/json" }) } diff --git a/spec/yookassa/refunds_spec.rb b/spec/yookassa/refunds_spec.rb index 0cc82e6..f3e9c97 100644 --- a/spec/yookassa/refunds_spec.rb +++ b/spec/yookassa/refunds_spec.rb @@ -2,9 +2,8 @@ RSpec.describe Yookassa::Refunds do let(:config) { { shop_id: "SHOP_ID", api_key: "API_KEY" } } - let(:client) { Yookassa::Client.new(**config) } + let(:refund) { described_class.new(**config) } let(:idempotency_key) { SecureRandom.hex(1) } - let(:refund) { client.refunds } let(:body) { File.read("spec/fixtures/refund_response.json") } before { stub_request(:any, //).to_return(body: body, headers: { "Content-Type" => "application/json" }) } diff --git a/spec/yookassa_spec.rb b/spec/yookassa_spec.rb index 6242b90..14e1a66 100644 --- a/spec/yookassa_spec.rb +++ b/spec/yookassa_spec.rb @@ -19,23 +19,6 @@ RSpec.describe Yookassa do end end - describe ".client" do - context "when no settings are provided" do - before { Yookassa.instance_variable_set(:@config, nil) } - - it "raises an error" do - expect { Yookassa.client }.to raise_error(Yookassa::ConfigError) - end - end - - context "when instance configured" do - it "creates and stores client" do - expect(Yookassa.client).to be_a(Yookassa::Client) - expect(Yookassa.client).to eq(Yookassa.client) - end - end - end - describe ".payments" do it "delegates request to client and creates an instance" do expect(Yookassa.payments).to be_a(Yookassa::Payments)