From c02c541e8c3b3c36f9d294b755311198b680e84c Mon Sep 17 00:00:00 2001 From: W Beecher Baker Date: Mon, 30 Sep 2024 11:22:42 -0400 Subject: [PATCH] add pagination support; add image fetch complement to Darwin Core API call --- src/modules/otus/services/TaxonWorks.js | 37 +++++++++++++------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/modules/otus/services/TaxonWorks.js b/src/modules/otus/services/TaxonWorks.js index 5f31696..176c042 100644 --- a/src/modules/otus/services/TaxonWorks.js +++ b/src/modules/otus/services/TaxonWorks.js @@ -1,5 +1,17 @@ import { makeAPIRequest } from '@/utils/request' +/** + * Generate query params for pagination. + * @param opt.per {number} - Number of items per page. + * @param opt.page {number} - Page number + * @returns {""|string} + */ +const perPage = (opt) => { + const { per, page } = opt ?? {} + const queryParams = [per && `per=${per}`, page && `page=${page}`].filter(Boolean).join("&") + return queryParams && `?${queryParams}` // prepend "?" if there are any query params +} + export default class TaxonWorks { static getTaxonNameCitations(taxonId, opt) { return makeAPIRequest.get(`/taxon_names/${taxonId}/inventory/catalog`, opt) @@ -59,24 +71,13 @@ export default class TaxonWorks { return makeAPIRequest.get(`/otus/${otuId}/inventory/content`, opt) } - // Note that this could support multiple OTUs. - // The API takes a comma-separated list of OTU IDs. - static getCollectionObjects(otuId, opt) { - return makeAPIRequest.get(`/collection_objects?otu_id[]=${otuId}`, opt) - // See taxonworks/lib/collection_object/filter.rb Queries:CollectionObject:Filter for full list of options. - // However, none seems to change the result. - /* - @with_buffered_collecting_event = boolean_param(params, :with_buffered_collecting_event) - @with_buffered_determinations = boolean_param(params, :with_buffered_determinations) - @with_buffered_other_labels = boolean_param(params, :with_buffered_other_labels) - */ - // return makeAPIRequest.get(`/collection_objects?otu_id[]=${otuId}&with_buffered_determination=true`, opt) - // return makeAPIRequest.get(`/collection_objects?otu_id[]=${otuId}&descendants=true`, opt) - // return makeAPIRequest.get(`/collection_objects?otu_id[]=${otuId}/dwc.json`, opt) - } - - static getOtuInventoryDarwinCore(otuId, opt) { - return makeAPIRequest.get(`/otus/${otuId}/inventory/dwc.json`, opt) + /** The descendant taxa of an OTU, in Darwin Core format. */ + static getDescendantsDarwinCore(otuId, opt) { + return makeAPIRequest.get(`/otus/${otuId}/inventory/dwc.json${perPage(opt)}`, opt) + } + + static getDescendantsImageGallery(otuId, opt) { + return makeAPIRequest.get(`/otus/${otuId}/inventory/dwc_gallery.json${perPage(opt)}`, opt) } /** Load image info (thumbnail, etc) from a URL such as one in a Darwin Core 'associatedMedia' field. */