Current section

Files

Jump to
aws_erlang src aws_forecastquery.erl
Raw

src/aws_forecastquery.erl

%% WARNING: DO NOT EDIT, AUTO-GENERATED CODE!
%% See https://github.com/aws-beam/aws-codegen for more details.
%% @doc Provides APIs for creating and managing Amazon Forecast resources.
-module(aws_forecastquery).
-export([query_forecast/2,
query_forecast/3]).
-include_lib("hackney/include/hackney_lib.hrl").
%%====================================================================
%% API
%%====================================================================
%% @doc Retrieves a forecast for a single item, filtered by the supplied
%% criteria.
%%
%% The criteria is a key-value pair. The key is either `item_id' (or the
%% equivalent non-timestamp, non-target field) from the `TARGET_TIME_SERIES'
%% dataset, or one of the forecast dimensions specified as part of the
%% `FeaturizationConfig' object.
%%
%% By default, `QueryForecast' returns the complete date range for the
%% filtered forecast. You can request a specific date range.
%%
%% To get the full forecast, use the CreateForecastExportJob operation.
%%
%% The forecasts generated by Amazon Forecast are in the same timezone as the
%% dataset that was used to create the predictor.
query_forecast(Client, Input)
when is_map(Client), is_map(Input) ->
query_forecast(Client, Input, []).
query_forecast(Client, Input, Options)
when is_map(Client), is_map(Input), is_list(Options) ->
request(Client, <<"QueryForecast">>, Input, Options).
%%====================================================================
%% Internal functions
%%====================================================================
-spec request(aws_client:aws_client(), binary(), map(), list()) ->
{ok, Result, {integer(), list(), hackney:client()}} |
{error, Error, {integer(), list(), hackney:client()}} |
{error, term()} when
Result :: map() | undefined,
Error :: map().
request(Client, Action, Input0, Options) ->
Client1 = Client#{service => <<"forecast">>},
Host = build_host(<<"forecastquery">>, Client1),
URL = build_url(Host, Client1),
Headers = [
{<<"Host">>, Host},
{<<"Content-Type">>, <<"application/x-amz-json-1.1">>},
{<<"X-Amz-Target">>, <<"AmazonForecastRuntime.", Action/binary>>}
],
Input = Input0,
Payload = jsx:encode(Input),
SignedHeaders = aws_request:sign_request(Client1, <<"POST">>, URL, Headers, Payload),
Response = hackney:request(post, URL, SignedHeaders, Payload, Options),
handle_response(Response).
handle_response({ok, 200, ResponseHeaders, Client}) ->
case hackney:body(Client) of
{ok, <<>>} ->
{ok, undefined, {200, ResponseHeaders, Client}};
{ok, Body} ->
Result = jsx:decode(Body),
{ok, Result, {200, ResponseHeaders, Client}}
end;
handle_response({ok, StatusCode, ResponseHeaders, Client}) ->
{ok, Body} = hackney:body(Client),
Error = jsx:decode(Body),
{error, Error, {StatusCode, ResponseHeaders, Client}};
handle_response({error, Reason}) ->
{error, Reason}.
build_host(_EndpointPrefix, #{region := <<"local">>, endpoint := Endpoint}) ->
Endpoint;
build_host(_EndpointPrefix, #{region := <<"local">>}) ->
<<"localhost">>;
build_host(EndpointPrefix, #{region := Region, endpoint := Endpoint}) ->
aws_util:binary_join([EndpointPrefix, Region, Endpoint], <<".">>).
build_url(Host, Client) ->
Proto = maps:get(proto, Client),
Port = maps:get(port, Client),
aws_util:binary_join([Proto, <<"://">>, Host, <<":">>, Port, <<"/">>], <<"">>).