-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle manifests that fail to deserialize
- Loading branch information
1 parent
6594459
commit 1f41564
Showing
2 changed files
with
64 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 63 additions & 54 deletions
117
src/IIIF/IIIF.Tests/Serialisation/Data/CookbookManifestData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,64 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using IIIF.Presentation.V3; | ||
using IIIF.Serialisation; | ||
|
||
namespace IIIF.Tests.Serialisation.Data; | ||
|
||
/// <summary> | ||
/// Used as [ClassData] - contains Manifests from IIIF Cookbook to validate deserialisation | ||
/// </summary> | ||
public class CookbookManifestData : IEnumerable<object[]> | ||
{ | ||
// This will store { manifest-id, deserialized-manifest } | ||
private readonly List<object[]> data = new(); | ||
|
||
// these have bugs in the cookbook, see https://github.com/IIIF/cookbook-recipes/pull/546 | ||
private List<string> skip = new() | ||
{ | ||
"https://iiif.io/api/cookbook/recipe/0219-using-caption-file/manifest.json", | ||
"https://iiif.io/api/cookbook/recipe/0040-image-rotation-service/manifest-service.json" | ||
}; | ||
|
||
public CookbookManifestData() | ||
{ | ||
using var httpClient = new HttpClient(); | ||
var theseusCollection = | ||
GetIIIFResource<Collection>("https://theseus-viewer.netlify.app/cookbook-collection.json", true); | ||
|
||
foreach (var item in theseusCollection.Items!) | ||
{ | ||
if (item is Manifest manifestRef) | ||
{ | ||
if (skip.Contains(manifestRef.Id)) continue; | ||
|
||
var iiif = GetIIIFResource<Manifest>(manifestRef.Id); | ||
data.Add(new object[] { manifestRef.Id, iiif }); | ||
} | ||
} | ||
|
||
T GetIIIFResource<T>(string url, bool mustSucceed = false) where T : JsonLdBase | ||
{ | ||
var resource = httpClient.GetAsync(url).Result; | ||
if (mustSucceed) resource.EnsureSuccessStatusCode(); | ||
if (!resource.IsSuccessStatusCode) return null; | ||
|
||
var iiif = resource.Content.ReadAsStream().FromJsonStream<T>(); | ||
return iiif; | ||
} | ||
} | ||
|
||
public IEnumerator<object[]> GetEnumerator() => data.GetEnumerator(); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using IIIF.Presentation.V3; | ||
using IIIF.Serialisation; | ||
|
||
namespace IIIF.Tests.Serialisation.Data; | ||
|
||
/// <summary> | ||
/// Used as [ClassData] - contains Manifests from IIIF Cookbook to validate deserialisation | ||
/// </summary> | ||
public class CookbookManifestData : IEnumerable<object[]> | ||
{ | ||
// This will store { manifest-id, deserialized-manifest } | ||
private readonly List<object[]> data = new(); | ||
|
||
// these have bugs in the cookbook, see https://github.com/IIIF/cookbook-recipes/pull/546 | ||
private List<string> skip = new() | ||
{ | ||
"https://iiif.io/api/cookbook/recipe/0219-using-caption-file/manifest.json", | ||
"https://iiif.io/api/cookbook/recipe/0040-image-rotation-service/manifest-service.json" | ||
}; | ||
|
||
public CookbookManifestData() | ||
{ | ||
using var httpClient = new HttpClient(); | ||
var theseusCollection = | ||
GetIIIFResource<Collection>("https://theseus-viewer.netlify.app/cookbook-collection.json", true); | ||
|
||
foreach (var item in theseusCollection.Items!) | ||
{ | ||
if (item is Manifest manifestRef) | ||
{ | ||
if (skip.Contains(manifestRef.Id)) continue; | ||
|
||
var iiif = GetIIIFResource<Manifest>(manifestRef.Id); | ||
data.Add(new object[] { manifestRef.Id, iiif }); | ||
} | ||
} | ||
|
||
T GetIIIFResource<T>(string url, bool mustSucceed = false) where T : JsonLdBase | ||
{ | ||
var resource = httpClient.GetAsync(url).Result; | ||
if (mustSucceed) resource.EnsureSuccessStatusCode(); | ||
if (!resource.IsSuccessStatusCode) return null; | ||
|
||
try | ||
{ | ||
var iiif = resource.Content.ReadAsStream().FromJsonStream<T>(); | ||
return iiif; | ||
} | ||
catch (Exception) | ||
{ | ||
if (mustSucceed) throw; | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
public IEnumerator<object[]> GetEnumerator() => data.GetEnumerator(); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} |