Exemplo n.º 1
0
        /// <summary>
        /// Invokes the asynchronous generic.
        /// </summary>
        /// <typeparam name="TReturnType">The type of the return type.</typeparam>
        /// <param name="method">The method.</param>
        /// <param name="args">The arguments.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override async Task <TReturnType> InvokeAsyncGeneric <TReturnType>(MethodInfo method, object[] args)
        {
            //URI to invoke lambda function
            var requestUri = new Uri($"https://lambda.{_region}.amazonaws.com/2015-03-31/functions/{_functionName}/invocations");

            //Serialize the request
            var serializedRequest = SerializeRequest(method, args);
            var lambdaRequest     = new LambdaRequest(_serviceMap.GetKey <TContract>(), serializedRequest);
            var lambdaRequestJosn = JsonConvert.SerializeObject(lambdaRequest);

            //Send request to lambda function
            var content  = new StringContent(lambdaRequestJosn, Encoding.UTF8, "binary/octet-stream");
            var response = await _lambdaClient.PostAsync(requestUri, content, _region, "lambda", _credentials);

            var responseString = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                throw new Exception($"There was an error trying to invoke the function. Make sure credentials, roles, region, and the function name are all set correctly. \n{responseString}");
            }


            var base64response = responseString.Trim('"');

            //Get the serialized byte array
            var responseBytes = Convert.FromBase64String(base64response);

            //Deserialize the request and throw if exception
            var deserializedObject = _serializer.ResponseSerializer.DeserializeResponse <TReturnType>(method, responseBytes);

            return(deserializedObject);
        }
        public override async Task <TReturnType> InvokeAsyncGeneric <TReturnType>(MethodInfo method, object[] args)
        {
            var serializedRequest = SerializeRequest(method, args);
            var lambdaRequest     = new LambdaRequest(_serviceMap.GetKey <TContract>(), serializedRequest);

            var serializedResponse = await _lambdaService.ExecuteRequest(lambdaRequest);

            var responseBytes = Convert.FromBase64String(serializedResponse);

            //This would happen at the client
            var deserializedObject = _serializer.ResponseSerializer.DeserializeResponse <TReturnType>(method, responseBytes);

            return(deserializedObject);
        }