get method

  1. @override
Resource? get(
  1. Link link
)
override

A service can return a Resource to:

  • respond to a request to its web API declared in links,
  • serve additional resources on behalf of the publication,
  • replace a publication resource by its own version.

Called by Publication.get for each request.

Warning: If you need to request one of the publication resources to answer the request, use the Fetcher provided by the PublicationServiceContext instead of Publication.get, otherwise it will trigger an infinite loop.

@return The Resource containing the response, or null if the service doesn't recognize this request.

Implementation

@override
Resource? get(Link link) {
  if (link.href != coverLink.href) {
    return null;
  }
  return LazyResource(() async {
    Image? image = await cover();
    if (image == null) {
      ResourceException error = ResourceException.other(
          Exception("Unable to convert cover to PNG."));
      return FailureResource(coverLink, error);
    } else {
      List<int> png = encodePng(image);
      Link link = coverLink.copy(width: image.width, height: image.height);
      return BytesResource(link, () async => png.toByteData());
    }
  });
}