./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 -export_type([pool/0]).
26 -type pool() :: atom().
27
28 -spec get(Host :: jid:lserver() | global,
29 PoolTag :: pool(),
30 Path :: binary(),
31 Headers :: list()) ->
32 {ok, {binary(), binary()}} | {error, any()}.
33 get(Host, PoolTag, Path, Headers) ->
34 7 make_request(Host, PoolTag, Path, <<"GET">>, Headers, <<>>).
35
36 -spec post(Host :: jid:lserver() | global,
37 PoolTag :: pool(),
38 Path :: binary(),
39 Headers :: list(),
40 Query :: binary()) ->
41 {ok, {binary(), binary()}} | {error, any()}.
42 post(Host, PoolTag, Path, Headers, Query) ->
43 145 make_request(Host, PoolTag, Path, <<"POST">>, Headers, Query).
44
45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46
47 make_request(Host, PoolTag, Path, Method, Headers, Query) ->
48 152 case mongoose_wpool_http:get_params(Host, PoolTag) of
49 {ok, PathPrefix, RequestTimeout} ->
50 152 make_request(Host, PoolTag, PathPrefix, RequestTimeout, Path, Method, Headers, Query);
51 Err ->
52
:-(
Err
53 end.
54
55 make_request(Host, PoolTag, PathPrefix, RequestTimeout, Path, Method, Headers, Query) ->
56 152 FullPath = <<PathPrefix/binary, Path/binary>>,
57 152 Req = {request, FullPath, Method, Headers, Query, 2, RequestTimeout},
58 152 try mongoose_wpool:call(http, Host, PoolTag, Req) of
59 {ok, {{Code, _Reason}, _RespHeaders, RespBody, _, _}} ->
60 148 {ok, {Code, RespBody}};
61 {error, timeout} ->
62
:-(
{error, request_timeout};
63 {'EXIT', Reason} ->
64
:-(
{error, {'EXIT', Reason}};
65 {error, Reason} ->
66 4 {error, Reason}
67 catch
68 exit:timeout ->
69
:-(
{error, pool_timeout};
70 exit:no_workers ->
71
:-(
{error, pool_down};
72 Type:Error ->
73
:-(
{error, {Type, Error}}
74 end.
Line Hits Source