From e7427fdd3ccad4a3f8069656918d521f308929e9 Mon Sep 17 00:00:00 2001 From: Rodrigo Castro Date: Fri, 30 Oct 2020 19:41:06 -0300 Subject: [PATCH] remove a adiciona do instance no response! (#113) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * remove o instance no response * a zoop recebe ein para criar conta bancária mas retorna um taxpayer_id!! * fmt --- .../bank_account_create_business.py | 30 +++++++++++++++++++ ...e.py => bank_account_create_individual.py} | 8 ++--- examples/data/add_business_seller.json | 8 ++--- .../data/bank_account_create_business.json | 30 +++++++++++++++++++ ...on => bank_account_create_individual.json} | 18 +++++------ tests/utils.py | 4 +-- tests/wrapper/test_bank_accounts_methods.py | 8 ----- tests/wrapper/test_buyer_methods.py | 5 ---- tests/wrapper/test_card_methods.py | 5 ---- tests/wrapper/test_requests_wrapper.py | 18 ----------- tests/wrapper/test_seller_methods.py | 7 ----- tests/wrapper/test_transactions_methods.py | 19 ++---------- tests/wrapper/test_webhook_methods.py | 2 -- zoop_wrapper/response.py | 2 -- zoop_wrapper/response.pyi | 6 +--- zoop_wrapper/wrapper/bank_account.py | 6 ++-- zoop_wrapper/wrapper/base.py | 17 ----------- zoop_wrapper/wrapper/card.py | 4 +-- zoop_wrapper/wrapper/transaction.py | 10 +++---- 19 files changed, 93 insertions(+), 114 deletions(-) create mode 100644 examples/bank_account/bank_account_create_business.py rename examples/bank_account/{bank_account_create.py => bank_account_create_individual.py} (82%) create mode 100644 examples/data/bank_account_create_business.json rename examples/data/{bank_account_create.json => bank_account_create_individual.json} (56%) diff --git a/examples/bank_account/bank_account_create_business.py b/examples/bank_account/bank_account_create_business.py new file mode 100644 index 00000000..a2e7bd6e --- /dev/null +++ b/examples/bank_account/bank_account_create_business.py @@ -0,0 +1,30 @@ +import os + +from zoop_wrapper import ZoopWrapper, BankAccount +from examples.utils import dump_response + + +""" +Nesse momento as constantes podem ser criadas no arquivo .py. +Mas é recomendado utilizar como variável de ambiente em um '.env' +""" +from zoop_wrapper.constants import MARKETPLACE_ID, ZOOP_KEY + + +client = ZoopWrapper(marketplace_id=MARKETPLACE_ID, key=ZOOP_KEY) + +business_seller_id = "ef90df686ab64e6cbcdc2c1d68156605" +response = client.retrieve_seller(business_seller_id) + +ba = { + "account_number": "123123", + "bank_code": "001", + "holder_name": "Algum Nome", + "routing_number": "123123", + "ein": response.data["ein"], + "type": BankAccount.SAVING_TYPE, +} + +response = client.add_bank_account(ba) + +dump_response(response, os.path.basename(__file__).split(".")[0]) diff --git a/examples/bank_account/bank_account_create.py b/examples/bank_account/bank_account_create_individual.py similarity index 82% rename from examples/bank_account/bank_account_create.py rename to examples/bank_account/bank_account_create_individual.py index 58fd3ede..dc65dbae 100644 --- a/examples/bank_account/bank_account_create.py +++ b/examples/bank_account/bank_account_create_individual.py @@ -17,11 +17,11 @@ brian = client.retrieve_seller(seller_brian) ba = { - "account_number": "123", - "bank_code": "237", + "account_number": "123123", + "bank_code": "001", "holder_name": "Algum Nome", - "routing_number": "foo", - "taxpayer_id": brian.instance.taxpayer_id, + "routing_number": "123123", + "taxpayer_id": brian.data["taxpayer_id"], "type": BankAccount.SAVING_TYPE, } diff --git a/examples/data/add_business_seller.json b/examples/data/add_business_seller.json index 19c59020..b5bd4c45 100644 --- a/examples/data/add_business_seller.json +++ b/examples/data/add_business_seller.json @@ -1,5 +1,5 @@ { - "id": "a482a4d33d3741b287071132bb3225af", + "id": "ef90df686ab64e6cbcdc2c1d68156605", "status": "pending", "resource": "seller", "type": "business", @@ -14,7 +14,7 @@ "business_opening_date": "0000-00-00", "business_facebook": null, "business_twitter": null, - "ein": "16535515052002", + "ein": "74201461611044", "statement_descriptor": null, "mcc": null, "business_address": { @@ -58,6 +58,6 @@ "uri": "/v1/marketplaces/foo/sellers/businesses/", "marketplace_id": "foo", "metadata": {}, - "created_at": "2020-08-24T17:22:06+00:00", - "updated_at": "2020-08-24T17:22:07+00:00" + "created_at": "2020-10-30T22:32:22+00:00", + "updated_at": "2020-10-30T22:32:22+00:00" } \ No newline at end of file diff --git a/examples/data/bank_account_create_business.json b/examples/data/bank_account_create_business.json new file mode 100644 index 00000000..7555af9d --- /dev/null +++ b/examples/data/bank_account_create_business.json @@ -0,0 +1,30 @@ +{ + "id": "4e03e7785405428a90f67f007721025c", + "resource": "bank_account", + "holder_name": "Algum Nome", + "taxpayer_id": "74201461611044", + "description": null, + "bank_name": "Banco do Brasil S.A.", + "bank_code": "001", + "type": "Savings", + "account_number": "123123", + "country_code": "BR", + "routing_number": "123123", + "routing_check_digit": null, + "phone_number": null, + "is_active": true, + "is_verified": false, + "debitable": false, + "customer": "ef90df686ab64e6cbcdc2c1d68156605", + "fingerprint": "05fdfcb94af1e73c223ac75ca69665c3afd6bd62aaad9bdc99af5590b2eb8438", + "address": null, + "verification_checklist": { + "postal_code_check": "unchecked", + "address_line1_check": "unchecked", + "deposit_check": "unchecked" + }, + "metadata": {}, + "uri": "/v1/marketplaces/foo/bank_accounts/4e03e7785405428a90f67f007721025c", + "created_at": "2020-10-30T22:34:01+00:00", + "updated_at": "2020-10-30T22:34:01+00:00" +} \ No newline at end of file diff --git a/examples/data/bank_account_create.json b/examples/data/bank_account_create_individual.json similarity index 56% rename from examples/data/bank_account_create.json rename to examples/data/bank_account_create_individual.json index 2030b7ad..f63723b6 100644 --- a/examples/data/bank_account_create.json +++ b/examples/data/bank_account_create_individual.json @@ -1,22 +1,22 @@ { - "id": "e85edf6d0add48339e3d8189f615c3b1", + "id": "b45141b144f34208b18c4b695e7a6b48", "resource": "bank_account", "holder_name": "Algum Nome", "taxpayer_id": "94998862499", "description": null, - "bank_name": "Banco Bradesco S.A.", - "bank_code": "237", + "bank_name": "Banco do Brasil S.A.", + "bank_code": "001", "type": "Savings", - "account_number": "123", + "account_number": "123123", "country_code": "BR", - "routing_number": "foo", + "routing_number": "123123", "routing_check_digit": null, "phone_number": null, "is_active": true, "is_verified": false, "debitable": false, "customer": "0b05a360f4b749498f74e13004c08024", - "fingerprint": "5415ba4927cb5cec4aa4dfd309a4f6813b5f2644423533ffb2d245713a285a61", + "fingerprint": "66fde16cbcd461a389ad0fa31c61c30c6c5a8a3f44d40cf783f808498a59b0ec", "address": null, "verification_checklist": { "postal_code_check": "unchecked", @@ -24,7 +24,7 @@ "deposit_check": "unchecked" }, "metadata": {}, - "uri": "/v1/marketplaces/foo/bank_accounts/e85edf6d0add48339e3d8189f615c3b1", - "created_at": "2020-10-09T19:54:56+00:00", - "updated_at": "2020-10-09T19:54:57+00:00" + "uri": "/v1/marketplaces/foo/bank_accounts/b45141b144f34208b18c4b695e7a6b48", + "created_at": "2020-10-30T22:34:10+00:00", + "updated_at": "2020-10-30T22:34:11+00:00" } \ No newline at end of file diff --git a/tests/utils.py b/tests/utils.py index a67a3f9c..030c0383 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -48,11 +48,11 @@ def tearDown(self): del self.client @staticmethod - def build_response_mock(status_code=200, content=None, instance=None): + def build_response_mock(status_code=200, content=None, data=None): response = MagicMock( status_code=status_code, json=MagicMock(return_value=content if content else {}), - instance=instance, + data=data if data else {}, ) def raise_for_status(): diff --git a/tests/wrapper/test_bank_accounts_methods.py b/tests/wrapper/test_bank_accounts_methods.py index 8d87e452..b71ea398 100644 --- a/tests/wrapper/test_bank_accounts_methods.py +++ b/tests/wrapper/test_bank_accounts_methods.py @@ -1,8 +1,6 @@ from unittest.mock import patch, MagicMock from tests.utils import APITestCase -from zoop_wrapper.models.bank_account import BankAccount -from zoop_wrapper.models.token import Token from tests.factories.bank_account import IndividualBankAccountFactory from tests.factories.token import ( BankAccountTokenFactory, @@ -33,8 +31,6 @@ def test_retrieve_bank_account(self): self.assertEqual(response.status_code, 200, msg=response.data) data = response.data self.assertEqual(data.get("id"), "foo", msg=data) - self.assertIsInstance(response.instance, BankAccount) - self.assertEqual(response.instance.id, "foo") def test_add_bank_account_token(self): """ @@ -46,8 +42,6 @@ def test_add_bank_account_token(self): response = self.client._BankAccountWrapper__add_bank_account_token(token) self.assertEqual(response.status_code, 201, msg=response.data) - self.assertIsInstance(response.instance, Token) - self.assertIsInstance(response.instance.bank_account, BankAccount) @patch( "zoop_wrapper.wrapper.ZoopWrapper._BankAccountWrapper__add_bank_account_token" @@ -68,5 +62,3 @@ def test_add_bank_account(self, mocked_add_token): response = self.client.add_bank_account(data) self.assertEqual(response.status_code, 200, msg=response.data) - self.assertIsInstance(response.instance, BankAccount) - self.assertEqual(response.instance.id, "foo") diff --git a/tests/wrapper/test_buyer_methods.py b/tests/wrapper/test_buyer_methods.py index 32365f4a..6c944716 100644 --- a/tests/wrapper/test_buyer_methods.py +++ b/tests/wrapper/test_buyer_methods.py @@ -1,7 +1,6 @@ from pycpfcnpj import gen from tests.utils import APITestCase -from zoop_wrapper.models.buyer import Buyer from tests.factories.buyer import BuyerFactory @@ -52,8 +51,6 @@ def test_retrieve_buyer(self): response = self.client.retrieve_buyer("foo") self.assertEqual(response.status_code, 200, msg=response.data) self.assertEqual(response.data.get("id"), "foo") - self.assertIsInstance(response.instance, Buyer) - self.assertEqual(response.instance.id, "foo") def test_remove_buyer(self): """ @@ -74,8 +71,6 @@ def test_search_buyer(self): response = self.client.search_buyer("bar") self.assertEqual(response.status_code, 200, msg=response.data) self.assertEqual(response.data.get("id"), "foo") - self.assertIsInstance(response.instance, Buyer) - self.assertEqual(response.instance.id, "foo") def test_update_buyer(self): self.set_put_mock(201, BuyerFactory(id="1").to_dict()) diff --git a/tests/wrapper/test_card_methods.py b/tests/wrapper/test_card_methods.py index 7990d4ac..2cf129e1 100644 --- a/tests/wrapper/test_card_methods.py +++ b/tests/wrapper/test_card_methods.py @@ -7,7 +7,6 @@ from ..factories.card import CardFactory from ..factories.seller import SellerFactory from ..factories.token import CardTokenFactory, CreateCardTokenFactory -from zoop_wrapper.models.card import Card from zoop_wrapper.exceptions import ValidationError @@ -21,8 +20,6 @@ def test_retrieve_card(self): response = self.client.list_bank_accounts_by_seller("foo") self.assertEqual(response.status_code, 200, msg=response.data) self.assertEqual(response.data.get("id"), "foo") - self.assertIsInstance(response.instance, Card) - self.assertEqual(response.instance.id, "foo") def test_add_card_token(self): """ @@ -52,8 +49,6 @@ def test_add_card(self, mocked_add_token): response = self.client.add_card(data, "bar") self.assertEqual(response.status_code, 200, msg=response.data) - self.assertIsInstance(response.instance, Card) - self.assertEqual(response.instance.id, "foo") @patch("zoop_wrapper.wrapper.ZoopWrapper._CardWrapper__add_card_token") def test_add_card_buyer(self, mocked_add_token): diff --git a/tests/wrapper/test_requests_wrapper.py b/tests/wrapper/test_requests_wrapper.py index d702fe0a..7ade4b55 100644 --- a/tests/wrapper/test_requests_wrapper.py +++ b/tests/wrapper/test_requests_wrapper.py @@ -101,21 +101,3 @@ def test_process_response_error_reasons(self): {"error": {"message": "foo", "reasons": ["bla bla bla"]}}, ) self.assertEqual(processed_response.reason, "foo ['bla bla bla']") - - def test_process_response_resource(self): - response = self.build_response_mock(content={"resource": "test"}) - - processed_response = self.client._RequestsWrapper__process_response(response) - self.assertIsNone(processed_response.instance) - - def test_process_response_resource_list(self): - response = self.build_response_mock( - content={ - "resource": "list", - "items": [{"resource": "test", "message": "foo"}], - } - ) - - processed_response = self.client._RequestsWrapper__process_response(response) - self.assertEqual(len(processed_response.instances), 1) - self.assertEqual(processed_response.instances, [None]) diff --git a/tests/wrapper/test_seller_methods.py b/tests/wrapper/test_seller_methods.py index 825843c0..c9506917 100644 --- a/tests/wrapper/test_seller_methods.py +++ b/tests/wrapper/test_seller_methods.py @@ -4,7 +4,6 @@ from tests.utils import APITestCase from tests.factories.seller import BusinessSellerFactory, IndividualSellerFactory from tests.factories.bank_account import IndividualBankAccountFactory -from zoop_wrapper.models.seller import Seller class ZoopWrapperSellerMethodsTestCase(APITestCase): @@ -28,8 +27,6 @@ def test_retrieve_seller(self): response = self.client.retrieve_seller("foo") self.assertEqual(response.status_code, 200, msg=response.data) self.assertEqual(response.data.get("id"), "foo") - self.assertIsInstance(response.instance, Seller) - self.assertEqual(response.instance.id, "foo") def test_search_individual_seller(self): """ @@ -40,8 +37,6 @@ def test_search_individual_seller(self): response = self.client.search_individual_seller("bar") self.assertEqual(response.status_code, 200, msg=response.data) self.assertEqual(response.data.get("id"), "foo") - self.assertIsInstance(response.instance, Seller) - self.assertEqual(response.instance.id, "foo") def test_search_business_seller(self): """ @@ -52,8 +47,6 @@ def test_search_business_seller(self): response = self.client.search_business_seller("bar") self.assertEqual(response.status_code, 200, msg=response.data) self.assertEqual(response.data.get("id"), "foo") - self.assertIsInstance(response.instance, Seller) - self.assertEqual(response.instance.id, "foo") def test_add_individual_seller(self): self.set_post_mock(201, {}) diff --git a/tests/wrapper/test_transactions_methods.py b/tests/wrapper/test_transactions_methods.py index 52590c29..73734ed7 100644 --- a/tests/wrapper/test_transactions_methods.py +++ b/tests/wrapper/test_transactions_methods.py @@ -1,14 +1,11 @@ from unittest.mock import patch from tests.utils import APITestCase -from zoop_wrapper.models.transaction import Transaction, Source -from zoop_wrapper.models.base import PaymentMethod from tests.factories.transaction import ( CancelTransactionCardFactory, TransactionBoletoFactory, TransactionCreditFactory, ) -from zoop_wrapper.models.token import Token from zoop_wrapper.exceptions import ValidationError @@ -46,8 +43,6 @@ def test_retrieve_transaction(self): response = self.client.retrieve_transaction("foo") self.assertEqual(response.status_code, 200, msg=response.data) self.assertEqual(response.data.get("id"), "foo") - self.assertIsInstance(response.instance, Transaction) - self.assertEqual(response.instance.id, "foo") def test_add_transaction_invoice(self): self.set_post_mock(201, TransactionBoletoFactory().to_dict()) @@ -74,8 +69,6 @@ def test_add_transaction_invoice(self): response = self.client.add_transaction(data) self.assertEqual(response.status_code, 201, msg=response.data) - self.assertEqual(response.instance.payment_type, "boleto") - self.assertIsInstance(response.instance.payment_method, PaymentMethod) def test_add_transaction_card_present(self): self.set_post_mock(201, TransactionCreditFactory().to_dict()) @@ -107,9 +100,6 @@ def test_add_transaction_card_present(self): response = self.client.add_transaction(data) self.assertEqual(response.status_code, 201, msg=response.data) - self.assertEqual(response.instance.payment_type, "credit") - self.assertIsInstance(response.instance.source.card, Token) - self.assertIsInstance(response.instance.source, Source) def test_add_transaction_card_not_present(self): self.set_post_mock(201, TransactionCreditFactory().to_dict()) @@ -138,9 +128,6 @@ def test_add_transaction_card_not_present(self): response = self.client.add_transaction(data) self.assertEqual(response.status_code, 201, msg=response.data) - self.assertEqual(response.instance.payment_type, "credit") - self.assertIsInstance(response.instance.source.card, Token) - self.assertIsInstance(response.instance.source, Source) def test__capture_or_void_transaction(self): """ @@ -159,7 +146,7 @@ def test__capture_or_void_transaction(self): t1 = TransactionCreditFactory(id="1", allow_empty=True) mocked_retrieve_transaction.return_value = self.build_response_mock( - 200, instance=t1 + 200, data={"amount": t1.amount, "on_behalf_of": t1.on_behalf_of} ) response = self.client._capture_or_void_transaction(t1.id, "void") @@ -191,7 +178,7 @@ def test__capture_or_void_transaction_amount(self): t1 = TransactionCreditFactory(id="1", amount=10000, allow_empty=True) mocked_retrieve_transaction.return_value = self.build_response_mock( - 200, instance=t1 + 200, data={"amount": t1.amount, "on_behalf_of": t1.on_behalf_of} ) response = self.client._capture_or_void_transaction(t1.id, "void", 100.00) @@ -224,7 +211,7 @@ def test__capture_or_void_transaction_amount_invalid(self): t1 = TransactionCreditFactory(id="1", amount=10000, allow_empty=True) mocked_retrieve_transaction.return_value = self.build_response_mock( - 200, instance=t1 + 200, data={"amount": t1.amount, "on_behalf_of": t1.on_behalf_of} ) with self.assertRaises(ValidationError) as error_context: diff --git a/tests/wrapper/test_webhook_methods.py b/tests/wrapper/test_webhook_methods.py index 98b630f5..923c55ea 100644 --- a/tests/wrapper/test_webhook_methods.py +++ b/tests/wrapper/test_webhook_methods.py @@ -69,8 +69,6 @@ def test_retrieve_webhook(self): response = self.client.retrieve_webhook("foo") self.assertEqual(response.status_code, 200, msg=response.data) self.assertEqual(response.data.get("id"), "foo") - self.assertIsInstance(response.instance, Webhook) - self.assertEqual(response.instance.id, "foo") self.mocked_get.assert_called_once_with( f"{self.base_url}/webhooks/foo/", auth=self.auth diff --git a/zoop_wrapper/response.py b/zoop_wrapper/response.py index 0283daba..a1b71315 100644 --- a/zoop_wrapper/response.py +++ b/zoop_wrapper/response.py @@ -12,6 +12,4 @@ class ZoopResponse(Response): Attributes: data (dict): json da resposta - instance (:class:`.ResourceModel`): instância do recurso - instances (lista de :class:`.ResourceModel`): lista de instâncias do recurso """ diff --git a/zoop_wrapper/response.pyi b/zoop_wrapper/response.pyi index d289cd24..aa1c783a 100644 --- a/zoop_wrapper/response.pyi +++ b/zoop_wrapper/response.pyi @@ -1,10 +1,6 @@ -from typing import Any, Optional, Dict, List +from typing import Any, Dict from requests.models import Response -from zoop_wrapper.models.base import ResourceModel - class ZoopResponse(Response): data = Dict[str, Any] - instance = Optional[ResourceModel] - instances = Optional[List[ResourceModel]] diff --git a/zoop_wrapper/wrapper/bank_account.py b/zoop_wrapper/wrapper/bank_account.py index 65d2dfe2..82851bac 100644 --- a/zoop_wrapper/wrapper/bank_account.py +++ b/zoop_wrapper/wrapper/bank_account.py @@ -82,12 +82,12 @@ def add_bank_account(self, data: dict): else: raise TypeError("this is not supposed to happen!") - seller_instance = seller_response.instance + seller_data = seller_response.data token_response = self.__add_bank_account_token(instance) - created_token = token_response.instance + created_token = token_response.data - data = {"customer": seller_instance.id, "token": created_token.id} + data = {"customer": seller_data["id"], "token": created_token["id"]} url = self._construct_url(action="bank_accounts") return self._post(url, data=data) diff --git a/zoop_wrapper/wrapper/base.py b/zoop_wrapper/wrapper/base.py index 70b92d6d..96cc57e9 100644 --- a/zoop_wrapper/wrapper/base.py +++ b/zoop_wrapper/wrapper/base.py @@ -3,7 +3,6 @@ from ..constants import ZOOP_KEY, MARKETPLACE_ID from ..exceptions import ValidationError from ..models.base import ZoopObject -from ..models.utils import get_instance_from_data from ..utils import get_logger from ..response import ZoopResponse @@ -29,12 +28,6 @@ def __process_response(response) -> ZoopResponse: Adiciona o :attr:`.data` carregado do :meth:`requests.Response.json`. - Adiciona o :attr:`.instance` ou :attr:`.instances` baseado no resource. - - .. note:: - Apenas adiciona :attr:`.instance` ou :attr:`.instances` se não tiver o dado 'deleted' no :attr:`.data` # noqa - que é retornado em todas as respostas de deleção (200 ok) e se tiver o dado `resource` no :attr:`.data` # noqa - Adiciona :attr:`.error` na resposta se tiver ocorrido erros Args: @@ -48,16 +41,6 @@ def __process_response(response) -> ZoopResponse: """ response.data = response.json() - deleted = response.data.get("deleted") - if not deleted: - resource = response.data.get("resource") - if resource == "list": - response.instances = [ - get_instance_from_data(item) for item in response.data.get("items") - ] - elif resource is not None: - response.instance = get_instance_from_data(response.data) - if response.data.get("error"): error = response.data.get("error") diff --git a/zoop_wrapper/wrapper/card.py b/zoop_wrapper/wrapper/card.py index a890c211..02b57ef3 100644 --- a/zoop_wrapper/wrapper/card.py +++ b/zoop_wrapper/wrapper/card.py @@ -79,9 +79,9 @@ def add_card(self, data: Union[dict, Token], customer_identifier: str): ) token_response = self.__add_card_token(token) - created_token = token_response.instance + created_token = token_response.data - data = {"customer": customer_identifier, "token": created_token.id} + data = {"customer": customer_identifier, "token": created_token["id"]} url = self._construct_url(action="cards") return self._post(url, data=data) diff --git a/zoop_wrapper/wrapper/transaction.py b/zoop_wrapper/wrapper/transaction.py index bad5bfda..48b8015a 100644 --- a/zoop_wrapper/wrapper/transaction.py +++ b/zoop_wrapper/wrapper/transaction.py @@ -151,23 +151,23 @@ def _capture_or_void_transaction(self, identifier, sub_action, amount=None): ) transaction_response = self.retrieve_transaction(identifier) - transaction = transaction_response.instance + transaction_data = transaction_response.data if amount is None: - amount = transaction.amount + amount = transaction_data["amount"] else: amount = convert_currency_float_value_to_cents(amount) - if amount > transaction.amount: + if amount > transaction_data["amount"]: raise ValidationError( self, f"A quantia {amount} é maior do que o " - f"valor {transaction.amount} da transação", + f"valor {transaction_data['amount']} da transação", ) data = { "amount": amount, - "on_behalf_of": transaction.on_behalf_of, + "on_behalf_of": transaction_data["on_behalf_of"], } url = self._construct_url(