Пример #1
0
        public static List <League> GetLeague(string CountryCode, ref string LastError)
        {
            try
            {
                lock (mLock)
                {
                    if (CashedLeagues == null)
                    {
                        CashedLeagues = new Dictionary <string, List <League> >();
                    }

                    if (CashedLeagues.ContainsKey(CountryCode))
                    {
                        return(CashedLeagues[CountryCode].ToList());
                    }
                    using (WebClient ws = new WebClient())
                    {
                        ws.Headers.Add("X-RapidAPI-Key", APIConfig.Get().Token);
                        string url  = APIConfig.Get().GetLeaguas + "/" + CountryCode;
                        string body = ws.DownloadString(url);

                        if (body == null)
                        {
                            LastError = "Отсутствует ответ от API сервера";
                            return(null);
                        }

                        ApiLeagues leagues = JsonConvert.DeserializeObject <ApiLeagues>(body);
                        if ((leagues == null) || (leagues.api == null))
                        {
                            LastError = "Отсутствует ответ от API сервера";
                            return(null);
                        }

                        if ((leagues.api.error ?? "") != "")
                        {
                            LastError = leagues.api.error;
                            return(null);
                        }

                        if (leagues.api.leagues != null)
                        {
                            CashedLeagues.Add(CountryCode, leagues.api.leagues);
                            return(leagues.api.leagues);
                        }
                        CashedLeagues.Add(CountryCode, new List <League>());

                        return(new List <League>());
                    }
                }
            }
            catch (Exception e)
            {
                LastError = e.Message;
                Console.WriteLine(e.Message);
                return(null);
            }
        }
Пример #2
0
        /// <summary>
        /// Вернуть все лиги в стране
        /// </summary>
        /// <param name="CountryCode">Идентификатор страны</param>
        public static List <League> GetLeagues(string CountryCode, ref string requestError)
        {
            requestError = null;
            List <League> answer = ApiLeagues.GetLeague(CountryCode, ref requestError);

            if (answer == null)
            {
                return(new List <League>());
            }
            return(answer);
        }