Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Program:

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

End of program
Output Printed: Number of books with the exact title 'Goodnight Moon': 13

Part3.1.2

List of keys of books published since 2000:

/works/OL151866W

/works/OL26831311W

/works/OL20081449W

/works/OL25589868W

/works/OL22177520W

/works/OL6219194W

/works/OL30825221W

/works/OL32378068W

/works/OL28095568W

/works/OL22016309W

/works/OL9192921W

/works/OL32412280W

/works/OL30848135W

/works/OL33865575W

/works/OL25057476W

/works/OL27429506W

/works/OL28801632W

/works/OL20411370W

/works/OL21154393W
/works/OL21673796W

/works/OL15047327W

/works/OL9856986W

/works/OL9856987W

/works/OL9856988W

/works/OL9958608W

/works/OL12191021M

/works/OL31229072W

/works/OL27152305W

/works/OL25941945W

/works/OL26595129W

/works/OL26528482W

/works/OL8060810W

/works/OL20763230W

/works/OL7983348M

/works/OL10821269M

/works/OL26559704W

/works/OL7456211W

/works/OL21110117W

/works/OL22138214W

/works/OL17538279W

/works/OL17528078W

/works/OL17569984W

/works/OL17472557W

/works/OL21888381W

/works/OL17416845W

/works/OL21121843W

/works/OL26780641W

/works/OL22162718W
/works/OL31445950W

/works/OL31852531W

/works/OL33915683W

/works/OL25053339W

/works/OL24730072W

/works/OL25117529W

/works/OL25238548W

/works/OL26176586W

/works/OL26145082W

/works/OL27016571W

/works/OL27016621W

/works/OL25687194W

/works/OL26131954W

/works/OL26141510W

/works/OL26135071W

/works/OL24633003W

/works/OL24678602W

/works/OL24554159W

/works/OL24687102W

/works/OL24584777W

/works/OL24623348W

/works/OL24516252W

/works/OL24636752W

/works/OL24710357W

/works/OL24648630W

/works/OL24792500W

/works/OL24701141W

/works/OL24727417W

/works/OL24723615W
Part3.2

Response does not match the expected response.

Differences:

Property: docs

Expected Value: [

"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"

Actual Value: [

"key": "/works/OL26831311W",

"type": "work",

"seed": [

"/books/OL36885090M",

"/books/OL36421085M",

"/books/OL36410993M",

"/books/OL36381057M",

"/works/OL26831311W",

"/subjects/children's_fiction",

"/subjects/technology",

"/authors/OL9653893A",

"/authors/OL10086113A"

],

"title": "Goodnight Moon Base",

"title_suggest": "Goodnight Moon Base",

"title_sort": "Goodnight Moon Base",

"edition_count": 4,

"edition_key": [

"OL36885090M",

"OL36421085M",

"OL36410993M",

"OL36381057M"
],

"publish_date": [

"2021"

],

"publish_year": [

2021

],

"first_publish_year": 2021,

"number_of_pages_median": 32,

"isbn": [

"1956622047",

"9781956622041",

"1956622055",

"1956622063",

"1956622071",

"9781956622072",

"9781956622058",

"9781956622065"

],

"last_modified_i": 1679409950,

"ebook_count_i": 0,

"ebook_access": "no_ebook",

"has_fulltext": false,

"public_scan_b": false,

"readinglog_count": 0,

"want_to_read_count": 0,

"currently_reading_count": 0,

"already_read_count": 0,

"cover_edition_key": "OL36885090M",
"cover_i": 13753793,

"publisher": [

"Aero Maestro"

],

"language": [

"eng"

],

"author_key": [

"OL9653893A",

"OL10086113A"

],

"author_name": [

"Brett Hoffstadt",

"Steve Tanaka"

],

"subject": [

"Children's fiction",

"Technology"

],

"publisher_facet": [

"Aero Maestro"

],

"subject_facet": [

"Children's fiction",

"Technology"

],

"_version_": 1760988970421846016,

"author_facet": [

"OL10086113A Steve Tanaka",


"OL9653893A Brett Hoffstadt"

],

"subject_key": [

"children's_fiction",

"technology"

End of output printed.

You might also like