Esempio n. 1
0
        public static async Task <string> GetApiData(string endpoint)
        {
            var client   = new ApiRestClient(_url);
            var response = await client.GetAsync(endpoint);

            return(response);
        }
        public async virtual Task <RestResponse> MakeApiRequestAsync(string secretArn, RestRequest request, string requestId = "", string correlationId = "")
        {
            Guard.Against <ArgumentNullException>(String.IsNullOrEmpty(secretArn), DeploySoftware_LaunchPad_AWS_Resources.ApiGatewayHelper_SecretArn_Is_NullOrEmpty);
            Guard.Against <ArgumentNullException>(ApiRestClient == null, DeploySoftware_LaunchPad_AWS_Resources.ApiGatewayHelper_MakeApiGatewayRequest_RestClient_Is_Null);
            Guard.Against <ArgumentNullException>(String.IsNullOrEmpty(request.Resource), DeploySoftware_LaunchPad_AWS_Resources.ApiGatewayHelper_MakeApiGatewayRequest_Request_Resource_Is_NullOrEmpty);
            _logger.Info(string.Format(DeploySoftware_LaunchPad_AWS_Resources.Logger_Info_ExecuteApiGatewayRequest_Executing, request.Method.ToString(), ApiBaseUrl, request.Resource));

            if (Token == null)
            {
                Token = await GetOAuthTokenUsingSecretCredentialsAsync(secretArn);

                // TODO save the token in the secret
            }
            request.AddHeader("authorization", "Bearer " + Token);

            // add the Correlation ID header, if it is a request transaction
            if (!string.IsNullOrEmpty(correlationId))
            {
                request.AddHeader("X-Correlation-ID", correlationId);
            }

            // add a unique requestId header to the receiving service
            if (string.IsNullOrEmpty(requestId))
            {
                requestId = Guid.NewGuid().ToString();
            }
            request.AddHeader("X-Request-ID", requestId);

            // make the request to the API gateway
            RestResponse response = await ApiRestClient.ExecuteAsync(request);

            if (response.IsSuccessful)
            {
                _logger.Info(string.Format("Request succeeded. Status code: {0}. CorrelationId: {1}", response.StatusCode, correlationId));
            }
            else
            {
                _logger.Error(string.Format("Request failed with status code {0}. Reason: {1}. CorrelationId: {2}",
                                            response.StatusCode,
                                            response.ErrorException.Message,
                                            correlationId),
                              response.ErrorException)
                ;
            }
            _logger.Info(string.Format(DeploySoftware_LaunchPad_AWS_Resources.Logger_Info_ExecuteApiGatewayRequest_Executed, request.Method.ToString(), ApiBaseUrl, request.Resource, response.StatusCode));
            return(response);
        }