-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGNewClassFile.cs
More file actions
37 lines (34 loc) · 1.08 KB
/
GNewClassFile.cs
File metadata and controls
37 lines (34 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
public class NewsController : ApiController
{
private const string GNEWS_API_KEY = "YOUR_API_KEY";
[HttpGet]
[Route("api/news")]
public async Task<IHttpActionResult> GetNews(string query = "technology")
{
using (var httpClient = new HttpClient())
{
try
{
string url = $"https://gnews.io/api/v4/search?q={query}&token={GNEWS_API_KEY}";
HttpResponseMessage response = await httpClient.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var responseData = await response.Content.ReadAsStringAsync();
return Ok(responseData);
}
else
{
return BadRequest("Failed to fetch news articles");
}
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
}
}