[go: up one dir, main page]

0% found this document useful (0 votes)
17 views4 pages

PHreesia

The document shows how to use LINQ to search for books on an API instead of using manual loops. It performs searches for books with specific titles, validates the response against an expected response, and prints output like book counts and keys.

Uploaded by

arshdeepsekhon76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

PHreesia

The document shows how to use LINQ to search for books on an API instead of using manual loops. It performs searches for books with specific titles, validates the response against an expected response, and prints output like book counts and keys.

Uploaded by

arshdeepsekhon76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

LINQ instead of manual loop

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++;
}
}
}

Console.WriteLine($"Number of books with the exact 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");

Console.WriteLine("List of keys of books published since 2000:");


count = 0;
if (data != null)
{
foreach (var book in data.docs)
{

if (book.first_publish_year != null &&


book.first_publish_year >= 2000)
{
Console.WriteLine(book.key);
count++;
}
}
}

}
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)}";

using (HttpClient client = new())


{
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
dynamic? data = JsonConvert.DeserializeObject(json);

var expectedResponse = JObject.Parse(@"{


'numFound': 1,
'start': 0,
'numFoundExact': true,
'docs': [
{
'key': '/works/OL15047325W',
'text': [
'/works/OL15047325W',
'Goodnight Moon 123 Lap Edition',
'OL9953293M',
'234235364',
'Clement Hurd (Illustrator)',
'9780061667558',
'HarperFestival',
'OL22122A',
'Margaret Wise Brown',
'Juniper Sage',
'Margaret Wise Brown',
'Golden MacDonald',
'Timothy Hay'
],
'type': 'work',
'seed': [
'/books/OL0000000M',
'/works/OL15047325W',
'/authors/OL22122A'
],
'title': 'Goodnight Moon 123 Lap Edition',
'title_suggest': 'Goodnight Moon 123 Lap Edition',
'has_fulltext': false,
'edition_count': 1,
'edition_key': [
'OL9953293M',
'OL11111111'
],
'publish_date': [
'July 1, 2008'
],
'publish_year': [
2008
],
'first_publish_year': 2008,
'oclc': [
'234235364'
],
'contributor': [
'Clement Hurd (Illustrator)'
],
'isbn': [
'0061667552',
'9780061667558'
],
'last_modified_i': 1582912881,
'ebook_count_i': 0,
'publisher': [
'HarperFestival'
],
'language': [
'eng'
],
'author_key': [
'OL22122A'
],
'author_name': [
'Margaret Wise Brown'
],
'author_alternative_name': [
'Juniper Sage',
'Margaret Wise Brown',
'Golden MacDonald',
'Timothy Hay'
],
'id_goodreads': [
'2508736'
],
'id_librarything': [
'3377897'
],
'publisher_facet': [
'HarperFestival'
],
'_version_': 1700699423277842432,
'author_facet': [
'OL22122A Margaret Wise Brown'
]
}
],
'num_found': 1,
'q': '',
'offset': null
}");

// Part 3.2.1: Validate the response against the expected response


bool isResponseValid = JToken.DeepEquals(JToken.FromObject(data),
expectedResponse);

if (isResponseValid)
{
Console.WriteLine("Response matches the expected response.");
}
else
{
Console.WriteLine("Response does not match the expected
response.");
Console.WriteLine("Differences:");

// Find and print the differing values


if (data != null)
{
foreach (var property in expectedResponse.Properties())
{
JToken actualValue = data[property.Name];
JToken expectedValue = property.Value;
if (!JToken.DeepEquals(actualValue, expectedValue))
{
Console.WriteLine($"Property: {property.Name}");
Console.WriteLine($"Expected Value:
{expectedValue}");
Console.WriteLine($"Actual Value: {actualValue}");
Console.WriteLine();
}
}
}
}
}
else
{
Console.WriteLine("API request failed. Status code: " +
response.StatusCode);
}
}
}
}

You might also like