-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEditionAPIExamples.cs
More file actions
89 lines (76 loc) · 3.18 KB
/
Copy pathEditionAPIExamples.cs
File metadata and controls
89 lines (76 loc) · 3.18 KB
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
using OpenLibraryNET.Loader;
using OpenLibraryNET.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenLibraryNET.examples
{
public class EditionAPIExamples
{
/*
* Given an edition's id, get all its information
*/
public async Task LoadEdition()
{
OpenLibraryClient client = new OpenLibraryClient();
string editionOLID = "";
// Get the edition's data, as well as its cover in small resolution
OLEdition edition = await client.GetEditionAsync(editionOLID, EditionIdType.OLID, ImageSize.Small);
// Equivalent
edition = new OLEdition()
{
ID = editionOLID,
Data = await client.Edition.GetDataAsync(editionOLID),
CoverS = await client.Image.GetCoverAsync(CoverIdType.OLID, editionOLID, ImageSize.Small)
};
// Equivalent, using static methods
HttpClient httpClient = new HttpClient();
edition = new OLEdition()
{
ID = editionOLID,
Data = await OLEditionLoader.GetDataAsync(httpClient, editionOLID),
CoverS = await OLImageLoader.GetCoverAsync(httpClient, CoverIdType.OLID, editionOLID, ImageSize.Small),
};
// Alternatives
// Get an edition by ISBN
edition = await client.GetEditionAsync("0123456789", EditionIdType.ISBN, ImageSize.Small);
// Get an edition by OCLC
edition = await client.GetEditionAsync("30647889", EditionIdType.OCLC, ImageSize.Small);
// Get an edition using a prepared bibkey; no cover
edition = await client.GetEditionAsync("ISBN:0123456789");
}
/*
* Get an edition without having a prior edition OLID,
* using the search function
*/
public async Task GetEditionFromSearch()
{
HttpClient client = new HttpClient();
// The search query
string query = "the lord of the rings two towers";
// 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;
}
OLEditionData editionData = (await OLWorkLoader.GetEditionsAsync(client, results[0].ID))![0];
OLEdition edition = new OLEdition()
{
ID = editionData.ID,
Data = editionData
};
// Alternatively, you can try to get the edition ID from OLWorkData's extensiondata
string editionID = results[0].ExtensionData["cover_edition_key"].ToObject<string>();
OLEdition editionAlt = new OLEdition()
{
ID = editionID,
Data = await OLEditionLoader.GetDataAsync(client, editionID, EditionIdType.OLID)
};
}
}
}