예제 #1
0
        /// <summary>
        /// Returns the canonicalized resource path for the service endpoint
        /// </summary>
        /// <param name="endpointUri">Endpoint to the service/resource</param>
        /// <returns>Canonicalized resource path for the endpoint</returns>
        protected string CanonicalResourcePath(Uri endpointUri)
        {
            if (string.IsNullOrEmpty(endpointUri.AbsolutePath))
            {
                return("/");
            }

            // encode the path per RFC3986
            return(HttpHelpers.UrlEncode(endpointUri.AbsolutePath, true));
        }
예제 #2
0
        public async Task <T> Request <T>(string endpoint, HttpMethod method, string body = null)
        {
            if (this.TemporaryCredentials == null || DateTime.UtcNow > this.TemporaryCredentials.Expiration)
            {
                this.TemporaryCredentials = await this.GetTemporaryCredentials();
            }

            var httpMethod = method.ToString().ToUpper();
            var uri        = new Uri($"https://{_baseUrl}/v3/{endpoint}");
            var headers    = new Dictionary <string, string>
            {
                { "content-type", "application/json" },
                { "x-amz-security-token", this.TemporaryCredentials.SessionToken },
                { "x-api-key", _apiKey }
            };

            var bodyHash = AWS4SignerBase.EMPTY_BODY_SHA256;

            if (body != null)
            {
                var encodedJsonBytes = Encoding.UTF8.GetBytes(body);
                bodyHash = ComputeSHA256Hash(encodedJsonBytes);
            }

            //Use the AWS4 signer to generate the signer and sign the request
            var signer = new AWS4SignerForAuthorizationHeader
            {
                EndpointUri = uri,
                HttpMethod  = httpMethod,
                Service     = "execute-api",
                Region      = "us-east-1"
            };

            var authorization = signer.ComputeSignature(headers, "", bodyHash, this.TemporaryCredentials.AccessKey, this.TemporaryCredentials.SecretKey);

            headers.Add("Authorization", authorization);

            var response = HttpHelpers.InvokeHttpRequest(uri, httpMethod, headers, body);

            if (response != null)
            {
                return(JsonConvert.DeserializeObject <T>(response));
            }

            return(default(T));
        }