Esempio n. 1
0
        /// <summary>
        /// </summary>
        /// <param name="name"></param>
        /// <param name="region"></param>
        /// <returns>The last 10 games the summoner has played.</returns>
        public async Task<List<GameDTO>> GetSummonersRecentGamesAsync(Summoner summoner)
        {
            if (summoner == null)
            {
                throw new ArgumentNullException("summoner");
            }
            if (summoner.ID == 0)
            {
                throw new ArgumentException("Summoner ID should be a value larger than 0.");
            }

            string requestPath = string.Format("game/by-summoner/{0}/recent", summoner.ID);
            string url = BuildURL(summoner.Region, requestPath);

            using (HttpClient client = new HttpClient())
            using (HttpResponseMessage response = await client.GetAsync(url))
            using (HttpContent content = response.Content)
            {
                string contentStr = await content.ReadAsStringAsync();
                GameHistoryDTO gameHistoryDTO = await JsonConvert.DeserializeObjectAsync<GameHistoryDTO>(contentStr);
                if (gameHistoryDTO == null)
                {
                    return null;
                }
                return gameHistoryDTO.games;
            }
        }
        /// <summary>
        /// </summary>
        /// <param name="name"></param>
        /// <param name="region"></param>
        /// <returns>The ID of a Summoner from their name and region.</returns>
        public async Task<Summoner> FromNameAsync(string name, eRegion region)
        {
            string requestPath = string.Format("summoner/by-name/{0}", name);
            string url = BuildURL(region, requestPath);

            using (HttpClient client = new HttpClient())
            using (HttpResponseMessage response = await client.GetAsync(url))
            using (HttpContent content = response.Content)
            {
                string contentStr = await content.ReadAsStringAsync();
                var result = await JsonConvert.DeserializeObjectAsync<Dictionary<string, SummonerDTO>>(contentStr);
                SummonerDTO summonerDTO = result.Select(x => x.Value).FirstOrDefault();
                
                if (summonerDTO == null)
                {
                    return null;
                }
                //todo: cache summoners.
                Summoner summoner = new Summoner(summonerDTO);
                summoner.Region = region;
                return summoner;
            }
        }