Skip to content

Commit

Permalink
API-1824 [BE][PHP SDK] Implement Sample Files for Functionality Testi…
Browse files Browse the repository at this point in the history
…ng (#66)

* API-1824 setup sample files PHP sdk

* API-1824 media sample methods, add uploads sample method call

* API-1824 list asset ids within collection, sample collections

* API-1824 asset usage sample

* API-1824 metaproperty changes, options/dependencies for sample file

* API-1824 dockerize bynder php sdk to run sample files

* API-1824 add additional download url method calls, add makefile/docker files, add README for instructions

* API-1824 use var_dump for BrandsSample.php

* API-1824 cleanup

* API-1824 use correct file path to sample image

* API-1824 update README for docker build
  • Loading branch information
ahongbynder authored Jan 3, 2024
1 parent f2ed9dd commit f7c7185
Show file tree
Hide file tree
Showing 13 changed files with 849 additions and 2 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ docker-compose*
.vscode
README.md
LICENSE
**/sample
8 changes: 8 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM composer:latest

WORKDIR /app

COPY composer.json ./
RUN composer install

COPY . .
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
run-php-sdk-docker:
docker-compose up -d

stop-php-sdk-docker:
docker-compose down

# make execute-php-sdk-sample sample-file-name=BrandsSample.php
# executes command in running container
execute-php-sdk-sample:
docker-compose exec bynder-php-sdk php /app/sample/$(sample-file-name)
172 changes: 171 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Media management.

Build the Docker image and tag it:
```bash
docker build -t bynder-php-sdk-tests
docker build . -t bynder-php-sdk-tests
```

Run the tests:
Expand All @@ -165,3 +165,173 @@ Or to run an individual test file:
```bash
./vendor/bin/phpunit tests/UtilTest.php
```

### Sample Files Functionality Testing

Scripts 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 `sample` create a file called `sample_config.php`. This file will be referenced from sample files.

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


Example `sample_config.php` file content:
```php
<?php
$bynderDomain = "portal.bynder.com";
$redirectUri = "https://google.com";
$clientId = <your OAuth2 client id>;
$clientSecret = <your OAuth2 client secret>;
$token = null;

// provide corresponding values to be used within API calls
// media id for info
$MEDIA_ID_FOR_INFO = "C078E8EE-C13A-4DA5-86EC8D6F335364EB";
// media id for download url
$MEDIA_ID_FOR_DOWNLOAD_URL = "C078E8EE-C13A-4DA5-86EC8D6F335364EB";
// media id for download url
$MEDIA_ITEM_ID_FOR_SPECIFIC_DOWNLOAD_URL = "C83B261D-715F-4188-809FE1214175A753";
// media id for renaming
$MEDIA_ID_FOR_RENAME = "C078E8EE-C13A-4DA5-86EC8D6F335364EB";
// media id for removal
$MEDIA_ID_FOR_REMOVAL = "C078E8EE-C13A-4DA5-86EC8D6F335364EB";
// collection id to get assets for
$GET_COLLECTION_ASSETS_ID = "615F03BB-D986-4786-B2C085D2F0718230";

// metaproperty id to get info for
$METAPROPERTY_ID_FOR_INFO = "0D563E99-218C-4613-86232D416EB7EA8A";
// metaproperty option id to get info for
$METAPROPERTY_OPTION_ID_FOR_INFO = "3C65AFA5-AC94-4223-A54757F373D209D6";
// metaproperty id to get dependency info for
$METAPROPERTY_ID_FOR_DEPENDENCY_INFO = "0D563E99-218C-4613-86232D416EB7EA8A";
// metaproperty id for specific option dependency
$METAPROPERTY_ID_FOR_SPECIFIC_OPTION_DEPEND = "0D563E99-218C-4613-86232D416EB7EA8A";
// metaproperty option id for specific option
$METAPROPERTY_OPTION_ID_FOR_SPECIFIC_OPTION_DEPEND = "DF1CF731-EFDF-484D-84BFD5CF8835B9D7";

// 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 `sample_config.php`.
Scripts will output authorization url to navigate to retrieve access code (will not open browser automatically, user must click link).
Access code is then provided to terminal prompt to retrieve an access token for API calls afterward.

### Command Line Instructions

Make sure both `composer` and `php` are installed locally. From root directory run `composer install` to install packages
form `composer.json`. Navigate to `sample` directory.

#### Brands Sample
```bash
php BrandsSample.php
```

Methods Used:
* getBrands()

#### Collections Sample
```bash
php CollectionsSample.php
```

Methods Used:
* getCollections($query)
* getCollectionAssets($collectionId)

#### Media Sample
```bash
php MediaSample.php
```

Methods Used:
* getDerivatives()
* getMediaList($query)
* getMediaInfo($mediaId)
* getMediaDownloadLocation($mediaId)
* getMediaDownloadLocationByVersion($mediaId, $version)
* getMediaDownloadLocationForAssetItem($mediaId, $itemId)
* modifyMedia($mediaId, $data)
* getMediaInfo($mediaId)
* deleteMedia($mediaId)

#### Metaproperties Sample
```bash
php MetapropertiesSample.php
```

Methods Used:
* getMetaproperties()
* getMetaproperty($metapropertyId)
* getMetapropertyDependencies($metapropertyId)
* getMetapropertyOptions($query)
* getMetapropetryGlobalOptionDependencies()
* getMetapropertyOptionDependencies($metapropertyId)
* getMetapropertySpecificOptionDependencies($metapropertyId, $metapropertyOptionId, $array)

#### Smart Filters Sample
```bash
php SmartFiltersSample.php
```

Methods Used:
* getSmartfilters()


#### Tags Sample
```bash
php TagsSample.php
```

Methods Used:
* getTags()

#### Uploads Sample
```bash
php UploadsSample.php
```

Methods Used:
* uploadFileAsync($data)
* getBrands()

#### Usage Sample
```bash
php UsageSample.php
```

Methods Used:
* createUsage($data)
* getUsage($data)
* deleteUsage($data)


### Docker Instructions

Sample files can be executed within Docker container. Makefile contains corresponding commands to run/build Docker container.

`Dockerfile.dev` file is used for container.

Makefile commands are executed from root directory. Run with sudo if permission is needed.

If needed, pull latest `composer` Docker image `docker pull composer:latest`

#### Makefile commands:

Build and start up Docker container for PHP SDK using Docker Compose
```bash
make run-php-sdk-docker
```

Stop running Docker container for PHP SDK:
```bash
make stop-php-sdk-docker
```

Run sample file within Docker container (BrandsSample.php is replaced with target sample file):
```bash
make execute-php-sdk-sample sample-file-name=BrandsSample.php
```
10 changes: 10 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.9'

services:
bynder-php-sdk:
container_name: bynder-php-sdk
build:
dockerfile: Dockerfile.dev
command: sh -c "tail -f /dev/null"
volumes:
- .:/app
58 changes: 58 additions & 0 deletions sample/BrandsSample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/sample_config.php');
use Bynder\Api\BynderClient;
use Bynder\Api\Impl\OAuth2;

try {
// instantiate BynderClient to make API requests for portal, client id, client secret with redirect uri
$bynder = new BynderClient(new Oauth2\Configuration(
$bynderDomain,
$redirectUri,
$clientId,
$clientSecret,
$token,
['timeout' => 5] // Guzzle HTTP request options
));

// if no access token, need to use OAuth flow to authorize and get access code, then use code to get token
if ($token === null) {
echo $bynder->getAuthorizationUrl([
'offline',
'current.user:read',
'current.profile:read',
'asset:read',
'asset:write',
'meta.assetbank:read',
'asset.usage:read',
'asset.usage:write',
]) . "\n\n";

$code = readline('Enter code: ');

if ($code == null) {
echo("Failed to get access token");
exit;
}

$token = $bynder->getAccessToken($code);
}

$assetBankManager = $bynder->getAssetBankManager();

// Get Brands. Returns a Promise.
$brandsListPromise = $assetBankManager->getBrands();
// Wait for the promise to be resolved.
$brandsList = $brandsListPromise->wait();

if (!empty($brandsList)) {
foreach ($brandsList as $brand) {
echo("Brand ID: " . $brand['id'] . "\n");
var_dump($brand);
}
}
} catch (Exception $e) {
var_dump($e);
}
?>

74 changes: 74 additions & 0 deletions sample/CollectionsSample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/sample_config.php');
use Bynder\Api\BynderClient;
use Bynder\Api\Impl\OAuth2;

try {
// instantiate BynderClient to make API requests for portal, client id, client secret with redirect uri
$bynder = new BynderClient(new Oauth2\Configuration(
$bynderDomain,
$redirectUri,
$clientId,
$clientSecret,
$token,
['timeout' => 5] // Guzzle HTTP request options
));

// if no access token, need to use OAuth flow to authorize and get access code, then use code to get token
if ($token === null) {
echo $bynder->getAuthorizationUrl([
'offline',
'current.user:read',
'current.profile:read',
'asset:read',
'asset:write',
'meta.assetbank:read',
'asset.usage:read',
'asset.usage:write',
'collection:read',
'collection:write'
]) . "\n\n";

$code = readline('Enter code: ');

if ($code == null) {
echo("Failed to get access token");
exit;
}

$token = $bynder->getAccessToken($code);
}

$assetBankManager = $bynder->getAssetBankManager();

// Get Collections List.
// optional filter
$collectionQueryFilter = [
'count' => true,
'limit' => 20
];
$collectionListPromise = $assetBankManager->getCollections($collectionQueryFilter);
$collectionsList = $collectionListPromise->wait();

// print collection list, each collection result
if (!empty($collectionsList) && !empty($collectionsList['collections'])) {
foreach ($collectionsList['collections'] as $collection) {
echo("Collection ID: " . $collection['id'] . "\n");
var_dump($collection);
}
}

// get collection assets for a collection
$collectionsAssetPromise = $assetBankManager->getCollectionAssets($GET_COLLECTION_ASSETS_ID);
$collectionAssets = $collectionsAssetPromise->wait();

if (!empty($collectionAssets)) {
echo("Collection Asset IDs for ID: " . $GET_COLLECTION_ASSETS_ID);
var_dump($collectionAssets);
}

} catch (Exception $e) {
var_dump($e);
}
?>
Loading

0 comments on commit f7c7185

Please sign in to comment.