Skip to content

Commit

Permalink
Handle manifests that fail to deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldgray committed Oct 28, 2024
1 parent 6594459 commit 1f41564
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class CookbookDeserialization
public void Can_Deserialize_Cookbook_Manifest(string manifestId, Manifest manifest)
{
// perfunctory assertion
manifest.Should().NotBeNull();
manifest.Should().NotBeNull($"{manifestId} is a valid cookbook manifest");
manifest.Id.Should().Be(manifestId);
}
}
117 changes: 63 additions & 54 deletions src/IIIF/IIIF.Tests/Serialisation/Data/CookbookManifestData.cs
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();
}

0 comments on commit 1f41564

Please sign in to comment.