private void ProcessResponse()

in src/PFXImportPowershell/PFXImportPS/cmdlets/ImportUserPFXCertificate.cs [218:305]


        private void ProcessResponse(HttpWebRequest request, UserPFXCertificate cert)
        {
            bool needsRetry = false;
            TimeSpan waitTime = TimeSpan.Zero;
            double retryAfter = 60; // TODO: get a good default wait time.
            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    if ((int)response.StatusCode >= 200 && (int)response.StatusCode <= 299)
                    {
                        successCnt++;
                    }
                    else
                    {
                        string responseMessage;
                        using (var rawStream = response.GetResponseStream())
                        using (var responseReader = new StreamReader(rawStream, Encoding.UTF8))
                        {
                            responseMessage = responseReader.ReadToEnd();
                        }

                        failureCnt++;

                        this.WriteError(
                            new ErrorRecord(
                                new InvalidOperationException(string.Format(LogMessages.ImportCertificateFailureWithThumbprint, cert.Thumbprint, response.StatusCode, Environment.NewLine, responseMessage)),
                                "Import Failure:" + responseMessage,
                                ErrorCategory.WriteError,
                                cert));
                    }
                }
            }
            catch (WebException we)
            {
                HttpWebResponse response = we.Response as HttpWebResponse;
                if (we.Status == WebExceptionStatus.ProtocolError && response.StatusCode == (HttpStatusCode)429)
                {
                    needsRetry = true;
                    if (response.Headers["x-ms-retry-after-ms"] != null)
                    {
                        retryAfter = double.Parse(response.Headers["x-ms-retry-after-ms"]);
                    }

                    if (response.Headers["Retry-After"] != null)
                    {
                        retryAfter = double.Parse(response.Headers["Retry-After"]);
                    }
                }
                else
                {

                    failureCnt++;

                    var resp = new StreamReader(we.Response.GetResponseStream()).ReadToEnd();

                    dynamic obj = JsonConvert.DeserializeObject(resp);

                    string messageFromServer;
                    if (obj.error != null)
                    {
                        messageFromServer = obj.error.message.ToString();
                    }
                    else
                    {
                        messageFromServer = String.Format("Failed to deserialize response {0}", resp);
                    }

                    this.WriteDebug(string.Format("Error Message: {0}", messageFromServer));

                    this.WriteError(
                        new ErrorRecord(
                            we,
                            "\n\n Error Message" + messageFromServer + "\n\n request-id:" + we.Response.Headers["request-id"],
                            ErrorCategory.WriteError,
                            cert));
                    
                }
            }

            // Waiting until response is closed to re-use request
            if (needsRetry)
            {
                this.WriteWarning(string.Format(LogMessages.GetUserPfxTooManyRequests, retryAfter));
                Thread.Sleep(TimeSpan.FromSeconds(retryAfter));
                ProcessResponse(request, cert);
            }
        }