Skip to content

Commit

Permalink
[#13] Just return the status code. Return boolean values for is_* fun…
Browse files Browse the repository at this point in the history
…ctions.
  • Loading branch information
jfacorro committed Oct 15, 2014
1 parent c57992c commit 7f35abe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/tirerl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ close_index(Destination, Index) when is_binary(Index) ->
is_index(Destination, Index) when is_binary(Index) ->
is_index(Destination, [Index]);
is_index(Destination, Indexes) when is_list(Indexes) ->
route_call(Destination, {is_index, Indexes}, infinity).
Result = route_call(Destination, {is_index, Indexes}, infinity),
boolean_result(Result).

%% @equiv count(Destination, ?ALL, [], Doc []).
-spec count(destination(), doc()) -> response().
Expand Down Expand Up @@ -377,7 +378,8 @@ is_type(Destination, Indexes, Type) when is_list(Indexes), is_binary(Type) ->
is_type(Destination, Index, Types) when is_binary(Index), is_list(Types) ->
is_type(Destination, [Index], Types);
is_type(Destination, Indexes, Types) when is_list(Indexes), is_list(Types) ->
route_call(Destination, {is_type, Indexes, Types}, infinity).
Result = route_call(Destination, {is_type, Indexes, Types}, infinity),
boolean_result(Result).

%% @equiv insert_doc(Destination, Index, Type, Id, Doc, []).
-spec insert_doc(destination(), index(), type(), id(), doc()) -> response().
Expand Down Expand Up @@ -425,7 +427,8 @@ update_doc(Destination, Index, Type, Id, Doc, Params)
-spec is_doc(destination(), index(), type(), id()) -> response().
is_doc(Destination, Index, Type, Id)
when is_binary(Index), is_binary(Type), is_binary(Id) ->
route_call(Destination, {is_doc, Index, Type, Id}, infinity).
Result = route_call(Destination, {is_doc, Index, Type, Id}, infinity),
boolean_result(Result).

%% @equiv get_doc(Destination, Index, Type, Id, []).
-spec get_doc(destination(), index(), type(), id()) -> response().
Expand Down Expand Up @@ -647,7 +650,8 @@ delete_alias(Destination, Index, Alias)
-spec is_alias(destination(), index(), index()) -> response().
is_alias(Destination, Index, Alias)
when is_binary(Index) andalso is_binary(Alias) ->
route_call(Destination, {is_alias, Index, Alias}, infinity).
Result = route_call(Destination, {is_alias, Index, Alias}, infinity),
boolean_result(Result).

%% @doc Gets an alias(or more, based on the string)
-spec get_alias(destination(), index(), index()) -> response().
Expand All @@ -666,3 +670,12 @@ get_alias(Destination, Index, Alias)
route_call(Name, Command, Timeout)
when is_atom(Name); is_pid(Name) ->
wpool:call(Name, Command, wpool:default_strategy(), Timeout).


-spec boolean_result({ok, integer()}) -> boolean().
boolean_result({ok, Status}) when Status < 400 ->
true;
boolean_result({ok, _Status}) ->
false;
boolean_result(Result) ->
Result.
8 changes: 8 additions & 0 deletions src/tirerl_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ do_request(Req, #{connection := Conn}) ->
process_response(Response)
catch
error:Reason ->
io:format("~p~n", [[Method, Uri, Body1, erlang:get_stacktrace()]]),
{error, Reason}
end.

Expand All @@ -139,6 +140,13 @@ process_response({ok, #{status_code := Status, body := Body} = Response}) ->
end
catch
error:badarg -> {error, Response}
end;
process_response({ok, #{status_code := Status} = Response}) ->
case Status of
Status when Status < 500 ->
{ok, Status};
_ ->
{error, Response}
end.

make_request({health}) ->
Expand Down

0 comments on commit 7f35abe

Please sign in to comment.