示例#1
0
        private async Task <NewsApiSourceResponse> getSourcesResponse()
        {
            using (var client = new HttpClient())
            {
                string url = "https://newsapi.org/v1/sources";

                HttpResponseMessage responseMessage = await client.GetAsync(url);

                if (responseMessage.IsSuccessStatusCode)
                {
                    string responseData          = responseMessage.Content.ReadAsStringAsync().Result;
                    NewsApiSourceResponse result = JsonConvert.DeserializeObject <NewsApiSourceResponse>(responseData);

                    if (result.Sources != null)
                    {
                        foreach (NewsApiSource source in result.Sources)
                        {
                            source.UrlsToLogos.Large  = us.ProcessImageUrl(source.UrlsToLogos.Large);
                            source.UrlsToLogos.Medium = us.ProcessImageUrl(source.UrlsToLogos.Medium);
                            source.UrlsToLogos.Small  = us.ProcessImageUrl(source.UrlsToLogos.Small);
                        }
                    }

                    return(result);
                }
            }

            return(null);
        }
示例#2
0
        public IEnumerable <NewsSource> GetSources()
        {
            Task <NewsApiSourceResponse> task = getSourcesResponse();

            List <NewsSource> result = new List <NewsSource>();

            if (task == null)
            {
                return(result);
            }

            NewsApiSourceResponse response = task.Result;

            // TODO process errors

            if (response == null)
            {
                return(result);
            }

            if (response.Status != NewsResponseStatus.OK)
            {
                return(result);
            }

            int i = 0;

            foreach (NewsApiSource apiSource in response.Sources)
            {
                i++;
                NewsSource source = convertApiSource2Source(apiSource, i);
                result.Add(source);
            }

            return(result);
        }