ETS JSON
Приклад реалізації табличного інтерфейсу без параметрів:
-module(users).
-behaviour(rest).
-compile({parse_transform, rest}).
-include("users.hrl").
-export([init/0, populate/1, exists/1, get/0, get/1, post/1, delete/1]).
-rest_record(user).
init() -> ets:new(users, [public, named_table, {keypos, #user.id}]).
populate(Users) -> ets:insert(users, Users).
exists(Id) -> ets:member(users, wf:to_list(Id)).
get() -> ets:tab2list(users).
get(Id) -> [User] = ets:lookup(users, wf:to_list(Id)), User.
delete(Id) -> ets:delete(users, wf:to_list(Id)).
post(#user{} = User) -> ets:insert(users, User);
post(Data) -> post(from_json(Data, #user{})).
Використання API
$ curl -i -X POST -d "id=vlad" localhost:8005/rest/users
$ curl -i -X POST -d "id=doxtop" localhost:8005/rest/users
$ curl -i -X GET localhost:8005/rest/users
$ curl -i -X PUT -d "id=5HT" localhost:8005/rest/users/vlad
$ curl -i -X GET localhost:8005/rest/users/5HT
$ curl -i -X DELETE localhost:8005/rest/users/5HT
KVS JSON
Приклад універсального багатопараметричного інтерфейсу до баз даних. Додайте вашу схему у mix.exs
{:erp, "~> 0.10.2"},
або rebar.config:
{erp, ".*", {git, "git://github.com/erpuno/erp", {tag,"master"}}},
Запит організаційної структури підприємства:
$ curl -X GET http://localhost:8005/rest/kvs/0/erp/group
{"\/erp\/group":[{"name":"Quanterall","url":"quanterall.com",
"location":[],"type":[]}]}
Перелік інвойсів компанії ФінаТех:
$ curl -i -X GET localhost:8005/rest/kvs/0/plm/FinaTech-Stamps/income
HTTP/1.1 200 OK
content-length: 1718
content-type: application/json
date: Fri, 04 Oct 2019 15:58:16 GMT
server: Cowboy
{"\/plm\/FinaTech-Stamps\/income":[{"invoice":"APR-2018-PAY-FTST","account":[],
"subaccount":[],"volume":{"fraction":0,"digits":12000},"price":{"fraction":0,
"digits":1},"instrument":"USD","type":"crypto","from":[],"to":[]},{"invoice":
"AUG-2018-PAY-FTST","account":[],"subaccount":[],"volume":{"fraction":0,
"digits":12000},"price":{"fraction":0,"digits":1},"instrument":"USD","type":
"crypto","from":[],"to":[]},{"invoice":"FEB-2018-PAY-FTST","account":[],
"subaccount":[],"volume":{"fraction":0,"digits":7000},"price":{"fraction":0,
"digits":1},"instrument":"USD","type":"crypto","from":[],"to":[]},{"invoice":
"JAN-2018-PAY-FTST","account":[],"subaccount":[],"volume":{"fraction":0,"digits":
5000},"price":{"fraction":0,"digits":1},"instrument":"USD","type":"crypto","from":
[],"to":[]},{"invoice":"JUL-2018-PAY-FTST","account":[],"subaccount":[],"volume":
{"fraction":0,"digits":10000},"price":{"fraction":0,"digits":1},"instrument":
"USD","type":"crypto","from":[],"to":[]},{"invoice":"JUN-2018-PAY-FTST",
"account":[],"subaccount":[],"volume":{"fraction":0,"digits":10000},"price":
{"fraction":0,"digits":1},"instrument":"USD","type":"crypto","from":[],"to":[]},
{"invoice":"MAR-2018-PAY-FTST","account":[],"subaccount":[],"volume":
{"fraction":0,"digits":10000},"price":{"fraction":0,"digits":1},"instrument":
"USD","type":"crypto","from":[],"to":[]},{"invoice":"MAY-2018-PAY-FTST",
"account":[],"subaccount":[],"volume":{"fraction":0,"digits":15000},
"price":{"fraction":0,"digits":1},"instrument":"USD","type":"crypto",
"from":[],"to":[]},{"invoice":"SEP-2018-PAY-FTST","account":[],"subaccount":
[],"volume":{"fraction":0,"digits":15000},"price":{"fraction":0,"digits":1},
"instrument":"USD","type":"crypto","from":[],"to":[]}]}
Запит конктретного інвойса:
$ curl -i -X GET localhost:8005/rest/kvs/1/APR-2018-PAY-FTST/plm/FinaTech-Stamps/income
HTTP/1.1 200 OK
content-length: 187
content-type: application/json
date: Fri, 04 Oct 2019 16:58:42 GMT
server: Cowboy
{"invoice":"APR-2018-PAY-FTST","account":[],"subaccount":[],"volume":
{"fraction":0,"digits":12000},"price":{"fraction":0,"digits":1},
"instrument":"USD","type":"crypto","from":[],"to":[]}
Модулі
- rest — rest
- rest_cowboy — простий міст у Erlang модулі
- rest_kvs — багатопараметричний інтерфейс до баз даних
Автор
- Dmitry Bushmelev