Akamai.EdgeGrid.Auth.EdgeGridV1Signer.Execute C# (CSharp) Method

Execute() public method

Opens the connection to the {OPEN} API, assembles the signing headers and uploads any files.
public Execute ( WebRequest request, ClientCredential credential, Stream uploadStream = null ) : Stream
request System.Net.WebRequest the
credential ClientCredential
uploadStream Stream
return Stream
        public Stream Execute(WebRequest request, ClientCredential credential, Stream uploadStream = null)
        {
            //Make sure that this connection will behave nicely with multiple calls in a connection pool.
            ServicePointManager.EnableDnsRoundRobin = true;
            request = this.Sign(request, credential, uploadStream);

            if (request.Method == "PUT" || request.Method == "POST" || request.Method == "PATCH")
            {
                //Disable the nastiness of Expect100Continue
                ServicePointManager.Expect100Continue = false;
                if (uploadStream == null)
                    request.ContentLength = 0;
                else if (uploadStream.CanSeek)
                    request.ContentLength = uploadStream.Length;
                else if (request is HttpWebRequest)
                    ((HttpWebRequest)request).SendChunked = true;

                if (uploadStream != null)
                {
                    // avoid internal memory allocation before buffering the output
                    if (request is HttpWebRequest)
                        ((HttpWebRequest)request).AllowWriteStreamBuffering = false;

                    if (String.IsNullOrEmpty(request.ContentType))
                        request.ContentType = "application/json";

                    using (Stream requestStream = request.GetRequestStream())
                    using (uploadStream)
                        uploadStream.CopyTo(requestStream, 1024 * 1024);
                }
            }

            if (request is HttpWebRequest)
            {
                var httpRequest = (HttpWebRequest)request;
                httpRequest.Accept = "*/*";
                if (String.IsNullOrEmpty(httpRequest.UserAgent))
                    httpRequest.UserAgent = "EdgeGrid.Net/v1";
            }

            WebResponse response = null;
            try
            {
                response = request.GetResponse();
            }
            catch (WebException e)
            {
                // non 200 OK responses throw exceptions.
                // is this because of Time drift? can we re-try?
                using (response = e.Response)
                    Validate(response);
            }

            return response.GetResponseStream();
        }

Usage Example

        public void TestAPIActionExecute()
        {
            string clientToken = "clientToken";
            string accessToken = "accessToken";
            string secret      = "secret-shh";
            var    credential  = new ClientCredential(clientToken, accessToken, secret);

            var    signer          = new EdgeGridV1Signer();
            string TestURIProtocol = "asdf";

            WebRequest.RegisterPrefix(TestURIProtocol, new WebRequestTestCreate());
            var request = (HttpWebRequestTest)WebRequest.Create("asdf://www.example.com/");

            var response = request.CreateResponse(HttpStatusCode.OK);

            request.NextResponse = response;

            Assert.AreSame(signer.Execute(request, credential), response.GetResponseStream());
            Assert.AreEqual(request.Method, "GET");
            Assert.AreEqual(request.Headers.Count, 1);

            request.Method = "PUT";
            signer.Execute(request, credential);
            Assert.AreEqual(request.ContentLength, 0);

            request.Method = "POST";
            var data         = "Lorem ipsum dolor sit amet, an sea putant quaeque, homero aperiam te eos.".ToByteArray();
            var uploadStream = new MemoryStream(data);

            signer.Execute(request, credential, uploadStream);
            Assert.AreEqual(request.ContentLength, 73);
            CollectionAssert.AreEqual(request.RequestStream.ToArray(), data);
        }
All Usage Examples Of Akamai.EdgeGrid.Auth.EdgeGridV1Signer::Execute