Current section
Files
Jump to
Current section
Files
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,
query_what_if_forecast/2,
query_what_if_forecast/3]).
-include_lib("hackney/include/hackney_lib.hrl").
%% Example:
%% data_point() :: #{
%% <<"Timestamp">> => string(),
%% <<"Value">> => float()
%% }
-type data_point() :: #{binary() => any()}.
%% Example:
%% forecast() :: #{
%% <<"Predictions">> => map()
%% }
-type forecast() :: #{binary() => any()}.
%% Example:
%% invalid_input_exception() :: #{
%% <<"Message">> => string()
%% }
-type invalid_input_exception() :: #{binary() => any()}.
%% Example:
%% invalid_next_token_exception() :: #{
%% <<"Message">> => string()
%% }
-type invalid_next_token_exception() :: #{binary() => any()}.
%% Example:
%% limit_exceeded_exception() :: #{
%% <<"Message">> => string()
%% }
-type limit_exceeded_exception() :: #{binary() => any()}.
%% Example:
%% query_forecast_request() :: #{
%% <<"EndDate">> => string(),
%% <<"Filters">> := map(),
%% <<"ForecastArn">> := string(),
%% <<"NextToken">> => string(),
%% <<"StartDate">> => string()
%% }
-type query_forecast_request() :: #{binary() => any()}.
%% Example:
%% query_forecast_response() :: #{
%% <<"Forecast">> => forecast()
%% }
-type query_forecast_response() :: #{binary() => any()}.
%% Example:
%% query_what_if_forecast_request() :: #{
%% <<"EndDate">> => string(),
%% <<"Filters">> := map(),
%% <<"NextToken">> => string(),
%% <<"StartDate">> => string(),
%% <<"WhatIfForecastArn">> := string()
%% }
-type query_what_if_forecast_request() :: #{binary() => any()}.
%% Example:
%% query_what_if_forecast_response() :: #{
%% <<"Forecast">> => forecast()
%% }
-type query_what_if_forecast_response() :: #{binary() => any()}.
%% Example:
%% resource_in_use_exception() :: #{
%% <<"Message">> => string()
%% }
-type resource_in_use_exception() :: #{binary() => any()}.
%% Example:
%% resource_not_found_exception() :: #{
%% <<"Message">> => string()
%% }
-type resource_not_found_exception() :: #{binary() => any()}.
-type query_forecast_errors() ::
resource_not_found_exception() |
resource_in_use_exception() |
limit_exceeded_exception() |
invalid_next_token_exception() |
invalid_input_exception().
-type query_what_if_forecast_errors() ::
resource_not_found_exception() |
resource_in_use_exception() |
limit_exceeded_exception() |
invalid_next_token_exception() |
invalid_input_exception().
%%====================================================================
%% 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:
%% https://docs.aws.amazon.com/en_us/forecast/latest/dg/API_CreateForecastExportJob.html
%% operation.
%%
%% The forecasts generated by Amazon Forecast are in the same timezone as the
%% dataset that was
%% used to create the predictor.
-spec query_forecast(aws_client:aws_client(), query_forecast_request()) ->
{ok, query_forecast_response(), tuple()} |
{error, any()} |
{error, query_forecast_errors(), tuple()}.
query_forecast(Client, Input)
when is_map(Client), is_map(Input) ->
query_forecast(Client, Input, []).
-spec query_forecast(aws_client:aws_client(), query_forecast_request(), proplists:proplist()) ->
{ok, query_forecast_response(), tuple()} |
{error, any()} |
{error, query_forecast_errors(), tuple()}.
query_forecast(Client, Input, Options)
when is_map(Client), is_map(Input), is_list(Options) ->
request(Client, <<"QueryForecast">>, Input, Options).
%% @doc Retrieves a what-if forecast.
-spec query_what_if_forecast(aws_client:aws_client(), query_what_if_forecast_request()) ->
{ok, query_what_if_forecast_response(), tuple()} |
{error, any()} |
{error, query_what_if_forecast_errors(), tuple()}.
query_what_if_forecast(Client, Input)
when is_map(Client), is_map(Input) ->
query_what_if_forecast(Client, Input, []).
-spec query_what_if_forecast(aws_client:aws_client(), query_what_if_forecast_request(), proplists:proplist()) ->
{ok, query_what_if_forecast_response(), tuple()} |
{error, any()} |
{error, query_what_if_forecast_errors(), tuple()}.
query_what_if_forecast(Client, Input, Options)
when is_map(Client), is_map(Input), is_list(Options) ->
request(Client, <<"QueryWhatIfForecast">>, 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, Input, Options) ->
RequestFun = fun() -> do_request(Client, Action, Input, Options) end,
aws_request:request(RequestFun, Options).
do_request(Client, Action, Input0, Options) ->
Client1 = Client#{service => <<"forecast">>},
DefaultHost = build_host(<<"forecastquery">>, Client1),
{URL, Host} = aws_util:apply_endpoint_url_override(build_url(DefaultHost, Client1), DefaultHost, <<"/">>, <<"AWS_ENDPOINT_URL_AWS_FORECASTQUERY">>),
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 = aws_client:proto(Client),
Port = aws_client:port(Client),
aws_util:binary_join([Proto, <<"://">>, Host, <<":">>, Port, <<"/">>], <<"">>).