Skip to content

Commit 944014b

Browse files
committed
Api call for news.
1 parent b4485c3 commit 944014b

File tree

6 files changed

+50
-10
lines changed

6 files changed

+50
-10
lines changed

News/MauiProgram.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static MauiApp CreateMauiApp()
1818
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
1919
});
2020

21-
builder.Services.AddSingleton<MockNewsService>();
21+
builder.Services.AddSingleton<NewsService>();
2222

2323
builder.Services.AddSingleton<NewsViewModel>();
2424
builder.Services.AddSingleton<NewsPage>();

News/Model/NewsToday.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using System.Text.Json.Serialization;
56
using System.Threading.Tasks;
67

78
namespace News.Model
89
{
910
public class NewsToday
1011
{
11-
public int Id { get; set; }
12+
//public int Id { get; set; }
13+
[JsonPropertyName("title")]
1214
public String Title { get; set; }
15+
[JsonPropertyName("description")]
1316
public String Content { get; set; }
17+
[JsonPropertyName("image_url")]
1418
public String ImageUrl { get; set; }
1519
}
1620
}

News/News.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<ItemGroup>
5252
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
5353
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
54+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
5455
</ItemGroup>
5556

5657
<ItemGroup>

News/Services/MockNewsService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class MockNewsService
1111
{
1212
List<NewsToday> newsList = new()
1313
{
14-
new NewsToday() {Id=1, Title="Trump Is Charged in Classified Documents Inquiry", Content="Donald J. Trump is the first former president in American history to face federal charges. He said he would surrender to the authorities.Credit...Charlie Neibergall/Associated Press", ImageUrl="https://media.cnn.com/api/v1/images/stellar/prod/230209152722-elon-musk-0127.jpg?c=original"},
15-
new NewsToday() {Id=2, Title="Gaza population being ‘dehumanized’ UN agency warns as Netanyahu rejects ceasefire calls", Content="Israeli forces rescue soldier held by Hamas in special operation, IDF says\r\nIsraeli troops advance in Gaza, hostage freed and ‘impossible’ hospital evacuation: What to know Monday\r\nUkrainian family of nine shot dead in their sleep in Russian-occupied Donetsk\r\nThai deaths in Hamas massacre spotlight poor agricultural workers from Asia who toil in Israel’s fields\r\nAn anti-Jewish riot in Russia’s Dagestan region shows the risks of Putin’s balancing act on Hamas", ImageUrl="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSlS98VPccHHsBk1m4fQ0CnrrcF5G_saPxjl2l0DYLqktP8jhlXCThxSPSpWHxr9F-fxyw&usqp=CAU"},
16-
new NewsToday() {Id=3, Title="Chinese authorities crack down on stray dogs after a Rottweiler mauls a toddler. But some say they’ve gone too far", Content="Hong Kong\r\nCNN\r\n\r\nA shocking video of a Rottweiler mauling a 2-year-old girl in China has prompted a crackdown by local authorities on stray dogs that some argue has now gone too far.\r\n\r\nAuthorities were initially praised for their swift response to the incident, but netizens soon began sharing accounts of stray canines in their neighborhood being roughly rounded up and, in some cases, put down.\r\n\r\nThe new directive to clamp down on large, unleashed dogs is being cited as the latest example of a knee-jerk reaction by Chinese authorities that also highlights the country’s long-standing struggle with animal rights and welfare.", ImageUrl="https://media.cnn.com/api/v1/images/stellar/prod/231030101544-dogs-china-covid-file.jpg?c=16x9&q=h_720,w_1280,c_fill/f_webp"}
14+
new NewsToday() {Title="Trump Is Charged in Classified Documents Inquiry", Content="Donald J. Trump is the first former president in American history to face federal charges. He said he would surrender to the authorities.Credit...Charlie Neibergall/Associated Press", ImageUrl="https://media.cnn.com/api/v1/images/stellar/prod/230209152722-elon-musk-0127.jpg?c=original"},
15+
new NewsToday() {Title="Gaza population being ‘dehumanized’ UN agency warns as Netanyahu rejects ceasefire calls", Content="Israeli forces rescue soldier held by Hamas in special operation, IDF says\r\nIsraeli troops advance in Gaza, hostage freed and ‘impossible’ hospital evacuation: What to know Monday\r\nUkrainian family of nine shot dead in their sleep in Russian-occupied Donetsk\r\nThai deaths in Hamas massacre spotlight poor agricultural workers from Asia who toil in Israel’s fields\r\nAn anti-Jewish riot in Russia’s Dagestan region shows the risks of Putin’s balancing act on Hamas", ImageUrl="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSlS98VPccHHsBk1m4fQ0CnrrcF5G_saPxjl2l0DYLqktP8jhlXCThxSPSpWHxr9F-fxyw&usqp=CAU"},
16+
new NewsToday() {Title="Chinese authorities crack down on stray dogs after a Rottweiler mauls a toddler. But some say they’ve gone too far", Content="Hong Kong\r\nCNN\r\n\r\nA shocking video of a Rottweiler mauling a 2-year-old girl in China has prompted a crackdown by local authorities on stray dogs that some argue has now gone too far.\r\n\r\nAuthorities were initially praised for their swift response to the incident, but netizens soon began sharing accounts of stray canines in their neighborhood being roughly rounded up and, in some cases, put down.\r\n\r\nThe new directive to clamp down on large, unleashed dogs is being cited as the latest example of a knee-jerk reaction by Chinese authorities that also highlights the country’s long-standing struggle with animal rights and welfare.", ImageUrl="https://media.cnn.com/api/v1/images/stellar/prod/231030101544-dogs-china-covid-file.jpg?c=16x9&q=h_720,w_1280,c_fill/f_webp"}
1717
};
1818

1919
public List<NewsToday> GetNews()

News/Services/NewsService.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using News.Model;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Net.Http;
7+
using System.Net.Http.Json;
8+
using System.Text;
9+
using System.Text.Json.Serialization;
10+
using System.Threading.Tasks;
11+
12+
namespace News.Services
13+
{
14+
public class NewsService
15+
{
16+
private HttpClient httpClient;
17+
private List<NewsToday> newsToday = new();
18+
public NewsService()
19+
{
20+
httpClient = new HttpClient();
21+
}
22+
23+
public async Task<List<NewsToday>> GetNewsTodays()
24+
{
25+
var url = "https://api.thenewsapi.com/v1/news/top?api_token=nY7SzuMYEPECPdnoIU6DX0l6TpyxdE1uswVNftU1&locale=us&limit=3";
26+
var response = await httpClient.GetAsync(url);
27+
if(response.IsSuccessStatusCode)
28+
{
29+
//need to map json and fix it.
30+
newsToday = await response.Content.ReadFromJsonAsync<List<NewsToday>>();
31+
}
32+
return newsToday;
33+
}
34+
}
35+
}

News/ViewModel/NewsViewModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ namespace News.ViewModel
1616
//public class NewsViewModel : INotifyPropertyChanged
1717
public partial class NewsViewModel : ObservableObject
1818
{
19-
private readonly MockNewsService _newsService;
19+
private NewsService _newsService;
2020
public ObservableCollection<NewsToday> NewsCollection { get; set; } = new ObservableCollection<NewsToday>();
21-
public NewsViewModel(MockNewsService mockNewsService)
21+
public NewsViewModel(NewsService newsService)
2222
{
23-
_newsService = mockNewsService;
23+
_newsService = newsService;
2424
GetNewsList();
2525
}
2626

27-
private void GetNewsList()
27+
private async void GetNewsList()
2828
{
29-
var news = _newsService.GetNews();
29+
var news = await _newsService.GetNewsTodays();
3030
foreach (var item in news)
3131
{
3232
NewsCollection.Add(item);

0 commit comments

Comments
 (0)