-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWorkAPIExamples.cs
74 lines (65 loc) · 2.47 KB
/
WorkAPIExamples.cs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using OpenLibraryNET.Loader;
using OpenLibraryNET.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenLibraryNET.examples
{
public class WorkAPIExamples
{
/*
* Given a work's id, get all its information
*/
public async Task LoadWork()
{
OpenLibraryClient client = new OpenLibraryClient();
string workOLID = "";
// Get all of the work's info and 10 of its editions
OLWork work = await client.GetWorkAsync(workOLID, 10);
// Equivalent
work = new OLWork()
{
ID = workOLID,
Data = await client.Work.GetDataAsync(workOLID),
Bookshelves = await client.Work.GetBookshelvesAsync(workOLID),
Ratings = await client.Work.GetRatingsAsync(workOLID),
Editions = await client.Work.GetEditionsAsync(workOLID, new KeyValuePair<string, string>("limit", "10"))
};
// Equivalent, using static methods
HttpClient httpClient = new HttpClient();
work = new OLWork()
{
ID = workOLID,
Data = await OLWorkLoader.GetDataAsync(httpClient, workOLID),
Bookshelves = await OLWorkLoader.GetBookshelvesAsync(httpClient, workOLID),
Ratings = await OLWorkLoader.GetRatingsAsync(httpClient, workOLID),
Editions = await OLWorkLoader.GetEditionsAsync(httpClient, workOLID, new KeyValuePair<string, string>("limit", "10"))
};
}
/*
* Get a work without having a prior work OLID,
* using the search function
*/
public async Task GetWorkFromSearch()
{
HttpClient client = new HttpClient();
// The search query
string query = "The picture of dorian gray";
// Request the first author returned by the search
var results = await OLSearchLoader.GetSearchResultsAsync(client, query, new KeyValuePair<string, string>("limit", "1"));
// Ensure there were enough search results
if (results == null || results.Length == 0)
{
// Error handling code
return;
}
OLWork work = new OLWork()
{
ID = results[0].ID,
Data = results[0]
};
}
}
}