anchor.diff:           content_types_accepted/2,
            content_types_provided/2,
            to_json/2,
  -         from_json/2]).
  +         from_json/2,
  +         to_raw/2,
  +         from_raw/2]).
   
   -ignore_xref([rest_init/2,
            rest_terminate/2,
  
positive.code: resource_exists(Req, State) ->
      {true, Req, State}.
  
  content_types_accepted(Req, State) ->
      {[{{<<"application">>, <<"json">>, '*'}, from_json}], Req, State}.
  
  content_types_provided(Req, State) ->
      {[{{<<"application">>, <<"json">>, '*'}, to_json}], Req, State}.
  
  % TODO
  is_authorized(Req, State=#state{handler_state=HState, handler=Handler}) ->
      {Ok, Req1} = Handler:is_authorized(HState, Req),
      {Ok, Req1, State}.
  
  from_json(Req, State=#state{handler_state=HState, handler=Handler}) ->
      {Method, Req1} = cowboy_req:method(Req),
      {ok, BodyRaw, Req2} = cowboy_req:body(Req1),
      % TODO: validate that it's json
      Body = iorio_json:decode(BodyRaw),
  	{PathInfo, Req3} = cowboy_req:path_info(Req2),
  	{Qs, Req4} = cowboy_req:qs_vals(Req3),
      case Handler:handle(HState, Req4, Method, PathInfo, Qs, Body) of
          {ok, {json, Response}, Req5} ->
              Req6 = iorio_http:json_response(Req5, Response),
              {true, Req6, State};
          {ok, {raw, Response, ContentType}, Req5} ->
              Req6 = iorio_http:set_content_type_body(Req5, ContentType, Response),
              {true, Req6, State};
          {error, Reason, Req5} ->
              Req6 = iorio_http:error(Req5, <<"error">>, Reason),
              {false, Req6, State}
      end.
  
  to_json(Req, State=#state{handler_state=HState, handler=Handler}) ->
      {Method, Req1} = cowboy_req:method(Req),
  	{PathInfo, Req2} = cowboy_req:path_info(Req1),
  	{Qs, Req3} = cowboy_req:qs_vals(Req2),
      case Handler:handle(HState, Req3, Method, PathInfo, Qs) of
          {ok, {json, Response}, Req4} ->
              ResponseJson = iorio_json:encode(Response),
              {ResponseJson, Req4, State};
          {ok, {raw, Response, ContentType}, Req4} ->
              Req5 = iorio_http:set_content_type(Req4, ContentType),
              {Response, Req5, State};
          {error, Reason, Req4} ->
              ResponseJson = iorio_json:encode([{type, <<"error">>},
                                                {reason, Reason}]),
              {ok, Req5} = cowboy_req:reply(400, [], ResponseJson, Req4),
              {halt, Req5, State}
      end.
  
  rest_terminate(Req, #state{handler_state=HState, handler=Handler}) ->
      Handler:stop_req(HState, Req).
  
negative.code:          content_types_accepted/2,
           content_types_provided/2,
           to_json/2,
           from_json/2,
           to_raw/2,
           from_raw/2]).
  
  -export([behaviour_info/1]).
  
  