Skip to content

Commit

Permalink
List supported currencies in API under api/currencies (spiral-project…
Browse files Browse the repository at this point in the history
…#961)

* List supported currencies in API under api/currencies

* Added test for /currencies route
  • Loading branch information
petermaksymo authored Dec 13, 2021
1 parent d3cf7dc commit 9f063a3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,23 @@ You can get some project stats with a `GET` on
"balance": -10.5
}
]

### Currencies

You can get a list of supported currencies with a `GET` on
`/api/currencies`:

$ curl --basic https://ihatemoney.org/api/currencies
[
"XXX",
"AED",
"AFN",
.
.
.
"ZAR",
"ZMW",
"ZWL"
]


8 changes: 8 additions & 0 deletions ihatemoney/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from werkzeug.security import check_password_hash
from wtforms.fields.core import BooleanField

from ihatemoney.currency_convertor import CurrencyConverter
from ihatemoney.emails import send_creation_email
from ihatemoney.forms import EditProjectForm, MemberForm, ProjectForm, get_billform_for
from ihatemoney.models import Bill, Person, Project, db
Expand Down Expand Up @@ -49,6 +50,13 @@ def wrapper(*args, **kwargs):
return wrapper


class CurrenciesHandler(Resource):
currency_helper = CurrencyConverter()

def get(self):
return self.currency_helper.get_currencies()


class ProjectsHandler(Resource):
def post(self):
form = ProjectForm(meta={"csrf": False})
Expand Down
2 changes: 2 additions & 0 deletions ihatemoney/api/v1/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ihatemoney.api.common import (
BillHandler,
BillsHandler,
CurrenciesHandler,
MemberHandler,
MembersHandler,
ProjectHandler,
Expand All @@ -17,6 +18,7 @@
CORS(api)
restful_api = Api(api)

restful_api.add_resource(CurrenciesHandler, "/currencies")
restful_api.add_resource(ProjectsHandler, "/projects")
restful_api.add_resource(ProjectHandler, "/projects/<string:project_id>")
restful_api.add_resource(TokenHandler, "/projects/<string:project_id>/token")
Expand Down
5 changes: 5 additions & 0 deletions ihatemoney/tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,11 @@ def test_bills_with_calculation(self):
self.assertStatus(400, req)

def test_currencies(self):
# check /currencies for list of supported currencies
resp = self.client.get("/api/currencies")
self.assertTrue(201, resp.status_code)
self.assertIn("XXX", json.loads(resp.data.decode("utf-8")))

# create project with a default currency
resp = self.api_create("raclette", default_currency="EUR")
self.assertTrue(201, resp.status_code)
Expand Down

0 comments on commit 9f063a3

Please sign in to comment.