Skip to content
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

Handle HTTP 409 (conflict) responses #13

Merged
merged 6 commits into from
Aug 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# fiobank

[![PyPI version](https://badge.fury.io/py/redis-collections.svg)](https://badge.fury.io/py/redis-collections)
[![PyPI version](https://badge.fury.io/py/fiobank.svg)](https://badge.fury.io/py/fiobank)
[![Build Status](https://travis-ci.org/honzajavorek/fiobank.svg?branch=master)](https://travis-ci.org/honzajavorek/fiobank)

[Fio Bank API](http://www.fio.cz/bank-services/internetbanking-api) in Python.
Expand Down Expand Up @@ -49,8 +49,8 @@ Listing latest transactions:
>>> client.last(from_date='2013-03-01') # sets cursor to given date and returns following transactions
```

For further information [read code](https://github.com/honzajavorek/fiobank/blob/master/fiobank.py).

## Conflict Error
[Fio API documentation](http://www.fio.cz/docs/cz/API_Bankovnictvi.pdf) (Section 8.2) states that a single token should be used only once per 30s. Otherwise a HTTP 409 Conflict will be returned and `fiobank.ThrottlingError` will be raised.

## License: ISC

Expand Down
26 changes: 18 additions & 8 deletions fiobank.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import requests


__all__ = ('FioBank',)
__all__ = ('FioBank', 'ThrottlingError')


str = six.text_type
Expand All @@ -33,6 +33,13 @@ def sanitize_value(value, convert=None):
return value


class ThrottlingError(Exception):
"""Throttling error raised when api is being used too fast."""

def __str__(self):
return 'Token should be used only once per 30s.'


class FioBank(object):

base_url = 'https://www.fio.cz/ib_api/rest/'
Expand All @@ -47,25 +54,25 @@ class FioBank(object):

# http://www.fio.cz/xsd/IBSchema.xsd
transaction_schema = {
'column22': ('transaction_id', str),
'column0': ('date', coerce_date),
'column1': ('amount', float),
'column14': ('currency', str),
'column2': ('account_number', str),
'column10': ('account_name', str),
'column3': ('bank_code', str),
'column26': ('bic', str),
'column12': ('bank_name', str),
'column4': ('constant_symbol', str),
'column5': ('variable_symbol', str),
'column6': ('specific_symbol', str),
'column7': ('user_identification', str),
'column16': ('recipient_message', str),
'column8': ('type', str),
'column9': ('executor', str),
'column10': ('account_name', str),
'column12': ('bank_name', str),
'column14': ('currency', str),
'column16': ('recipient_message', str),
'column17': ('instruction_id', str),
'column18': ('specification', str),
'column22': ('transaction_id', str),
'column25': ('comment', str),
'column17': ('instruction_id', str),
'column26': ('bic', str),
}

info_schema = {
Expand All @@ -87,6 +94,9 @@ def _request(self, action, **params):
url = template.format(token=self.token, **params)

response = requests.get(url)
if response.status_code == requests.codes['conflict']:
raise ThrottlingError()

response.raise_for_status()

if response.content:
Expand Down