Skip to content

Commit

Permalink
Remove debug code and improve code estethic
Browse files Browse the repository at this point in the history
  • Loading branch information
lafka committed Aug 24, 2012
1 parent 63cfb7e commit f72811b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
18 changes: 6 additions & 12 deletions src/tavern_http.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -50,15 +50,12 @@ init(_Transport, Req, [Handler]) ->
handle(Module, Req, #tavern{} = State) -> handle(Module, Req, #tavern{} = State) ->
try try
case tavern_req:call(Req, State, fun tavern_req:client_acceptable/2) of case tavern_req:call(Req, State, fun tavern_req:client_acceptable/2) of
%% Request is valid, pass it back to module for completion
{true, Req2, State2} -> {true, Req2, State2} ->
{Status, Payload, Req3, State3} = handle_call(Module, Req2, State2), {Status, NewReq, NewState, Payload} = handle_call(Module, Req2, State2),
error_logger:info_msg("resp: call: ~p ~p~n", [Status, Payload]), handle_resp(NewReq, NewState, Status, Payload);
handle_resp(Req3, State3, Status, Payload); %% tavern_req specified return status
%% tavern_req specifies return status {{Status, Payload}, NewReq, NewState} ->
{{Status, Payload}, Req2, State2} -> handle_resp(NewReq, NewState, Status, Payload)
error_logger:info_msg("resp: direct: ~p ~p~n", [Status, Payload]),
handle_resp(Req2, State2, Status, Payload)
end end
catch catch
Class:Reason -> Class:Reason ->
Expand All @@ -80,8 +77,6 @@ terminate(_Handler, _Req, _State) ->
handle_call(Module, Req, #tavern{handlers = Handlers} = State) -> handle_call(Module, Req, #tavern{handlers = Handlers} = State) ->
{Method, Req} = cowboy_http_req:method(Req), {Method, Req} = cowboy_http_req:method(Req),
{Method, Handler} = lists:keyfind(Method, 1, Handlers), {Method, Handler} = lists:keyfind(Method, 1, Handlers),
error_logger:info_msg("handler_list: ~p~nmethod: ~p~nhandler: ~p~n",
[Handlers, Method, Handler]),
case erlang:function_exported(Module, Handler, 2) of case erlang:function_exported(Module, Handler, 2) of
true -> Module:Handler(Req, State); true -> Module:Handler(Req, State);
false -> erlang:error({no_export, Module, Handler}) false -> erlang:error({no_export, Module, Handler})
Expand All @@ -96,7 +91,6 @@ handle_resp(Req, State, Status, Payload) when is_atom(Status) ->
handle_resp(Req, #tavern{accept = Accept} = State, Status, Payload) -> handle_resp(Req, #tavern{accept = Accept} = State, Status, Payload) ->
try try
{ok, EncodedPayload} = tavern_marshal:encode(Accept, Payload), {ok, EncodedPayload} = tavern_marshal:encode(Accept, Payload),
error_logger:info_msg("abc: ~p~n", [EncodedPayload]),
A = cowboy_http_req:reply(Status, [], EncodedPayload, Req), A = cowboy_http_req:reply(Status, [], EncodedPayload, Req),
{ok, Resp} = A, {ok, Resp} = A,
{ok, Resp, State} {ok, Resp, State}
Expand All @@ -109,7 +103,7 @@ handle_resp(Req, #tavern{accept = Accept} = State, Status, Payload) ->
"** stacktrace:~n~p~n", "** stacktrace:~n~p~n",
[Module, Class, Reason, erlang:get_stacktrace()]), [Module, Class, Reason, erlang:get_stacktrace()]),
{ok, Resp2} = cowboy_http_req:reply(status('Internal Server Error'), {ok, Resp2} = cowboy_http_req:reply(status('Internal Server Error'),
[], <<"(error #1001:) could write response">>, [], <<"(error #1001:) could not write response">>,
Req), Req),
{ok, Resp2, State} {ok, Resp2, State}
end. end.
Expand Down
19 changes: 8 additions & 11 deletions src/tavern_req.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ consumed_type(Req, State) ->
{true, Req} -> {true, Req} ->
#tavern{consumes = TypeList} = State, #tavern{consumes = TypeList} = State,
{Mime1, Mime2, Charset} = content_type(Req), {Mime1, Mime2, Charset} = content_type(Req),
error_logger:info_msg("eats: ~p - ~p~n", [{Mime1, Mime2, Charset}, TypeList]),
case match_media_type({Mime1, Mime2, Charset}, TypeList) of case match_media_type({Mime1, Mime2, Charset}, TypeList) of
[] -> [] ->
{ {'Not Acceptable', [ { {'Not Acceptable', [
Expand Down Expand Up @@ -120,23 +119,21 @@ consumed_type(Req, State) ->
%% {true, Req, State}. %% {true, Req, State}.


decode_body(Req, #tavern{content_type = ContentType} = State) -> decode_body(Req, #tavern{content_type = ContentType} = State) ->
error_logger:info_msg("decode_body: ~p~n", [ContentType]),
{ok, Binary} = cowboy_http_req:body(1500, Req), {ok, Binary} = cowboy_http_req:body(1500, Req),
Payload = tavern_marshal:decode(ContentType, Binary), Payload = tavern_marshal:decode(ContentType, Binary),
{true, Req, State#tavern{body = Payload}, success}. {true, Req, State#tavern{body = Payload}, success}.


call(Req, State, Fun) when is_function(Fun) -> call(Req, State, Fun) when is_function(Fun) ->
case Fun(Req, State) of call(Req, State, Fun, success).
{true, Req2, State2, success} -> {true, Req2, State2};
{true, Req2, State2, Next} -> call(Req2, State2, Next);
{{_, _} = Result, Req2, State2} -> {Result, Req2, State2}
end.


call(Req, State, Fun, Next) when is_function(Fun) -> call(Req, State, Fun, Next) when is_function(Fun) ->
case Fun(Req, State, Next) of case Fun(Req, State) of
{true, Req2, State2, success} -> {true, Req2, State2}; {true, Req2, State2, success} ->
{true, Req2, State2, Next2} -> call(Req2, State2, Next, Next2); {true, Req2, State2};
{{_, _} = Result, Req2, State2} -> {Result, Req2, State2} {true, Req2, State2, Next} ->
call(Req2, State2, Next);
{{_, _} = Result, Req2, State2} ->
{Result, Req2, State2}
end. end.


%% Private API %% Private API
Expand Down

0 comments on commit f72811b

Please sign in to comment.