Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
Webhooks! (#14)
Browse files Browse the repository at this point in the history
* inicia alguns exemplos

* adiciona webhook

* adiciona exemplos

* adiciona alguns testes!

* altera um pouco os exemplos!

* altera exemplos

* fmt

* corrige formatação e black
  • Loading branch information
rodrigondec authored Sep 15, 2020
1 parent 8c5dbe9 commit 528d48d
Show file tree
Hide file tree
Showing 25 changed files with 239 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pip.install:
pip install -r requirements-dev.txt
pip install -U -r requirements-dev.txt

pip.install.build:
pip install -r requirements-build.txt
pip install -U -r requirements-build.txt

config.env:
cp .env.sample .env
Expand Down
10 changes: 7 additions & 3 deletions examples/address/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

client = ImopayWrapper()

a = {
"owner": "algum id",
owner_imopay_id = "foo"

data = {
"owner": owner_imopay_id,
"city": "Natal",
"uf": "RN",
"zip_code": "99999999",
Expand All @@ -14,4 +16,6 @@
}


response = client.address.create(a)
response = client.address.create(data)

print(response.data)
2 changes: 1 addition & 1 deletion examples/address/get_by_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

response = client.address.get_by_document(document)

print(response)
print(response.data)
6 changes: 3 additions & 3 deletions examples/address/retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

client = ImopayWrapper()

id = "foo"
imopay_id = "foo"

response = client.address.retrieve(id)
response = client.address.retrieve(imopay_id)

print(response)
print(response.data)
8 changes: 5 additions & 3 deletions examples/address/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

client = ImopayWrapper()

id = "foo"
imopay_id = "foo"

a = {
data = {
"owner": "bar",
"city": "Natal",
"uf": "RN",
Expand All @@ -15,4 +15,6 @@
"complement": None,
}

response = client.address.update(id, a)
response = client.address.update(imopay_id, data)

print(response.data)
6 changes: 4 additions & 2 deletions examples/company/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

client = ImopayWrapper()

c = {
data = {
"email": "[email protected]",
"phone": "+55 74 99572 8196",
"cnpj": "13415795551619",
Expand All @@ -13,4 +13,6 @@
}


response = client.company.create(c)
response = client.company.create(data)

print(response.data)
6 changes: 4 additions & 2 deletions examples/company/retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

client = ImopayWrapper()

id = "foo"
imopay_id = "foo"

response = client.company.retrieve(id)
response = client.company.retrieve(imopay_id)

print(response.data)
9 changes: 9 additions & 0 deletions examples/company/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from imopay_wrapper import ImopayWrapper

client = ImopayWrapper()

cnpj = "foo"

response = client.company.retrieve(cnpj)

print(response.data)
8 changes: 5 additions & 3 deletions examples/company/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

client = ImopayWrapper()

id = "foo"
imopay_id = "foo"

c = {
data = {
"email": "[email protected]",
"phone": "+55 74 99572 8196",
"cnpj": "13415795551619",
Expand All @@ -14,4 +14,6 @@
"website": "https://horton-black.info/",
}

response = client.company.update(id, c)
response = client.company.update(imopay_id, data)

print(response.data)
6 changes: 4 additions & 2 deletions examples/person/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

client = ImopayWrapper()

p = {
data = {
"email": "[email protected]",
"phone": "+55 (019) 92498 1907",
"first_name": "William",
Expand All @@ -12,4 +12,6 @@
"mobile_phone": "+55 60 97852 2366",
}

response = client.person.create(p)
response = client.person.create(data)

print(response.data)
6 changes: 4 additions & 2 deletions examples/person/retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

client = ImopayWrapper()

id = "foo"
imopay_id = "foo"

response = client.person.retrieve(id)
response = client.person.retrieve(imopay_id)

print(response.data)
9 changes: 9 additions & 0 deletions examples/person/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from imopay_wrapper import ImopayWrapper

client = ImopayWrapper()

cpf = "foo"

response = client.person.retrieve(cpf)

print(response.data)
8 changes: 5 additions & 3 deletions examples/person/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

client = ImopayWrapper()

id = "foo"
imopay_id = "foo"

p = {
data = {
"email": "[email protected]",
"phone": "+55 (019) 92498 1907",
"first_name": "William",
Expand All @@ -14,4 +14,6 @@
"mobile_phone": "+55 60 97852 2366",
}

response = client.person.update(id, p)
response = client.person.update(imopay_id, data)

print(response.data)
Empty file.
Empty file added examples/webhook/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions examples/webhook/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from imopay_wrapper import ImopayWrapper


client = ImopayWrapper()

data = {
"url": "http://494830363d1d.ngrok.io",
"event": "t",
"description": "webhook de teste",
}

response = client.webhook.create(data)

print(response.data)
10 changes: 10 additions & 0 deletions examples/webhook/destroy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from imopay_wrapper import ImopayWrapper


client = ImopayWrapper()

imopay_imopay_id = "foo"

response = client.webhook.destroy(imopay_imopay_id)

print(response.data)
10 changes: 10 additions & 0 deletions imopay_wrapper/models/webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from dataclasses import dataclass

from .base import BaseImopayObj


@dataclass
class Webhook(BaseImopayObj):
event: str
url: str
description: str
2 changes: 2 additions & 0 deletions imopay_wrapper/wrapper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .company import CompanyWrapper
from .person import PersonWrapper
from .transaction import TransactionWrapper
from .webhook import WebhookWrapper


class ImopayWrapper:
Expand All @@ -10,3 +11,4 @@ def __init__(self, *args, **kwargs):
self.company = CompanyWrapper(*args, **kwargs)
self.person = PersonWrapper(*args, **kwargs)
self.transaction = TransactionWrapper(*args, **kwargs)
self.webhook = WebhookWrapper(*args, **kwargs)
14 changes: 13 additions & 1 deletion imopay_wrapper/wrapper/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from json.decoder import JSONDecodeError

import requests

from ..constants import IMOPAY_ENV, IMOPAY_API_KEY
Expand Down Expand Up @@ -42,7 +44,11 @@ def __process_response(response) -> requests.Response:
Returns:
'objeto' (:class:`.requests.Response`) de resposta http
"""
response.data = response.json()
try:
response.data = response.json()
except JSONDecodeError:
response.data = {}
response.reason = response.data
response.raise_for_status()
return response

Expand Down Expand Up @@ -246,3 +252,9 @@ class RetrieveMixin:
def retrieve(self, identifier: str):
url = self._construct_url(action=self.action, identifier=identifier)
return self._get(url)


class DestroyMixin:
def destroy(self, identifier: str):
url = self._construct_url(action=self.action, identifier=identifier)
return self._delete(url)
16 changes: 16 additions & 0 deletions imopay_wrapper/wrapper/webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .base import BaseImopayWrapper, CreateMixin, DestroyMixin
from ..models.webhook import Webhook


class WebhookWrapper(BaseImopayWrapper, CreateMixin, DestroyMixin):
"""
Wrapper para os métodos de webhooks
"""

@property
def action(self):
return "webhooks"

@property
def model(self):
return Webhook
2 changes: 1 addition & 1 deletion tests/wrapper/test_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from imopay_wrapper.models.address import Address


class PersonWrapperTestCase(LocalImopayTestCase):
class AddressWrapperTestCase(LocalImopayTestCase):
def setUp(self):
self.client = ImopayWrapper().address

Expand Down
24 changes: 24 additions & 0 deletions tests/wrapper/test_base_imopay.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from imopay_wrapper.wrapper.base import (
BaseImopayWrapper,
CreateMixin,
DestroyMixin,
UpdateMixin,
RetrieveMixin,
)
Expand All @@ -16,6 +17,7 @@ def setUp(self):
(
BaseImopayWrapper,
CreateMixin,
DestroyMixin,
UpdateMixin,
RetrieveMixin,
),
Expand Down Expand Up @@ -51,6 +53,28 @@ def test_create(self):
mocked_model.return_value.return_value.to_dict.return_value,
)

def test_destroy(self):
with patch(
"imopay_wrapper.wrapper.base.BaseImopayWrapper.model",
new_callable=PropertyMock,
) as mocked_model, patch(
"imopay_wrapper.wrapper.base.BaseImopayWrapper.action",
new_callable=PropertyMock,
) as mocked_action, patch(
"imopay_wrapper.wrapper.base.BaseImopayWrapper._delete"
) as mocked_delete:
self.client.destroy("1")

mocked_model.assert_not_called()

mocked_action.assert_called_once()

mocked_delete.assert_called_once_with(
self.client._construct_url(
action=mocked_action.return_value, identifier="1"
)
)

def test_update(self):
with patch(
"imopay_wrapper.wrapper.base.BaseImopayWrapper.model",
Expand Down
Loading

0 comments on commit 528d48d

Please sign in to comment.