-- Copyright (c) 2013-2014 PivotCloud, Inc.
--
-- Aws.Lambda.Commands.InvokeAsync
--
-- Please feel free to contact us at licensing@pivotmail.com with any
-- contributions, additions, or other feedback; we would love to hear from
-- you.
--
-- Licensed under the Apache License, Version 2.0 (the "License"); you may
-- not use this file except in compliance with the License. You may obtain a
-- copy of the License at http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-- License for the specific language governing permissions and limitations
-- under the License.

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UnicodeSyntax #-}

module Aws.Lambda.Commands.InvokeAsync
( -- * Request
  InvokeAsync(..)
, invokeAsync
  -- ** Lenses
, iaFunctionName
, iaArguments

  -- * Response
, InvokeAsyncResponse(..)
) where

import Aws.Lambda.Core

import Control.Applicative
import Control.Lens
import Data.Aeson
import qualified Data.Text as T
import Network.HTTP.Types

-- | Submits an invocation request to AWS Lambda. Upon receiving the request,
-- Lambda executes the specified function asynchronously. To see the logs
-- generated by the Lambda function execution, see the CloudWatch logs console.
--
-- This operation requires permission for the @lambda:InvokeAsync@ action.
--
data InvokeAsync
  = InvokeAsync
  { _iaFunctionName  !T.Text
  , _iaArguments  !Value
  } deriving (Eq, Show)

makeLenses ''InvokeAsync

-- | A minimal 'InvokeAsync' request.
--
invokeAsync
   T.Text -- ^ '_iaFunctionName'
   InvokeAsync
invokeAsync fn = InvokeAsync fn $ object []

data InvokeAsyncResponse
  = InvokeAsyncResponse
  deriving (Eq, Show)

makeLenses ''InvokeAsyncResponse

instance FromJSON InvokeAsyncResponse where
  parseJSON _ = pure InvokeAsyncResponse

instance LambdaTransaction InvokeAsync Value InvokeAsyncResponse where
  buildQuery ia =
    lambdaQuery'
      POST
      ["functions", ia ^. iaFunctionName, "invoke-async"]
      (ia ^. iaArguments)