示例#1
0
        private async Task <NewsApiArticleResponse> getArticlesResponse(string sourceId)
        {
            using (var client = new HttpClient())
            {
                string url = string.Format("https://newsapi.org/v1/articles?source={0}&apiKey=49c60107132542058df6c2fa3b27eb91", sourceId);

                HttpResponseMessage responseMessage = await client.GetAsync(url);

                if (responseMessage.IsSuccessStatusCode)
                {
                    string responseData = responseMessage.Content.ReadAsStringAsync().Result;

                    NewsApiArticleResponse result = JsonConvert.DeserializeObject <NewsApiArticleResponse>(responseData);

                    foreach (NewsApiArticle article in result.Articles)
                    {
                        article.UrlToImage = us.ProcessImageUrl(article.UrlToImage);
                    }

                    return(result);
                }
            }

            return(null);
        }
示例#2
0
        public IEnumerable <Link> GetLinks(string sourceId)
        {
            Task <NewsApiArticleResponse> task = getArticlesResponse(sourceId);

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

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

            NewsApiArticleResponse response = task.Result;

            // TODO process errors and response == null

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

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

            int i = 0;

            foreach (NewsApiArticle article in response.Articles)
            {
                i++;
                Link link = convertArticle2Link(article, i);
                result.Add(link);
            }

            return(result);
        }