diff --git a/docs/api.md b/docs/api.md index a4b91948..9c94839b 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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" + ] + + diff --git a/ihatemoney/api/common.py b/ihatemoney/api/common.py index bc35ac93..1cfc34c5 100644 --- a/ihatemoney/api/common.py +++ b/ihatemoney/api/common.py @@ -5,6 +5,7 @@ from flask_restful import Resource, abort 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 @@ -49,6 +50,13 @@ def need_auth(f): 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}) diff --git a/ihatemoney/api/v1/resources.py b/ihatemoney/api/v1/resources.py index dc1708ce..6c46e4f9 100644 --- a/ihatemoney/api/v1/resources.py +++ b/ihatemoney/api/v1/resources.py @@ -5,6 +5,7 @@ from flask_restful import Api from ihatemoney.api.common import ( BillHandler, BillsHandler, + CurrenciesHandler, MemberHandler, MembersHandler, ProjectHandler, @@ -17,6 +18,7 @@ api = Blueprint("api", __name__, url_prefix="/api") CORS(api) restful_api = Api(api) +restful_api.add_resource(CurrenciesHandler, "/currencies") restful_api.add_resource(ProjectsHandler, "/projects") restful_api.add_resource(ProjectHandler, "/projects/") restful_api.add_resource(TokenHandler, "/projects//token") diff --git a/ihatemoney/tests/api_test.py b/ihatemoney/tests/api_test.py index d9f06aa4..f5475b11 100644 --- a/ihatemoney/tests/api_test.py +++ b/ihatemoney/tests/api_test.py @@ -576,6 +576,11 @@ class APITestCase(IhatemoneyTestCase): 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)