From 62dadc5e01f38335d56cea9dc79cca52cfc5c971 Mon Sep 17 00:00:00 2001 From: ahongbynder Date: Thu, 30 Nov 2023 11:47:47 -0800 Subject: [PATCH] API-1822 add metaproperties sample, update logging for samples, setup methods for collection sample --- .../com/bynder/sdk/sample/BrandsSample.java | 10 +- .../bynder/sdk/sample/CollectionsSample.java | 99 +++++++++++++++---- .../com/bynder/sdk/sample/MediaSample.java | 38 ++++--- .../sdk/sample/MetapropertiesSample.java | 66 +++++++++++++ .../bynder/sdk/sample/SmartFiltersSample.java | 29 +++--- .../com/bynder/sdk/sample/TagsSample.java | 10 +- .../com/bynder/sdk/sample/UploadSample.java | 4 +- .../com/bynder/sdk/sample/UsageSample.java | 13 +-- 8 files changed, 205 insertions(+), 64 deletions(-) diff --git a/src/main/java/com/bynder/sdk/sample/BrandsSample.java b/src/main/java/com/bynder/sdk/sample/BrandsSample.java index 520e2266..2d8afdc6 100644 --- a/src/main/java/com/bynder/sdk/sample/BrandsSample.java +++ b/src/main/java/com/bynder/sdk/sample/BrandsSample.java @@ -37,10 +37,12 @@ public static void main(final String[] args) throws URISyntaxException, IOExcept // Call the API to request for brands List brands = assetService.getBrands().blockingSingle().body(); - LOG.info("brands"); - for (Brand brand : brands) { - LOG.info(brand.getName()); - LOG.info(brand.getDescription()); + if (brands != null && !brands.isEmpty()) { + for (Brand brand : brands) { + LOG.info("Brand ID: " + brand.getId()); + LOG.info("Brand Name: " + brand.getName()); + LOG.info("Brand Description: " + brand.getDescription()); + } } } } diff --git a/src/main/java/com/bynder/sdk/sample/CollectionsSample.java b/src/main/java/com/bynder/sdk/sample/CollectionsSample.java index 0513f43b..2cb92250 100644 --- a/src/main/java/com/bynder/sdk/sample/CollectionsSample.java +++ b/src/main/java/com/bynder/sdk/sample/CollectionsSample.java @@ -2,10 +2,9 @@ import com.bynder.sdk.configuration.Configuration; import com.bynder.sdk.model.Collection; +import com.bynder.sdk.query.collection.*; import com.bynder.sdk.service.BynderClient; import com.bynder.sdk.service.collection.CollectionService; -import com.bynder.sdk.query.collection.CollectionQuery; -import com.bynder.sdk.query.collection.CollectionCreateQuery; import com.bynder.sdk.util.Utils; import java.io.IOException; @@ -13,8 +12,8 @@ import java.net.URL; import java.util.List; -import java.util.Map; import java.util.Properties; +import java.util.Random; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,31 +36,91 @@ public static void main(final String[] args) throws URISyntaxException, IOExcept // Initialize collection service CollectionService collectionService = client.getCollectionService(); - // get collections with limit and page - CollectionQuery collectionQuery = new CollectionQuery().setLimit(10).setPage(1); + // get collections + CollectionQuery collectionQuery = new CollectionQuery(); List collections = collectionService.getCollections(collectionQuery).blockingSingle().body(); - for (Collection collectionResult : collections) { - LOG.info("collection: "); - LOG.info(collectionResult.getId()); - LOG.info(collectionResult.getName()); - LOG.info(collectionResult.getDescription()); + if (collections != null && !collections.isEmpty()) { + for (Collection collectionResult : collections) { + LOG.info("collection: "); + LOG.info(collectionResult.getId()); + LOG.info(collectionResult.getName()); + LOG.info(collectionResult.getDescription()); + } } - // add collection - CollectionCreateQuery createCollectionQuery = new CollectionCreateQuery("New Collection 1234"); + // get collection info + String collectionInfoId = appProperties.getProperty("GET_COLLECTION_INFO_ID"); + CollectionInfoQuery collectionInfoQuery = new CollectionInfoQuery(collectionInfoId); + Collection collectionInfo = collectionService.getCollectionInfo(collectionInfoQuery).blockingSingle().body(); + if (collectionInfo != null) { + LOG.info("collection info name: " + collectionInfo.getName()); + LOG.info("collection description: " + collectionInfo.getDescription()); + } + + // create collection + Random random = new Random(); + int randomInt = random.nextInt(100); + String newCollectionName = "New Collection" + randomInt; + LOG.info("new collection name: " + newCollectionName); + CollectionCreateQuery createCollectionQuery = new CollectionCreateQuery(newCollectionName); collectionService.createCollection(createCollectionQuery).blockingSingle(); // get collections with added collection List updatedCollections = collectionService.getCollections(collectionQuery).blockingSingle().body(); - for (Collection collectionResult : collections) { - LOG.info("collection: "); - LOG.info(collectionResult.getId()); - LOG.info(collectionResult.getName()); - LOG.info(collectionResult.getDescription()); + if (updatedCollections != null && !updatedCollections.isEmpty()) { + for (Collection collectionResult : updatedCollections) { + LOG.info("collection: "); + LOG.info(collectionResult.getId()); + LOG.info(collectionResult.getName()); + LOG.info(collectionResult.getDescription()); + } + } + + // TODO resolve collection methods + + // share collection to recipients + String shareCollectionId = appProperties.getProperty("SHARE_COLLECTION_ID"); + LOG.info("trying to share collection id: " + shareCollectionId); + String[] shareCollectionRecipients = new String[] {"alex.hong@bynder.com"}; + CollectionRecipientRight recipientRight = CollectionRecipientRight.EDIT; + CollectionShareQuery collectionShareQuery = new CollectionShareQuery(shareCollectionId, shareCollectionRecipients, recipientRight) + .setLoginRequired(false) + .setSendMail(true) + .setMessage("test"); + collectionService.shareCollection(collectionShareQuery).blockingSingle(); + + // addMediaToCollection + String addMediaToCollectionId = appProperties.getProperty("ADD_MEDIA_TO_COLLECTION_COLLECTION_ID"); + String mediaIdToAdd = appProperties.getProperty("ADD_MEDIA_TO_COLLECTION_MEDIA_ID"); + String[] addMediaIds = new String[] {mediaIdToAdd}; + + for (String mediaId : addMediaIds) { + LOG.info("test: " + mediaId); } - // TODO addMediaToCollection - // TODO removeMediaFromCollection - // TODO shareCollection + LOG.info("adding media ids: " + mediaIdToAdd); + CollectionAddMediaQuery collectionAddMediaQuery = new CollectionAddMediaQuery(addMediaToCollectionId, addMediaIds); + collectionService.addMediaToCollection(collectionAddMediaQuery).blockingSingle(); + + + CollectionInfoQuery addMediaCollectionInfoQuery = new CollectionInfoQuery(addMediaToCollectionId); + List mediaIdsFromCollection = collectionService.getCollectionMediaIds(addMediaCollectionInfoQuery).blockingSingle().body(); + for (String mediaId : mediaIdsFromCollection) { + LOG.info("media id: " + mediaId); + } + + // removeMediaFromCollection + String removeFromCollectionId = appProperties.getProperty("REMOVE_FROM_COLLECTION_ID"); + String mediaIdToRemove = appProperties.getProperty("REMOVE_MEDIA_ID_FROM_COLLECTION"); + String[] removeMediaIds = new String[] {mediaIdToRemove}; + LOG.info("removing media ids: " + mediaIdToRemove); + CollectionRemoveMediaQuery collectionRemoveMediaQuery = new CollectionRemoveMediaQuery(removeFromCollectionId, removeMediaIds); + collectionService.removeMediaFromCollection(collectionRemoveMediaQuery).blockingSingle(); + + CollectionInfoQuery removeMediaCollectionInfoQuery = new CollectionInfoQuery(removeFromCollectionId); + List mediaIdsFromCollectionAfterRemoval = collectionService.getCollectionMediaIds(removeMediaCollectionInfoQuery).blockingSingle().body(); + for (String mediaId : mediaIdsFromCollectionAfterRemoval) { + LOG.info("media id: " + mediaId); + } } } diff --git a/src/main/java/com/bynder/sdk/sample/MediaSample.java b/src/main/java/com/bynder/sdk/sample/MediaSample.java index 18cd6755..d038ff9f 100644 --- a/src/main/java/com/bynder/sdk/sample/MediaSample.java +++ b/src/main/java/com/bynder/sdk/sample/MediaSample.java @@ -43,28 +43,38 @@ public static void main(final String[] args) throws URISyntaxException, IOExcept // Call the API to request for media assets MediaQuery mediaQuery = new MediaQuery().setType(MediaType.IMAGE).setOrderBy(OrderBy.NAME_DESC).setLimit(10).setPage(1); List mediaList = assetService.getMediaList(mediaQuery).blockingSingle().body(); - for (Media media : mediaList) { - LOG.info(media.getId()); - LOG.info(media.getName()); + if (mediaList != null && !mediaList.isEmpty()) { + for (Media media : mediaList) { + LOG.info(media.getId()); + LOG.info(media.getName()); + } } - if (!mediaList.isEmpty()) { - // get media info for single asset - String mediaId = mediaList.get(0).getId(); - MediaInfoQuery mediaInfoQuery = new MediaInfoQuery(mediaId); - Media foundMedia = assetService.getMediaInfo(mediaInfoQuery).blockingSingle().body(); + // get media info for single asset + String mediaIdInfo = appProperties.getProperty("MEDIA_ID_FOR_INFO"); + MediaInfoQuery mediaInfoQuery = new MediaInfoQuery(mediaIdInfo); + Media foundMedia = assetService.getMediaInfo(mediaInfoQuery).blockingSingle().body(); + if (foundMedia != null) { LOG.info("get media info result: "); - LOG.info(foundMedia.getId()); - LOG.info(foundMedia.getName()); + LOG.info("Media ID: " + foundMedia.getId()); + LOG.info("Media Name: " + foundMedia.getName()); + LOG.info("Media Brand ID: " + foundMedia.getBrandId()); + } - // modify name of asset - MediaModifyQuery modifyQuery = new MediaModifyQuery(mediaId).setName("New Name"); - assetService.modifyMedia(modifyQuery).blockingSingle(); + // modify name of asset + String mediaIdForRename = appProperties.getProperty("MEDIA_ID_FOR_RENAME"); + MediaModifyQuery modifyQuery = new MediaModifyQuery(mediaIdForRename) + .setName("New Name Updated") + .setDescription("Test Updated Description"); + assetService.modifyMedia(modifyQuery).blockingSingle(); - Media updatedFoundMedia = assetService.getMediaInfo(mediaInfoQuery).blockingSingle().body(); + MediaInfoQuery mediaInfoQueryRename = new MediaInfoQuery(mediaIdForRename); + Media updatedFoundMedia = assetService.getMediaInfo(mediaInfoQueryRename).blockingSingle().body(); + if (updatedFoundMedia != null) { LOG.info("get updated media info result: "); LOG.info(updatedFoundMedia.getId()); LOG.info(updatedFoundMedia.getName()); + LOG.info(updatedFoundMedia.getDescription()); } } } \ No newline at end of file diff --git a/src/main/java/com/bynder/sdk/sample/MetapropertiesSample.java b/src/main/java/com/bynder/sdk/sample/MetapropertiesSample.java index e69de29b..a96fa854 100644 --- a/src/main/java/com/bynder/sdk/sample/MetapropertiesSample.java +++ b/src/main/java/com/bynder/sdk/sample/MetapropertiesSample.java @@ -0,0 +1,66 @@ +package com.bynder.sdk.sample; + +import com.bynder.sdk.configuration.Configuration; +import com.bynder.sdk.model.Metaproperty; +import com.bynder.sdk.model.MetapropertyOption; +import com.bynder.sdk.query.MetapropertyQuery; +import com.bynder.sdk.service.BynderClient; +import com.bynder.sdk.service.asset.AssetService; +import com.bynder.sdk.util.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +public class MetapropertiesSample { + private static final Logger LOG = LoggerFactory.getLogger(MetapropertiesSample.class); + + public static void main(final String[] args) throws URISyntaxException, IOException { + /** + * Loads app.properties file under src/main/resources + */ + Properties appProperties = Utils.loadConfig("app"); + + // Initialize BynderClient with a permanent token + BynderClient client = BynderClient.Builder.create( + new Configuration.Builder(new URL(appProperties.getProperty("BASE_URL"))) + .setPermanentToken(appProperties.getProperty("PERMANENT_TOKEN")).build()); + + + // Initialize asset service + AssetService assetService = client.getAssetService(); + + // get metaproperties + MetapropertyQuery metapropertyQuery = new MetapropertyQuery(); + + Map metapropertiesMap = assetService.getMetaproperties(metapropertyQuery).blockingSingle().body(); + + if (metapropertiesMap != null) { + for (Map.Entry metapropertyEntry : metapropertiesMap.entrySet()) { + LOG.info("current metaproperty"); + LOG.info("Key: " + metapropertyEntry.getKey()); + Metaproperty currentMetaproperty = metapropertyEntry.getValue(); + + LOG.info("ID: " + currentMetaproperty.getId()); + LOG.info("Name: " + currentMetaproperty.getName()); + LOG.info("Label: " + currentMetaproperty.getLabel()); + LOG.info("Type: " + currentMetaproperty.getType()); + List metapropertyOptionList = currentMetaproperty.getOptions(); + + // metaproperty options if found + if (metapropertyOptionList != null && !metapropertyOptionList.isEmpty()) { + for (MetapropertyOption metapropertyOption : metapropertyOptionList) { + LOG.info("Metaproperty Option ID: " + metapropertyOption.getId()); + LOG.info("Metaproperty Option Label: " + metapropertyOption.getLabel()); + LOG.info("Metaproperty Name: " + metapropertyOption.getName()); + } + } + } + } + } +} diff --git a/src/main/java/com/bynder/sdk/sample/SmartFiltersSample.java b/src/main/java/com/bynder/sdk/sample/SmartFiltersSample.java index 5dd20b29..1cb0e382 100644 --- a/src/main/java/com/bynder/sdk/sample/SmartFiltersSample.java +++ b/src/main/java/com/bynder/sdk/sample/SmartFiltersSample.java @@ -36,22 +36,23 @@ public static void main(final String[] args) throws URISyntaxException, IOExcept AssetService assetService = client.getAssetService(); List smartFilters = assetService.getSmartfilters().blockingSingle().body(); - for (Smartfilter smartFilter : smartFilters) { - LOG.info(smartFilter.getId()); - // smart filter metaproperties - List smartFilterMetaproperties = smartFilter.getMetaproperties(); - if (!smartFilterMetaproperties.isEmpty()) { - LOG.info("smart filter metaproperty ids:"); - for (String metaproperty : smartFilterMetaproperties) { - LOG.info(metaproperty); + if (smartFilters != null && !smartFilters.isEmpty()) { + for (Smartfilter smartFilter : smartFilters) { + LOG.info("Smart Filter ID: " + smartFilter.getId()); + // smart filter metaproperties + List smartFilterMetaproperties = smartFilter.getMetaproperties(); + if (!smartFilterMetaproperties.isEmpty()) { + LOG.info("smart filter metaproperty ids:"); + for (String metaproperty : smartFilterMetaproperties) { + LOG.info("Smart Filter Metaproperty ID: " + metaproperty); + } } - } - // labels - Map smartFilterLabels = smartFilter.getLabels(); - for (Map.Entry entry : smartFilterLabels.entrySet()) { - LOG.info("smart filter label: "); - LOG.info(entry.getKey() + " " + entry.getValue()); + // smart filter labels + Map smartFilterLabels = smartFilter.getLabels(); + for (Map.Entry entry : smartFilterLabels.entrySet()) { + LOG.info("smart filter label: " + entry.getKey() + " " + entry.getValue()); + } } } } diff --git a/src/main/java/com/bynder/sdk/sample/TagsSample.java b/src/main/java/com/bynder/sdk/sample/TagsSample.java index eb9d9963..a1aa6fd7 100644 --- a/src/main/java/com/bynder/sdk/sample/TagsSample.java +++ b/src/main/java/com/bynder/sdk/sample/TagsSample.java @@ -35,10 +35,12 @@ public static void main(final String[] args) throws URISyntaxException, IOExcept // get tags and media count for each tag List assetTags = assetService.getTags().blockingSingle().body(); - for (Tag assetTag : assetTags) { - LOG.info(assetTag.getId()); - LOG.info(assetTag.getTag()); - LOG.info(String.valueOf(assetTag.getMediaCount())); + if (assetTags != null && !assetTags.isEmpty()) { + for (Tag assetTag : assetTags) { + LOG.info("Asset Tag ID: " + assetTag.getId()); + LOG.info("Asset Tag: " + assetTag.getTag()); + LOG.info("Asset Tag Media Count: " + assetTag.getMediaCount()); + } } } } diff --git a/src/main/java/com/bynder/sdk/sample/UploadSample.java b/src/main/java/com/bynder/sdk/sample/UploadSample.java index f2a9c207..80b3788b 100644 --- a/src/main/java/com/bynder/sdk/sample/UploadSample.java +++ b/src/main/java/com/bynder/sdk/sample/UploadSample.java @@ -40,14 +40,14 @@ public static void main(final String[] args) throws URISyntaxException, IOExcept // Call the API to request for brands String brandId = ""; List brands = assetService.getBrands().blockingSingle().body(); - if (!brands.isEmpty()) { + if (brands != null && !brands.isEmpty()) { brandId = brands.get(0).getId(); } String filePath = "src/main/java/com/bynder/sdk/sample/testasset.png"; LOG.info(filePath); UploadQuery uploadQuery = new UploadQuery(filePath, brandId); - // Add the filename you want specify in this manner + // Add the filename you want to specify in this manner uploadQuery.setFileName("testasset.png"); SaveMediaResponse saveMediaResponse = assetService.uploadFile(uploadQuery).blockingSingle(); LOG.info(saveMediaResponse.getMediaId()); diff --git a/src/main/java/com/bynder/sdk/sample/UsageSample.java b/src/main/java/com/bynder/sdk/sample/UsageSample.java index 31cc6768..6ff6e427 100644 --- a/src/main/java/com/bynder/sdk/sample/UsageSample.java +++ b/src/main/java/com/bynder/sdk/sample/UsageSample.java @@ -8,12 +8,10 @@ import com.bynder.sdk.util.Utils; import java.io.IOException; -import java.lang.Integer; import java.net.URISyntaxException; import java.net.URL; import java.util.List; -import java.util.Map; import java.util.Properties; import org.slf4j.Logger; @@ -35,12 +33,15 @@ public static void main(final String[] args) throws URISyntaxException, IOExcept // Initialize asset service AssetService assetService = client.getAssetService(); - UsageQuery usageQuery= new UsageQuery(); + String mediaIdForAssetUsage = appProperties.getProperty("MEDIA_ID_FOR_ASSET_USAGE"); + UsageQuery usageQuery= new UsageQuery().setAssetId(mediaIdForAssetUsage); List assetUsages = assetService.getUsage(usageQuery).blockingSingle().body(); - // TODO use asset usage - for (Usage assetUsage : assetUsages) { - LOG.info(assetUsage.getAssetId()); + if (assetUsages != null && !assetUsages.isEmpty()) { + for (Usage assetUsage : assetUsages) { + LOG.info("Asset Usage ID: " + assetUsage.getId()); + LOG.info(assetUsage.getAssetId()); + } } } }