petermcd / monzo-api

Python package to interact with the API provided by Monzo bank
MIT License
29 stars 5 forks source link

[BUG] Receipt GET Requests Failing #42

Closed petermcd closed 2 years ago

petermcd commented 2 years ago

Describe the bug Some get requests are failing when the URL needs to be appended caused by #41

To Reproduce

from datetime import datetime, timedelta

from monzo.handlers.filesystem import FileSystem
from monzo.authentication import Authentication
from monzo.endpoints.receipt import Receipt, ReceiptItem

TWO_DAYS_AGO = datetime.now() - timedelta(days=2)

handler = FileSystem(file='monzo.json')

credentials = handler.fetch()

auth = Authentication(
    client_id=credentials['client_id'],
    client_secret=credentials['client_secret'],
    redirect_url='',
    access_token=credentials['access_token'],
    access_token_expiry=credentials['expiry'],
    refresh_token=credentials['refresh_token'],
)

TRANSACTION_ID = 'REPLACE WITH TRANSACTION ID'
EXTERNAL_ID = 'ABC12345'

auth.register_callback_handler(credentials)

item = ReceiptItem(
    description='Test item',
    amount=1410,
    currency='GBP',
)

receipt = Receipt(
    auth=auth,
    transaction_id=TRANSACTION_ID,
    external_id=EXTERNAL_ID,
    transaction_total=1410,
    transaction_currency='GBP',
    items=[item],
)

Receipt.create(auth=auth, receipt=receipt)

receipt.fetch(external_id=EXTERNAL_ID)

Expected behavior Receipt to be fetched

Environment (please complete the following information):

Additional context N/A

petermcd commented 2 years ago

As with #38 the reason this is failing Monzo allows you to create a receipt that is inadvertantly invalid. The actual output is:

{"code":"bad_response.marshaling","message":"Failed to marshal proto receipt to DTO","params":{"amount_currency":"GBP","client_id":"xxx","external_id":"ABC123","tax_currency":"","user_id":"xxx"},"retryable":{}}

It appears the created receipt is good enough to show in the Monzo app but not return using the get API or the delete using the Delete API.

petermcd commented 2 years ago

Issue fixed by not sending null values to the API, therefore optional receipt items now have a sensible default value