PHreesia
PHreesia
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
class Program
{
static async Task Main()
{
// Part 3.1: Search for books having "Goodnight Moon" in its title
string searchQuery = "Goodnight Moon";
string apiUrl = $"https://openlibrary.org/search.json?
title={Uri.EscapeDataString(searchQuery)}";
using (HttpClient client = new())
{
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
dynamic? data = JsonConvert.DeserializeObject(json);
// Part 3.1.1: Print the total number of books with the exact title
"Goodnight Moon"
int count = 0;
if (data != null)
{
foreach (var doc in data["docs"])
{
string title = doc["title"].ToString();
if (title == searchQuery)
{
count++;
}
}
}
// Part 3.1.2: Print the list of keys of books published since 2000
Console.WriteLine("\n\n");
Console.WriteLine("Part3.1.2");
Console.WriteLine("\n\n");
}
else
{
Console.WriteLine("API request failed. Status code: " +
response.StatusCode);
}
}
Console.WriteLine("\n");
Console.WriteLine("Part3.2");
Console.WriteLine("\n");
// Part 3.2: Search for books with the title "Goodnight Moon Base"
searchQuery = "Goodnight Moon Base";
apiUrl = $"https://openlibrary.org/search.json?
title={Uri.EscapeDataString(searchQuery)}";
if (isResponseValid)
{
Console.WriteLine("Response matches the expected response.");
}
else
{
Console.WriteLine("Response does not match the expected
response.");
Console.WriteLine("Differences:");