private static Tuple MakeSingleCall()

in AdlsDotNetSDK/WebTransport.cs [826:935]


        private static Tuple<byte[], int> MakeSingleCall(string opCode, string path, ByteBuffer requestData, ByteBuffer responseData, QueryParams qp, AdlsClient client, RequestOptions req, OperationResponse resp, IDictionary<string, string> customHeaders)
        {
            string token = null;
            Operation op = Operation.Operations[opCode];
            string urlString = CreateHttpRequestUrl(op, path, client, resp, qp.Serialize(opCode), req);
            if (string.IsNullOrEmpty(urlString))
            {
                return null;
            }

            try
            {
                // Create does not throw WebException
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(urlString);

                // If security certificate is used then no need to pass token
                if (req.ClientCert != null)
                {
                    webReq.ClientCertificates.Add(req.ClientCert);
                }

                Stopwatch watch = Stopwatch.StartNew();
                token = client.GetTokenAsync().GetAwaiter().GetResult();
                watch.Stop();
                resp.TokenAcquisitionLatency = watch.ElapsedMilliseconds;
                if (string.IsNullOrEmpty(token))
                {
                    resp.Ex = new ArgumentException($"Token is null or empty.");
                    return null;
                }

                if (token.Length <= AuthorizationHeaderLengthThreshold)
                {
                    resp.Ex = new ArgumentException($"Token Length is {token.Length}. Token is most probably malformed.");
                    return null;
                }

                resp.AuthorizationHeaderLength = token.Length;
                AssignCommonHttpHeaders(webReq, client, req, token, op.Method, customHeaders, requestData.Count);
                using (CancellationTokenSource timeoutCancellationTokenSource = GetCancellationTokenSourceForTimeout(req))
                {
                    try
                    {
                        //This point onwards if operation is cancelled http request is aborted
                        timeoutCancellationTokenSource.Token.Register(OnCancel, webReq);
                        if (!op.Method.Equals("GET"))
                        {
                            if (op.RequiresBody && requestData.Data != null)
                            {
                                using (Stream ipStream = GetCompressedStream(webReq.GetRequestStream(), client, requestData.Count))
                                {
                                    ipStream.Write(requestData.Data, requestData.Offset, requestData.Count);
                                }
                            }
                            else
                            {
                                SetWebRequestContentLength(webReq, 0);
                            }
                        }
                        using (var webResponse = (HttpWebResponse)webReq.GetResponse())
                        {
                            resp.HttpStatus = webResponse.StatusCode;
                            resp.HttpMessage = webResponse.StatusDescription;
                            resp.RequestId = webResponse.Headers["x-ms-request-id"];
                            PostPowershellLogDetails(webReq, webResponse);
                            if (op.ReturnsBody)
                            {

                                if (!InitializeResponseData(webResponse, ref responseData))
                                {
                                    return null;
                                }

                                int totalBytes = 0;
                                using (Stream opStream = webResponse.GetResponseStream())
                                {

                                    int noBytes;
                                    int totalLengthToRead = responseData.Count;
                                    //Read the required amount of data. In case of chunked it is what users requested, else it is amount of data sent
                                    do
                                    {
                                        noBytes = opStream.Read(responseData.Data, responseData.Offset, totalLengthToRead);
                                        totalBytes += noBytes;
                                        responseData.Offset += noBytes;
                                        totalLengthToRead -= noBytes;

                                    } while (noBytes > 0 && totalLengthToRead > 0);
                                }

                                return
                                    Tuple.Create(responseData.Data,
                                        totalBytes); //Return the total bytes read also since in case of chunked amount of data returned can be less than data returned
                            }
                        }
                    }
                    catch (WebException e)
                    {
                        HandleWebException(e, resp, path, req.RequestId, token, webReq, timeoutCancellationTokenSource.Token);
                    }
                }
            }// Any unhandled exception is caught here
            catch (Exception e)
            {
                resp.Ex = e;

            }

            return null;
        }