Ejemplo n.º 1
0
        public static void UpdateDisplayName(AbstractLogger logger, IMixWebCallFactory mixWebCallFactory, string displayName, Action <IUpdateDisplayNameResult> callback)
        {
            try
            {
                SetDisplayNameRequest setDisplayNameRequest = new SetDisplayNameRequest();
                setDisplayNameRequest.DisplayName = displayName;
                SetDisplayNameRequest request = setDisplayNameRequest;
                IWebCall <SetDisplayNameRequest, SetDisplayNameResponse> webCall = mixWebCallFactory.DisplaynamePut(request);
                webCall.OnResponse += delegate(object sender, WebCallEventArgs <SetDisplayNameResponse> e)
                {
                    SetDisplayNameResponse response = e.Response;
                    if (ValidateSetDisplayNameResponse(response))
                    {
                        callback(new UpdateDisplayNameResult(success: true));
                    }
                    else
                    {
                        logger.Critical("Failed to validate update display name response!");
                        callback(new UpdateDisplayNameResult(success: false));
                    }
                };
                webCall.OnError += delegate(object sender, WebCallErrorEventArgs e)
                {
                    switch (e.Status)
                    {
                    case "DISPLAYNAME_MODERATION_FAILED":
                        logger.Warning("Failed to update display name due to moderation failure: " + e.Message);
                        callback(new UpdateDisplayNameFailedModerationResult(success: false));
                        break;

                    case "DISPLAYNAME_ASSIGNMENT_FAILED":
                        logger.Warning("Failed to update display name due to assignment failure: " + e.Message);
                        callback(new UpdateDisplayNameExistsResult(success: false));
                        break;

                    default:
                        callback(new UpdateDisplayNameResult(success: false));
                        break;
                    }
                };
                webCall.Execute();
            }
            catch (Exception ex)
            {
                logger.Critical("Unhandled exception: " + ex);
                callback(new UpdateDisplayNameResult(success: false));
            }
        }
Ejemplo n.º 2
0
        private void HandleWwwDone(object sender, WwwDoneEventArgs e)
        {
            wwwCall.OnDone -= HandleWwwDone;
            bool   flag  = false;
            string error = wwwCall.Error;

            if (!string.IsNullOrEmpty(error))
            {
                string text = error.ToLower();
                flag = text == "couldn't connect to host" || text.Contains("timedout") || text.Contains("timed out");
            }
            if (flag)
            {
                string responsePlaintext = GetResponsePlaintext(wwwCall.ResponseBody);
                string text2             = HttpLogBuilder.BuildTimeoutLog(wwwCall.RequestId, uri, method, wwwCall.ResponseHeaders, responsePlaintext, wwwCall.TimeToStartUpload, wwwCall.TimeToFinishUpload, wwwCall.PercentUploaded, wwwCall.TimeToStartDownload, wwwCall.TimeToFinishDownload, wwwCall.PercentDownloaded, wwwCall.TimeoutReason, wwwCall.TimeoutTime);
                timeoutLogs.Append(text2);
                timeoutLogs.Append("\n\n");
                logger.Error(text2);
                wwwCall.Dispose();
                if (numAttempts > 3)
                {
                    logger.Critical("Too many timeouts: " + uri.AbsoluteUri + "\nPrevious logs:\n" + timeoutLogs);
                    DispatchError("Too many timeouts");
                }
                else
                {
                    Execute();
                }
                return;
            }
            uint statusCode = wwwCall.StatusCode;

            if (statusCode >= 500 && statusCode <= 599)
            {
                string responsePlaintext = GetResponsePlaintext(wwwCall.ResponseBody);
                string text2             = HttpLogBuilder.BuildResponseLog(wwwCall.RequestId, uri, method, wwwCall.ResponseHeaders, responsePlaintext, statusCode);
                wwwCall.Dispose();
                if (numAttempts > 1)
                {
                    logger.Critical("Too many server errors:\n" + text2);
                    DispatchError(statusCode + " Server error: " + wwwCall.Error);
                }
                else
                {
                    logger.Error(text2);
                    Execute();
                }
                return;
            }
            bool flag2 = statusCode >= 200 && statusCode <= 299;

            if (statusCode >= 400 && statusCode <= 499)
            {
                string responsePlaintext;
                try
                {
                    responsePlaintext = GetResponseText(wwwCall.ResponseBody);
                }
                catch (Exception ex)
                {
                    logger.Warning("Text was not encrypted, error: " + ex);
                    responsePlaintext = GetResponsePlaintext(wwwCall.ResponseBody);
                }
                if (statusCode == 429)
                {
                    this.OnError(this, new WebCallErrorEventArgs(statusCode + " Too many requests", "THROTTLED"));
                    return;
                }
                string status = GetStatus(responsePlaintext);
                if (statusCode == 401)
                {
                    logger.Debug(HttpLogBuilder.BuildResponseLog(wwwCall.RequestId, uri, method, wwwCall.ResponseHeaders, responsePlaintext, statusCode));
                    wwwCall.Dispose();
                    this.OnUnauthorized(this, new WebCallUnauthorizedEventArgs(status, responsePlaintext));
                }
                else
                {
                    logger.Error(HttpLogBuilder.BuildResponseLog(wwwCall.RequestId, uri, method, wwwCall.ResponseHeaders, responsePlaintext, statusCode));
                    this.OnError(this, new WebCallErrorEventArgs(statusCode + " Client error = " + wwwCall.Error, status));
                    wwwCall.Dispose();
                }
            }
            else if (flag2)
            {
                string responsePlaintext;
                try
                {
                    responsePlaintext = GetResponseText(wwwCall.ResponseBody);
                }
                catch (Exception ex)
                {
                    string responsePlaintext2 = GetResponsePlaintext(wwwCall.ResponseBody);
                    logger.Critical(string.Concat("Error getting response body:\n", ex, "\n", HttpLogBuilder.BuildResponseLog(wwwCall.RequestId, uri, method, wwwCall.ResponseHeaders, responsePlaintext2, statusCode)));
                    DispatchError(statusCode + " Couldn't get response body text");
                    wwwCall.Dispose();
                    return;
                }
                logger.Debug(HttpLogBuilder.BuildResponseLog(wwwCall.RequestId, uri, method, wwwCall.ResponseHeaders, responsePlaintext, statusCode));
                TResponse val = null;
                try
                {
                    val = JsonParser.FromJson <TResponse>(responsePlaintext);
                }
                catch (Exception ex)
                {
                    logger.Critical(string.Concat("Error parsing response body: ", ex, "\n", responsePlaintext));
                }
                if (val != null && val.Status == "OK")
                {
                    this.OnResponse(this, new WebCallEventArgs <TResponse>(val));
                }
                else
                {
                    DispatchError(statusCode + " Response JSON is invalid or indicates an error: " + responsePlaintext);
                }
                wwwCall.Dispose();
            }
            else
            {
                bool flag3 = false;
                if (!string.IsNullOrEmpty(error))
                {
                    string text = error.ToLower();
                    flag3 = text.Contains("connection appears to be offline") || text.Contains("connection was lost") || text.Contains("network is unreachable");
                }
                if (flag3)
                {
                    logger.Error("Offline error = " + wwwCall.Error);
                }
                else
                {
                    logger.Critical("Other web call error = " + wwwCall.Error);
                }
                DispatchError(statusCode + " Other web call error = " + wwwCall.Error);
                wwwCall.Dispose();
            }
        }