Esempio n. 1
0
        public static T CreateErrorResponse <T>(LastFmApiError error) where T : LastResponse, new()
        {
            var r = new T
            {
                Success = false,
                Error   = error
            };

            return(r);
        }
Esempio n. 2
0
        public static PageResponse <T> CreateErrorResponse(LastFmApiError error)
        {
            var r = new PageResponse <T>
            {
                Success = false,
                Error   = error
            };

            r.AddDefaultPageInfo();

            return(r);
        }
Esempio n. 3
0
        /// <summary>
        /// TODO see issue #5
        /// </summary>
        /// <param name="json">String of JSON</param>
        /// <param name="error">Enum indicating the error, .None if there is no error</param>
        /// <returns>True when the JSON could be parsed and it didn't describe a known Last.Fm error.</returns>
        public static bool IsResponseValid(string json, out LastFmApiError error)
        {
            if (string.IsNullOrWhiteSpace(json))
            {
                error = LastFmApiError.Unknown;
                return(false);
            }

            JObject jo;

            try
            {
                jo = JsonConvert.DeserializeObject <JObject>(json);
            }
            catch (JsonException)
            {
                error = LastFmApiError.Unknown;
                return(false);
            }

            var codeString = jo.Value <string>("error");

            if (string.IsNullOrWhiteSpace(codeString) && json.Length > 1)
            {
                error = LastFmApiError.None;
                return(true);
            }

            error = LastFmApiError.Unknown;

            int code;

            if (Int32.TryParse(codeString, out code))
            {
                switch (code)
                {
                case 2:
                    error = LastFmApiError.ServiceServiceWhereArtThou;
                    break;

                case 3:
                    error = LastFmApiError.BadMethod;
                    break;

                case 4:
                    error = LastFmApiError.BadAuth;
                    break;

                case 5:
                    error = LastFmApiError.BadFormat;
                    break;

                case 6:
                    error = LastFmApiError.MissingParameters;
                    break;

                case 7:
                    error = LastFmApiError.BadResource;
                    break;

                case 8:
                    error = LastFmApiError.Failure;
                    break;

                case 9:
                    error = LastFmApiError.SessionExpired;
                    break;

                case 10:
                    error = LastFmApiError.BadApiKey;
                    break;

                case 11:
                    error = LastFmApiError.ServiceDown;
                    break;

                case 13:
                    error = LastFmApiError.BadMethodSignature;
                    break;

                case 16:
                    error = LastFmApiError.TemporaryFailure;
                    break;

                case 26:
                    error = LastFmApiError.KeySuspended;
                    break;

                case 29:
                    error = LastFmApiError.RateLimited;
                    break;
                }
            }

            return(false);
        }