<commit_before>-module(aehttp_client).

-export([
         request/3,
         request/7
        ]).

%%=============================================================================
%% API
%%=============================================================================

%% Required config values: int_http, ext_http
request(OpId, Params, Cfg) ->
    Op = endpoints:operation(OpId),
    {Method, Interface} = operation_spec(Op),
    BaseUrl = make_base_url(proplists:get_value(Interface, Cfg)),
    Path = operation_path(Method, OpId, convert_params(Params)),
    request(Method, BaseUrl, Path, Params, [], [], []).

request(get, BaseUrl, Path, QueryParams, Headers, HttpOpts, Opts) ->
    Url = make_url(BaseUrl, Path, QueryParams),
    ct:log("GET ~p", [Url]),
    Resp = httpc:request(get, {Url, Headers}, HttpOpts, Opts),
    process_response(Resp);
request(post, BaseUrl, Path, BodyParams, Headers, HttpOpts, Opts) ->
    Url = make_url(BaseUrl, Path, #{}),
    {ContentType, Body} = make_body(BodyParams, Path),
    ct:log("POST ~p~nContentType ~p~n~p", [Url, ContentType, BodyParams]),
    Resp = httpc:request(post, {Url, Headers, ContentType, Body}, HttpOpts, Opts),
    process_response(Resp).

%%=============================================================================
%% Internal functions
%%=============================================================================

process_response({ok, {{_, Code, _State}, _Head, Body}}) ->
    try
        case iolist_to_binary(Body) of
            <<>> ->
                ct:log("Return code: ~p", [Code]),
                {ok, Code, #{}};
            B ->
                B1 = jsx:decode(B, [return_maps]),
                ct:log("Return code: ~p, body: ~p", [Code, B1]),
                {ok, Code, B1}
        end
    catch
        error:E -> {error, {parse_error, [E, erlang:get_stacktrace()]}}
    end;
process_response({error, _} = Error) ->
    Error.
<commit_msg>Fix decoding of response body<commit_after>-module(aehttp_client).

-export([
         request/3,
         request/7
        ]).

%%=============================================================================
%% API
%%=============================================================================

%% Required config values: int_http, ext_http
request(OpId, Params, Cfg) ->
    Op = endpoints:operation(OpId),
    {Method, Interface} = operation_spec(Op),
    BaseUrl = make_base_url(proplists:get_value(Interface, Cfg)),
    Path = operation_path(Method, OpId, convert_params(Params)),
    request(Method, BaseUrl, Path, Params, [], [], []).

request(get, BaseUrl, Path, QueryParams, Headers, HttpOpts, Opts) ->
    Url = make_url(BaseUrl, Path, QueryParams),
    ct:log("GET ~p", [Url]),
    Resp = httpc:request(get, {Url, Headers}, HttpOpts, Opts),
    process_response(Resp);
request(post, BaseUrl, Path, BodyParams, Headers, HttpOpts, Opts) ->
    Url = make_url(BaseUrl, Path, #{}),
    {ContentType, Body} = make_body(BodyParams, Path),
    ct:log("POST ~p~nContentType ~p~n~p", [Url, ContentType, BodyParams]),
    Resp = httpc:request(post, {Url, Headers, ContentType, Body}, HttpOpts, Opts),
    process_response(Resp).

%%=============================================================================
%% Internal functions
%%=============================================================================

process_response({ok, {{_, Code, _State}, _Head, Body}}) ->
    case iolist_to_binary(Body) of
        <<>> ->
            ct:log("Return code: ~p", [Code]),
            {ok, Code, #{}};
        Body1 ->
            Body2 = jsx:decode(Body1, [{labels, attempt_atom}, return_maps]),
            ct:log("Return code: ~p, body: ~p", [Code, Body2]),
            {ok, Code, Body2}
    end;
process_response({error, _} = Error) ->
    Error.

{'fraction_non_alphanumeric': 0.30616565228896536, 'fraction_numerical': 0.0031754432389521038, 'mean_word_length': 4.033288948069241, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '649990', 'n_tokens_mistral': 1152, 'n_tokens_neox': 1094, 'n_words': 240}