./ct_report/coverage/mongoose_http_client.COVER.html

1 %%==============================================================================
2 %% Copyright 2016 Erlang Solutions Ltd.
3 %%
4 %% Licensed under the Apache License, Version 2.0 (the "License");
5 %% you may not use this file except in compliance with the License.
6 %% You may obtain a copy of the License at
7 %%
8 %% http://www.apache.org/licenses/LICENSE-2.0
9 %%
10 %% Unless required by applicable law or agreed to in writing, software
11 %% distributed under the License is distributed on an "AS IS" BASIS,
12 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 %% See the License for the specific language governing permissions and
14 %% limitations under the License.
15 %%==============================================================================
16 %%%-------------------------------------------------------------------
17 %%% Created : 26. Jun 2018 13:07
18 %%%-------------------------------------------------------------------
19 -module(mongoose_http_client).
20 -author("bartlomiej.gorny@erlang-solutions.com").
21
22 %% API
23 -export([get/4, post/5]).
24
25 -spec get(Host :: jid:lserver() | global,
26 PoolTag :: atom(),
27 Path :: binary(),
28 Headers :: list()) ->
29 {ok, {binary(), binary()}} | {error, any()}.
30 get(Host, PoolTag, Path, Headers) ->
31 7 make_request(Host, PoolTag, Path, <<"GET">>, Headers, <<>>).
32
33 -spec post(Host :: jid:lserver() | global,
34 PoolTag :: atom(),
35 Path :: binary(),
36 Headers :: list(),
37 Query :: binary()) ->
38 {ok, {binary(), binary()}} | {error, any()}.
39 post(Host, PoolTag, Path, Headers, Query) ->
40
:-(
make_request(Host, PoolTag, Path, <<"POST">>, Headers, Query).
41
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43
44 make_request(Host, PoolTag, Path, Method, Headers, Query) ->
45 7 case mongoose_wpool_http:get_params(Host, PoolTag) of
46 {ok, PathPrefix, RequestTimeout} ->
47 7 make_request(Host, PoolTag, PathPrefix, RequestTimeout, Path, Method, Headers, Query);
48 Err ->
49
:-(
Err
50 end.
51
52 make_request(Host, PoolTag, PathPrefix, RequestTimeout, Path, Method, Headers, Query) ->
53 7 FullPath = <<PathPrefix/binary, Path/binary>>,
54 7 Req = {request, FullPath, Method, Headers, Query, 2, RequestTimeout},
55 7 try mongoose_wpool:call(http, Host, PoolTag, Req) of
56 {ok, {{Code, _Reason}, _RespHeaders, RespBody, _, _}} ->
57 5 {ok, {Code, RespBody}};
58 {error, timeout} ->
59
:-(
{error, request_timeout};
60 {'EXIT', Reason} ->
61
:-(
{error, {'EXIT', Reason}};
62 {error, Reason} ->
63 2 {error, Reason}
64 catch
65 exit:timeout ->
66
:-(
{error, pool_timeout};
67 exit:no_workers ->
68
:-(
{error, pool_down};
69 Type:Error ->
70
:-(
{error, {Type, Error}}
71 end.
Line Hits Source