rest_patch_resource.erl 960 B

12345678910111213141516171819202122232425262728293031323334
  1. -module(rest_patch_resource).
  2. -export([init/3, allowed_methods/2, content_types_provided/2, get_text_plain/2,
  3. content_types_accepted/2, patch_text_plain/2]).
  4. init(_Transport, _Req, _Opts) ->
  5. {upgrade, protocol, cowboy_rest}.
  6. allowed_methods(Req, State) ->
  7. {[<<"HEAD">>, <<"GET">>, <<"PATCH">>], Req, State}.
  8. content_types_provided(Req, State) ->
  9. {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}.
  10. get_text_plain(Req, State) ->
  11. {<<"This is REST!">>, Req, State}.
  12. content_types_accepted(Req, State) ->
  13. case cowboy_req:method(Req) of
  14. {<<"PATCH">>, Req0} ->
  15. {[{{<<"text">>, <<"plain">>, []}, patch_text_plain}], Req0, State};
  16. {_, Req0} ->
  17. {[], Req0, State}
  18. end.
  19. patch_text_plain(Req, State) ->
  20. case cowboy_req:body(Req) of
  21. {ok, <<"halt">>, Req0} ->
  22. {ok, Req1} = cowboy_req:reply(400, Req0),
  23. {halt, Req1, State};
  24. {ok, <<"false">>, Req0} ->
  25. {false, Req0, State};
  26. {ok, _Body, Req0} ->
  27. {true, Req0, State}
  28. end.