-
Notifications
You must be signed in to change notification settings - Fork 2
/
MiscellaneousExamples.cs
198 lines (162 loc) · 7.59 KB
/
MiscellaneousExamples.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using OpenLibraryNET.Data;
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 MiscellaneousExamples
{
HttpClient client = new HttpClient();
OpenLibraryClient olClient = new OpenLibraryClient();
public async Task SubjectExamples()
{
string subject = "World war 2";
// Get subject data for this subject
OLSubjectData? subjectData = await OLSubjectLoader.GetDataAsync(client, subject);
// Get more detailed subject data for this subject
// This data will be accessible through the ExtensionData property
// (as is all data without fields in the corresponding OL...Data record)
OLSubjectData? subjectData2 = await OLSubjectLoader.GetDataAsync
(
client,
subject,
new KeyValuePair<string, string>("detail", "true")
);
// Get only 10 works for this subject and offset by 5
// Therefore get work 5 through 14
OLSubjectData? subjectData3 = await OLSubjectLoader.GetDataAsync
(
client,
subject,
new KeyValuePair<string, string>("limit", "10")
);
// You can also get subjects from, for example, works, like so
OLWorkData work = (await OLWorkLoader.GetDataAsync(client, "OL675783W"))!;
IReadOnlyList<string> workSubjects = work.Subjects;
}
public async Task SearchExamples()
{
string query = "my query";
// You can search for works,
OLWorkData[]? workData = await OLSearchLoader.GetSearchResultsAsync(client, query);
// for authors,
OLAuthorData[]? authorData = await OLSearchLoader.GetAuthorSearchResultsAsync(client, query);
// for subjects
OLSubjectData[]? subjectData = await OLSearchLoader.GetSubjectSearchResultsAsync(client, query);
// and for user created lists
OLListData[]? listData = await OLSearchLoader.GetListSearchResultsAsync(client, query);
// Search for Lolita, by Vladimir Nabokov
OLWorkData[]? workData2 = await OLSearchLoader.GetSearchResultsAsync
(
client,
"",
new KeyValuePair<string, string>("title", "lolita"),
new KeyValuePair<string, string>("author", "vladimir nabokov")
);
// Alternatively, you could search like this; they are equivalent
OLWorkData[]? workData3 = await OLSearchLoader.GetSearchResultsAsync
(
client,
"title:lolita author:vladimir nabokov"
);
// Search for random authors with "Max" in their name
// Limit the search to three, and offset the results by 5
// (therefore results 6-8 will be returned)
OLAuthorData[]? authorData2 = await OLSearchLoader.GetAuthorSearchResultsAsync
(
client,
"Max",
new KeyValuePair<string, string>("sort", "random"),
new KeyValuePair<string, string>("limit", "3"),
new KeyValuePair<string, string>("offset", "5")
);
}
public async Task RecentChangesExamples()
{
// Get recent changes
OLRecentChangesData[]? data = await OLRecentChangesLoader.GetRecentChangesAsync(client);
// Get changes made on January 1st 1970
OLRecentChangesData[]? data1 = await OLRecentChangesLoader.GetRecentChangesAsync(client, "1970", "01", "01");
// Get most recent "add-book" changes
OLRecentChangesData[]? data2 = await OLRecentChangesLoader.GetRecentChangesAsync(client, kind: "add-book");
// Get last 5 changes made by a bot
KeyValuePair<string, string>[] parameters = new KeyValuePair<string, string>[]
{
new ("limit", "5"),
new ("bot", "true")
};
OLRecentChangesData[]? data3 = await OLRecentChangesLoader.GetRecentChangesAsync(client, parameters: parameters);
// Get (up to) 5 "merge-authors" changes made by a bot in May 2018
OLRecentChangesData[]? data4 = await OLRecentChangesLoader.GetRecentChangesAsync
(
client,
kind: "merge-authors",
year: "2018",
month: "05",
parameters: parameters
);
}
public async Task ListsExamples()
{
// Get list with id "012345" belonging to user "luca3317"
OLListData? data = await OLListLoader.GetListAsync(client, "luca3317", "012345");
// Get all lists belonging to user "luca3317"
OLListData[]? data1 = await OLListLoader.GetUserListsAsync(client, "luca3317");
// Get all editions in list "012345" by user "luca3317"
OLEditionData[]? data2 = await OLListLoader.GetListEditionsAsync(client, "luca3317", "012345");
// Get first 5 editions in list "012345" by user "luca3317"
OLEditionData[]? data3 = await OLListLoader.GetListEditionsAsync
(
client,
"luca3317",
"012345",
new KeyValuePair<string, string>("limit", "5")
);
// You can also get lists from other APIs
string editionOLID = "";
OLListData[]? data4 = await OLEditionLoader.GetListsAsync(client, editionOLID);
string workOLID = "";
OLListData[]? data5 = await OLWorkLoader.GetListsAsync(client, workOLID);
string authorOLID = "";
OLListData[]? data6 = await OLAuthorLoader.GetListsAsync(client, authorOLID);
string subject = "";
OLListData[]? data7 = await OLSubjectLoader.GetListsAsync(client, subject);
}
public async Task PartnerExamples()
{
// Get partner data by ISBN
OLPartnerData? data = await OLPartnerLoader.GetDataAsync(client, PartnerIdType.ISBN, "0123456789");
// Get multiple partner data records at once
OLPartnerData[]? data1 = await OLPartnerLoader.GetMultiDataAsync(client, "isbn:0865474613", "olid:OL35738141M");
// Equivalent
OLPartnerData[]? data2 = await OLPartnerLoader.GetMultiDataAsync
(
client,
OpenLibraryUtility.SetBibkeyPrefix(PartnerIdType.ISBN, "0865474613"),
OpenLibraryUtility.SetBibkeyPrefix(PartnerIdType.OLID, "OL35738141M")
);
}
public async Task CoverExamples()
{
// From a work id, get the cover of the first edition
string workOLID = "OL675783W";
OLEditionData data = (await OLWorkLoader.GetEditionsAsync
(
client,
workOLID,
new KeyValuePair<string, string>("limit", "1")
))![0];
byte[] cover = await OLImageLoader.GetCoverAsync(client, CoverIdType.OLID, data.ID, ImageSize.Medium);
}
public async Task AuthorPhotoExamples()
{
// From an author id, get one of the author's photos
string authorID = "OL234664A";
byte[] authorPhoto = await OLImageLoader.GetAuthorPhotoAsync(client, AuthorPhotoIdType.OLID, authorID, ImageSize.Small);
}
}
}