Пример #1
0
        public async Task <IActionResult> Search(string term)
        {
            ItunesSearchResult result =
                await _libraryService.Search(term, MediaEntity.Album);

            if (!result.IsSucceeded)
            {
                return(BadRequest());
            }

            return(Ok(result));
        }
Пример #2
0
        public async Task <ItunesSearchResult> Search(string term, MediaEntity entity)
        {
            ItunesSearchResult result = new ItunesSearchResult();

            var response = await _client
                           .GetAsync($"{_baseUrl}/search?term={term}&entity={entity.GetString()}");

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                result             = JsonConvert.DeserializeObject <ItunesSearchResult>(content);
                result.IsSucceeded = true;
            }

            return(result);
        }
Пример #3
0
        public static List <GroupedResult> Search(string Term)
        {
            try
            {
                //Making Web Request
                ItunesSearchResult res     = new ItunesSearchResult();
                string             BaseURL = "https://itunes.apple.com/search?term=";
                HttpWebRequest     request = (HttpWebRequest)WebRequest.Create(BaseURL + Term);
                request.Method = "GET";
                request.Headers.Add("cache-control", "no-cache");
                request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
                request.ContentType            = "application/json";

                //Getting Response from Web Request
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                string content = string.Empty;
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(stream))
                    {
                        content = sr.ReadToEnd();
                        //Serializing the Response to Object.
                        res = JsonConvert.DeserializeObject <ItunesSearchResult>(content);
                    }
                }
                //To Capitalize the group key.
                TextInfo textInfo = Thread.CurrentThread.CurrentCulture.TextInfo;
                return(res.results.GroupBy(t => t.kind == null? (t.collectionType == null? t.wrapperType:t.collectionType) : t.kind).Select(t => new GroupedResult()
                {
                    Type = textInfo.ToTitleCase(t.Key.ToString()) + "s", results = t.ToList(), TotalCount = res.resultCount
                }).ToList());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }