Skip to content

Commit 6e85826

Browse files
RTLcoilstrausr
authored andcommitted
Structured metadata support
* Add structured metadata APIs and entities
1 parent 5c4504a commit 6e85826

File tree

14 files changed

+963
-98
lines changed

14 files changed

+963
-98
lines changed

lib-es5/api.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,4 +618,169 @@ exports.update_resources_access_mode_by_ids = function update_resources_access_m
618618
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
619619

620620
return updateResourcesAccessMode(access_mode, "public_ids[]", ids, callback, options);
621+
};
622+
623+
/**
624+
* Creates a new metadata field definition
625+
*
626+
* @see https://cloudinary.com/documentation/admin_api#create_a_metadata_field
627+
*
628+
* @param {Object} field The field to add
629+
* @param {Function} callback Callback function
630+
* @param {Object} options Configuration options
631+
*
632+
* @return {Object}
633+
*/
634+
exports.add_metadata_field = function add_metadata_field(field, callback) {
635+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
636+
637+
var params = only(field, "external_id", "type", "label", "mandatory", "default_value", "validation", "datasource");
638+
options.content_type = "json";
639+
return call_api("post", ["metadata_fields"], params, callback, options);
640+
};
641+
642+
/**
643+
* Returns a list of all metadata field definitions
644+
*
645+
* @see https://cloudinary.com/documentation/admin_api#get_metadata_fields
646+
*
647+
* @param {Function} callback Callback function
648+
* @param {Object} options Configuration options
649+
*
650+
* @return {Object}
651+
*/
652+
exports.list_metadata_fields = function list_metadata_fields(callback) {
653+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
654+
655+
return call_api("get", ["metadata_fields"], {}, callback, options);
656+
};
657+
658+
/**
659+
* Deletes a metadata field definition.
660+
*
661+
* The field should no longer be considered a valid candidate for all other endpoints
662+
*
663+
* @see https://cloudinary.com/documentation/admin_api#delete_a_metadata_field_by_external_id
664+
*
665+
* @param {String} field_external_id The external id of the field to delete
666+
* @param {Function} callback Callback function
667+
* @param {Object} options Configuration options
668+
*
669+
* @return {Object}
670+
*/
671+
exports.delete_metadata_field = function delete_metadata_field(field_external_id, callback) {
672+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
673+
674+
return call_api("delete", ["metadata_fields", field_external_id], {}, callback, options);
675+
};
676+
677+
/**
678+
* Get a metadata field by external id
679+
*
680+
* @see https://cloudinary.com/documentation/admin_api#get_a_metadata_field_by_external_id
681+
*
682+
* @param {String} external_id The ID of the metadata field to retrieve
683+
* @param {Function} callback Callback function
684+
* @param {Object} options Configuration options
685+
*
686+
* @return {Object}
687+
*/
688+
exports.metadata_field_by_field_id = function metadata_field_by_field_id(external_id, callback) {
689+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
690+
691+
return call_api("get", ["metadata_fields", external_id], {}, callback, options);
692+
};
693+
694+
/**
695+
* Updates a metadata field by external id
696+
*
697+
* Updates a metadata field definition (partially, no need to pass the entire object) passed as JSON data.
698+
* See {@link https://cloudinary.com/documentation/admin_api#generic_structure_of_a_metadata_field Generic structure of a metadata field} for details.
699+
*
700+
* @see https://cloudinary.com/documentation/admin_api#update_a_metadata_field_by_external_id
701+
*
702+
* @param {String} external_id The ID of the metadata field to update
703+
* @param {Object} field Updated values of metadata field
704+
* @param {Function} callback Callback function
705+
* @param {Object} options Configuration options
706+
*
707+
* @return {Object}
708+
*/
709+
exports.update_metadata_field = function update_metadata_field(external_id, field, callback) {
710+
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
711+
712+
var params = only(field, "external_id", "type", "label", "mandatory", "default_value", "validation", "datasource");
713+
options.content_type = "json";
714+
return call_api("put", ["metadata_fields", external_id], params, callback, options);
715+
};
716+
717+
/**
718+
* Updates a metadata field datasource
719+
*
720+
* Updates the datasource of a supported field type (currently only enum and set), passed as JSON data. The
721+
* update is partial: datasource entries with an existing external_id will be updated and entries with new
722+
* external_id’s (or without external_id’s) will be appended.
723+
*
724+
* @see https://cloudinary.com/documentation/admin_api#update_a_metadata_field_datasource
725+
*
726+
* @param {String} field_external_id The ID of the field to update
727+
* @param {Object} entries_external_id Updated values for datasource
728+
* @param {Function} callback Callback function
729+
* @param {Object} options Configuration options
730+
*
731+
* @return {Object}
732+
*/
733+
exports.update_metadata_field_datasource = function update_metadata_field_datasource(field_external_id, entries_external_id, callback) {
734+
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
735+
736+
var params = only(entries_external_id, "values");
737+
options.content_type = "json";
738+
return call_api("put", ["metadata_fields", field_external_id, "datasource"], params, callback, options);
739+
};
740+
741+
/**
742+
* Deletes entries in a metadata field datasource
743+
*
744+
* Deletes (blocks) the datasource entries for a specified metadata field definition. Sets the state of the
745+
* entries to inactive. This is a soft delete, the entries still exist under the hood and can be activated again
746+
* with the restore datasource entries method.
747+
*
748+
* @see https://cloudinary.com/documentation/admin_api#delete_entries_in_a_metadata_field_datasource
749+
*
750+
* @param {String} field_external_id The ID of the metadata field
751+
* @param {Array} entries_external_id An array of IDs of datasource entries to delete
752+
* @param {Function} callback Callback function
753+
* @param {Object} options Configuration options
754+
*
755+
* @return {Object}
756+
*/
757+
exports.delete_datasource_entries = function delete_datasource_entries(field_external_id, entries_external_id, callback) {
758+
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
759+
760+
options.content_type = "json";
761+
var params = { external_ids: entries_external_id };
762+
return call_api("delete", ["metadata_fields", field_external_id, "datasource"], params, callback, options);
763+
};
764+
765+
/**
766+
* Restores entries in a metadata field datasource
767+
*
768+
* Restores (unblocks) any previously deleted datasource entries for a specified metadata field definition.
769+
* Sets the state of the entries to active.
770+
*
771+
* @see https://cloudinary.com/documentation/admin_api#restore_entries_in_a_metadata_field_datasource
772+
*
773+
* @param {String} field_external_id The ID of the metadata field
774+
* @param {Array} entries_external_id An array of IDs of datasource entries to delete
775+
* @param {Function} callback Callback function
776+
* @param {Object} options Configuration options
777+
*
778+
* @return {Object}
779+
*/
780+
exports.restore_metadata_field_datasource = function restore_metadata_field_datasource(field_external_id, entries_external_id, callback) {
781+
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
782+
783+
options.content_type = "json";
784+
var params = { external_ids: entries_external_id };
785+
return call_api("post", ["metadata_fields", field_external_id, "datasource_restore"], params, callback, options);
621786
};

lib-es5/uploader.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,4 +654,28 @@ exports.unsigned_image_upload_tag = function unsigned_image_upload_tag(field, up
654654
unsigned: true,
655655
upload_preset: upload_preset
656656
}));
657+
};
658+
659+
/**
660+
* Populates metadata fields with the given values. Existing values will be overwritten.
661+
*
662+
* @param {Object} metadata A list of custom metadata fields (by external_id) and the values to assign to each
663+
* @param {Array} public_ids The public IDs of the resources to update
664+
* @param {Function} callback Callback function
665+
* @param {Object} options Configuration options
666+
*
667+
* @return {Object}
668+
*/
669+
exports.update_metadata = function update_metadata(metadata, public_ids, callback) {
670+
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
671+
672+
return call_api("metadata", callback, options, function () {
673+
var params = {
674+
metadata: utils.encode_context(metadata),
675+
public_ids: utils.build_array(public_ids),
676+
timestamp: utils.timestamp(),
677+
type: options.type
678+
};
679+
return [params];
680+
});
657681
};

lib-es5/utils/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,9 @@ function updateable_resource_params(options) {
705705
if (options.context != null) {
706706
params.context = utils.encode_context(options.context);
707707
}
708+
if (options.metadata != null) {
709+
params.metadata = utils.encode_context(options.metadata);
710+
}
708711
if (options.custom_coordinates != null) {
709712
params.custom_coordinates = utils.encode_double_array(options.custom_coordinates);
710713
}

lib-es5/v2/api.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,13 @@ v1_adapters(exports, api, {
5151
update_resources_access_mode_by_tag: 2,
5252
update_resources_access_mode_by_ids: 2,
5353
search: 1,
54-
delete_derived_by_transformation: 2
54+
delete_derived_by_transformation: 2,
55+
add_metadata_field: 1,
56+
list_metadata_fields: 1,
57+
delete_metadata_field: 1,
58+
metadata_field_by_field_id: 1,
59+
update_metadata_field: 2,
60+
update_metadata_field_datasource: 2,
61+
delete_datasource_entries: 2,
62+
restore_metadata_field_datasource: 2
5563
});

lib-es5/v2/uploader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ v1_adapters(exports, uploader, {
2626
remove_all_context: 1,
2727
replace_tag: 2,
2828
create_archive: 0,
29-
create_zip: 0
29+
create_zip: 0,
30+
update_metadata: 2
3031
});
3132

3233
exports.direct_upload = uploader.direct_upload;

lib/api.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,152 @@ exports.update_resources_access_mode_by_ids = function update_resources_access_m
498498
) {
499499
return updateResourcesAccessMode(access_mode, "public_ids[]", ids, callback, options);
500500
};
501+
502+
/**
503+
* Creates a new metadata field definition
504+
*
505+
* @see https://cloudinary.com/documentation/admin_api#create_a_metadata_field
506+
*
507+
* @param {Object} field The field to add
508+
* @param {Function} callback Callback function
509+
* @param {Object} options Configuration options
510+
*
511+
* @return {Object}
512+
*/
513+
exports.add_metadata_field = function add_metadata_field(field, callback, options = {}) {
514+
const params = only(field, "external_id", "type", "label", "mandatory", "default_value", "validation", "datasource");
515+
options.content_type = "json";
516+
return call_api("post", ["metadata_fields"], params, callback, options);
517+
};
518+
519+
/**
520+
* Returns a list of all metadata field definitions
521+
*
522+
* @see https://cloudinary.com/documentation/admin_api#get_metadata_fields
523+
*
524+
* @param {Function} callback Callback function
525+
* @param {Object} options Configuration options
526+
*
527+
* @return {Object}
528+
*/
529+
exports.list_metadata_fields = function list_metadata_fields(callback, options = {}) {
530+
return call_api("get", ["metadata_fields"], {}, callback, options);
531+
};
532+
533+
/**
534+
* Deletes a metadata field definition.
535+
*
536+
* The field should no longer be considered a valid candidate for all other endpoints
537+
*
538+
* @see https://cloudinary.com/documentation/admin_api#delete_a_metadata_field_by_external_id
539+
*
540+
* @param {String} field_external_id The external id of the field to delete
541+
* @param {Function} callback Callback function
542+
* @param {Object} options Configuration options
543+
*
544+
* @return {Object}
545+
*/
546+
exports.delete_metadata_field = function delete_metadata_field(field_external_id, callback, options = {}) {
547+
return call_api("delete", ["metadata_fields", field_external_id], {}, callback, options);
548+
};
549+
550+
/**
551+
* Get a metadata field by external id
552+
*
553+
* @see https://cloudinary.com/documentation/admin_api#get_a_metadata_field_by_external_id
554+
*
555+
* @param {String} external_id The ID of the metadata field to retrieve
556+
* @param {Function} callback Callback function
557+
* @param {Object} options Configuration options
558+
*
559+
* @return {Object}
560+
*/
561+
exports.metadata_field_by_field_id = function metadata_field_by_field_id(external_id, callback, options = {}) {
562+
return call_api("get", ["metadata_fields", external_id], {}, callback, options);
563+
};
564+
565+
/**
566+
* Updates a metadata field by external id
567+
*
568+
* Updates a metadata field definition (partially, no need to pass the entire object) passed as JSON data.
569+
* See {@link https://cloudinary.com/documentation/admin_api#generic_structure_of_a_metadata_field Generic structure of a metadata field} for details.
570+
*
571+
* @see https://cloudinary.com/documentation/admin_api#update_a_metadata_field_by_external_id
572+
*
573+
* @param {String} external_id The ID of the metadata field to update
574+
* @param {Object} field Updated values of metadata field
575+
* @param {Function} callback Callback function
576+
* @param {Object} options Configuration options
577+
*
578+
* @return {Object}
579+
*/
580+
exports.update_metadata_field = function update_metadata_field(external_id, field, callback, options = {}) {
581+
const params = only(field, "external_id", "type", "label", "mandatory", "default_value", "validation", "datasource");
582+
options.content_type = "json";
583+
return call_api("put", ["metadata_fields", external_id], params, callback, options);
584+
};
585+
586+
/**
587+
* Updates a metadata field datasource
588+
*
589+
* Updates the datasource of a supported field type (currently only enum and set), passed as JSON data. The
590+
* update is partial: datasource entries with an existing external_id will be updated and entries with new
591+
* external_id’s (or without external_id’s) will be appended.
592+
*
593+
* @see https://cloudinary.com/documentation/admin_api#update_a_metadata_field_datasource
594+
*
595+
* @param {String} field_external_id The ID of the field to update
596+
* @param {Object} entries_external_id Updated values for datasource
597+
* @param {Function} callback Callback function
598+
* @param {Object} options Configuration options
599+
*
600+
* @return {Object}
601+
*/
602+
exports.update_metadata_field_datasource = function update_metadata_field_datasource(field_external_id, entries_external_id, callback, options = {}) {
603+
const params = only(entries_external_id, "values");
604+
options.content_type = "json";
605+
return call_api("put", ["metadata_fields", field_external_id, "datasource"], params, callback, options);
606+
};
607+
608+
/**
609+
* Deletes entries in a metadata field datasource
610+
*
611+
* Deletes (blocks) the datasource entries for a specified metadata field definition. Sets the state of the
612+
* entries to inactive. This is a soft delete, the entries still exist under the hood and can be activated again
613+
* with the restore datasource entries method.
614+
*
615+
* @see https://cloudinary.com/documentation/admin_api#delete_entries_in_a_metadata_field_datasource
616+
*
617+
* @param {String} field_external_id The ID of the metadata field
618+
* @param {Array} entries_external_id An array of IDs of datasource entries to delete
619+
* @param {Function} callback Callback function
620+
* @param {Object} options Configuration options
621+
*
622+
* @return {Object}
623+
*/
624+
exports.delete_datasource_entries = function delete_datasource_entries(field_external_id, entries_external_id, callback, options = {}) {
625+
options.content_type = "json";
626+
const params = { external_ids: entries_external_id };
627+
return call_api("delete", ["metadata_fields", field_external_id, "datasource"], params, callback, options);
628+
};
629+
630+
/**
631+
* Restores entries in a metadata field datasource
632+
*
633+
* Restores (unblocks) any previously deleted datasource entries for a specified metadata field definition.
634+
* Sets the state of the entries to active.
635+
*
636+
* @see https://cloudinary.com/documentation/admin_api#restore_entries_in_a_metadata_field_datasource
637+
*
638+
* @param {String} field_external_id The ID of the metadata field
639+
* @param {Array} entries_external_id An array of IDs of datasource entries to delete
640+
* @param {Function} callback Callback function
641+
* @param {Object} options Configuration options
642+
*
643+
* @return {Object}
644+
*/
645+
exports.restore_metadata_field_datasource = function restore_metadata_field_datasource(field_external_id, entries_external_id, callback, options = {}) {
646+
options.content_type = "json";
647+
const params = { external_ids: entries_external_id };
648+
return call_api("post", ["metadata_fields", field_external_id, "datasource_restore"], params, callback, options);
649+
};

0 commit comments

Comments
 (0)