Skip to content

Commit

Permalink
API-1822 [BE] [SDK] Implement Sample Files for JAVA-SDK Functionality…
Browse files Browse the repository at this point in the history
… Testing (#114)

* API-1822 setup sample files for java sdk api calls

* API-1822 add upload sample, smart filters sample, usage sample

* API-1822 add metaproperties sample, update logging for samples, setup methods for collection sample

* API-1822 update collections sample with share, add media, remove media

* API-1822 resolve package name

* API-1822 setup asset usage sample

* API-1822 use OAuth for client, add testasset image for upload

* API-1822 add media removal, add README, add null checks for collections

* API-1822 update readme for sample files

* API-1822 get derivatives for portal

* API-1822 update readme for derivatives method

* API-1822 cleanup

* API-1822 add System.exit(0) to terminate processes

* API-1822 cleanup
  • Loading branch information
ahongbynder authored Dec 7, 2023
1 parent 64acb65 commit 4331018
Show file tree
Hide file tree
Showing 10 changed files with 963 additions and 0 deletions.
163 changes: 163 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,166 @@ Response<List<Tag>> tagsResponse = assetService.getTags().blockingSingle();
// Get media (request with query)
Response<List<Media>> mediaResponse = assetService.getMediaList(new MediaQuery().setType(MediaType.IMAGE).setLimit(100).setPage(1)).blockingSingle();
```

### Sample Files Functionality Testing

Classes within `sample` contain code to execute corresponding functionalities. The purpose is to demonstrate how methods
are called and provide a convenient method to execute functions.

Within `src/main/resources` create an `app.properties` file. This file will be referenced from sample files.

Make sure all values are populated correctly before running sample files.

Example `app.properties` file content:

```java
# permanent token if using permanent token auth
PERMANENT_TOKEN=<your auth permanent token>
# portal url
BASE_URL=https://portal.bynder.com
# OAuth info
REDIRECT_URI=https://google.com
CLIENT_ID=<your OAuth2 client id>
CLIENT_SECRET=<your OAuth2 client secret>
# media id for info
MEDIA_ID_FOR_INFO=5B8357A7-5DEB-4BC7-9CFBDEE1ECE120A9
# media id for renaming
MEDIA_ID_FOR_RENAME=5B8357A7-5DEB-4BC7-9CFBDEE1ECE120A9
# media id for removal
MEDIA_ID_FOR_REMOVAL=946A1800-6298-4201-AEB8F2830B07400E
# collection id to get info for
GET_COLLECTION_INFO_ID=615F03BB-D986-4786-B2C085D2F0718230
# collection id to share
SHARE_COLLECTION_ID=615F03BB-D986-4786-B2C085D2F0718230
# recipient to receive shared collection
COLLECTION_SHARE_RECIPIENT=recipient@mail.com
# media id to add to collection
ADD_MEDIA_TO_COLLECTION_MEDIA_ID=C078E8EE-C13A-4DA5-86EC8D6F335364EB
# collection id for media to be added to
ADD_MEDIA_TO_COLLECTION_COLLECTION_ID=615F03BB-D986-4786-B2C085D2F0718230
# collection id to remove asset from
REMOVE_FROM_COLLECTION_ID=615F03BB-D986-4786-B2C085D2F0718230
# media id to remove from collection
REMOVE_MEDIA_ID_FROM_COLLECTION=C078E8EE-C13A-4DA5-86EC8D6F335364EB
# media id used for creating asset usage
MEDIA_ID_FOR_ASSET_USAGE=C078E8EE-C13A-4DA5-86EC8D6F335364EB
# integration id used for asset usage
INTEGRATION_ID_FOR_ASSET_USAGE=0191a303-9d99-433e-ada4-d244f37e1d7d
```
Within each sample file, OAuth credentials are read in from `app.properties`.
This will prompt the browser to open to retrieve an access code and then redirected to the redirect URI.
Access code is then provided to terminal prompt to retrieve an access token for API calls afterward.


#### Maven

Make sure `mvn` CLI is installed.

From root directory, dependencies can be installed from `pom.xml` using command:
```bash
mvn clean install -Dgpg.skip -Dmaven.javadoc.skip
```

#### Brands Sample

Execute `BrandsSample.java` file with command

```bash
mvn compile exec:java -Dexec.mainClass=com.bynder.sdk.sample.BrandsSample
```

Methods Used:
* getBrands()

#### Collections Sample

Execute `CollectionsSample.java` file with command

```bash
mvn compile exec:java -Dexec.mainClass=com.bynder.sdk.sample.CollectionsSample
```

Methods Used:
* getCollections(collectionQuery)
* getCollectionInfo(collectionInfoQuery)
* createCollection(createCollectionQuery)
* shareCollection(collectionShareQuery)
* addMediaToCollection(collectionAddMediaQuery)
* getCollectionMediaIds(addMediaCollectionInfoQuery)
* removeMediaFromCollection(collectionRemoveMediaQuery)

#### Media Sample

Execute `MediaSample.java` file with command

```bash
mvn compile exec:java -Dexec.mainClass=com.bynder.sdk.sample.MediaSample
```

Methods Used:

* getMediaList(mediaQuery)
* getMediaInfo(mediaInfoQuery)
* getMediaDownloadUrl(mediaDownloadQuery)
* modifyMedia(modifyQuery)
* getMediaInfo(mediaInfoQueryRename)
* deleteMedia(mediaDeleteQuery)
* getDerivatives()

#### Metaproperties Sample

Execute `MetapropertiesSample.java` file with command

```bash
mvn compile exec:java -Dexec.mainClass=com.bynder.sdk.sample.MetapropertiesSample
```

Methods Used:
* getMetaproperties(metapropertyQuery)

#### Smart Filters Sample

Execute `SmartFiltersSample.java` file with command

```bash
mvn compile exec:java -Dexec.mainClass=com.bynder.sdk.sample.SmartFiltersSample
```

Methods Used:
* getSmartfilters()
* smartFilter.getMetaproperties()
* smartFilter.getLabels()

#### Tags Sample

Execute `TagsSample.java` file with command

```bash
mvn compile exec:java -Dexec.mainClass=com.bynder.sdk.sample.TagsSample
```

Methods Used:
* getTags()

#### Upload Sample

Execute `UploadSample.java` file with command

```bash
mvn compile exec:java -Dexec.mainClass=com.bynder.sdk.sample.UploadSample
```

Methods Used:
* uploadFile(uploadQuery)

#### Usage Sample

Execute `UsageSample.java` file with command

```bash
mvn compile exec:java -Dexec.mainClass=com.bynder.sdk.sample.UsageSample
```
Methods Used:
* createUsage(usageCreateQuery)
* getUsage(usageQuery)
* deleteUsage(usageDeleteQuery)
78 changes: 78 additions & 0 deletions src/main/java/com/bynder/sdk/sample/BrandsSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.bynder.sdk.sample;

import com.bynder.sdk.configuration.Configuration;
import com.bynder.sdk.configuration.HttpConnectionSettings;
import com.bynder.sdk.configuration.OAuthSettings;
import com.bynder.sdk.model.Brand;
import com.bynder.sdk.service.BynderClient;
import com.bynder.sdk.service.asset.AssetService;
import com.bynder.sdk.service.oauth.OAuthService;
import com.bynder.sdk.util.Utils;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class BrandsSample {
private static final Logger LOG = LoggerFactory.getLogger(BrandsSample.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 OAuth
OAuthSettings oAuthSettings = new OAuthSettings(appProperties.getProperty("CLIENT_ID"), appProperties.getProperty("CLIENT_SECRET"), new URI(appProperties.getProperty("REDIRECT_URI")));
BynderClient client = BynderClient.Builder.create(
new Configuration.Builder(new URL(appProperties.getProperty("BASE_URL")))
.setOAuthSettings(oAuthSettings)
.setHttpConnectionSettings(new HttpConnectionSettings()).build());
List<String> scopes = Arrays.asList("offline", "asset:read", "asset:write", "asset.usage:read",
"asset.usage:write", "collection:read", "collection:write", "meta.assetbank:read",
"meta.assetbank:write", "meta.workflow:read");

// Initialize OAuthService
OAuthService oauthService = client.getOAuthService();
URL authorizationUrl = oauthService.getAuthorizationUrl("state example", scopes);

// Open browser with authorization URL
Desktop desktop = Desktop.getDesktop();
desktop.browse(authorizationUrl.toURI());

// Ask for the code returned in the redirect URI
System.out.println("Insert the code: ");
Scanner scanner = new Scanner(System.in);
String code = scanner.nextLine();
scanner.close();

// Get the access token
oauthService.getAccessToken(code, scopes).blockingSingle();

// Initialize asset service
AssetService assetService = client.getAssetService();

// Call the API to request for brands
List<Brand> brands = assetService.getBrands().blockingSingle().body();
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());
}
}
System.exit(0);
}
}
Loading

0 comments on commit 4331018

Please sign in to comment.