Skip to content

Commit

Permalink
Version 1.0.0 SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcsantos committed Mar 8, 2017
1 parent d738e7c commit 411fbe8
Show file tree
Hide file tree
Showing 20 changed files with 2,280 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
/nbproject/
/vendor/
composer.lock
docker-compose.yml
index.php
site.conf
*.jpg
*.jpeg
*.txt
tests/*/config.json
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Bynder B.V.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
115 changes: 113 additions & 2 deletions README.md
@@ -1,2 +1,113 @@
# bynder-php-sdk
SDK in PHP for integration with Bynder
# Bynder PHP SDK

The main goal of this SDK is to speed up the integration of Bynder customers who use PHP. Making it easier to connect to the Bynder API (http://docs.bynder.apiary.io) and executing requests on it.

## Requirements and dependencies

The PHP SDK requires the following in order to fully work:

- [`PHP >= 5.6`](https://secure.php.net/manual/en/book.curl.php), older versions of PHP not recommended
- [`curl`](https://secure.php.net/manual/en/book.curl.php), although you can use your own non-cURL client if you prefer

Composer should handle all the dependencies automatically.

## Installation

This SDK depends on a few libraries in order to work, installing it with Composer should take care of everything automatically.

To install the SDK with [Composer](http://getcomposer.org/). Run the following command on the root of the project:

```bash
composer install
```

To use the SDK, we use Composer's [autoload](https://getcomposer.org/doc/00-intro.md#autoloading) in order to include all the files automatically:

```php
require_once('vendor/autoload.php');
```

## How to use it

This is a simple example on how to retrieve data from the Bynder asset bank. For a more detailed example of implementation refer to the [sample code](https://github.com/Bynder/bynder-php-sdk/blob/develop/sample/sample.php).

Before executing any request to the Bynder API we need to instantiate the **BynderApi** class, the following example shows how to use the **BynderApiFactory** to construct a **BynderApi** instance:
```php
$bynderApi = BynderApiFactory::create(
array(
'consumerKey' => BYNDER_CONSUMER_KEY,
'consumerSecret' => BYNDER_CONSUMER_SECRET,
'token' => BYNDER_CLIENT_KEY,
'tokenSecret' => BYNDER_CLIENT_SECRET,
'baseUrl' => BYNDER_URL
)
);
```
After getting the **BynderApi** service configured successfully we need to get an instance of the **AssetBankManager** in order to do any of the API calls relative to the Bynder Asset Bank module:

```php
$assetBankManager = $bynderApi->getAssetBankManager();
```
And with this, we can start our request to the API, listed in the **Methods Available** section following. Short example of getting all the **Media Items**:

```php
$mediaList = $assetBankManager->getMediaList();
```
This call will return a list with all the Media Items available in the Bynder environment. Note that some of the calls accept a query array in order to filter the results via the API call params (see [Bynder API Docs](http://docs.bynder.apiary.io/)) for more details.
For instance, if we only wanted to retrieve **2 images** here is what the call would look like:
```php
$mediaList = $assetBankManager->getMediaList(
array(
'limit' => 2,
'type' => 'image')
);
```

All the calls are **Asynchronous**, which means they will return a **Promise** object, making it a bit more flexible in order to adjust to any kind of application.
Again, for more a thourough example there is a sample [application use case](https://github.com/Bynder/bynder-php-sdk/blob/develop/sample/sample.php) in this repo.

## Methods Available
These are the methods currently availble on the **Bynder PHP SDK**, refer to the [Bynder API Docs](http://docs.bynder.apiary.io/)) for more specific details on the calls.

#### BynderApi:
Gets an instance of the Asset Bank Manager service if already with access tokens set up.
Also allows to generate and authenticate request tokens, which are necessary for the rest of
the Asset Bank calls.
```php
getAssetBankManager();
getRequestToken();
authoriseRequestToken($query);
getAccessToken();
setAccessTokenCredentials($token, $tokenSecret);
userLogin($username, $password);
userLogout();
```


#### AssetBankManager:
All the Asset Bank related calls, provides information and access to
Media management.
```php
getBrands();
getMediaList($query);
getMediaInfo($mediaId, $versions);
getMetaproperties();
getTags();
getCategories();
uploadFileAsync($data);
deleteMedia($mediaId);
```

## Tests

Install dependencies as mentioned above (which will resolve [PHPUnit](http://packagist.org/packages/phpunit/phpunit)), then you can run the test suite:

```bash
./vendor/bin/phpunit
```

Or to run an individual test file:

```bash
./vendor/bin/phpunit tests/UtilTest.php
```
33 changes: 33 additions & 0 deletions composer.json
@@ -0,0 +1,33 @@
{
"name": "Bynder/bynder-php-sdk",
"description": "Bynder PHP Library",
"keywords": [
"bynder",
"sdk",
"api"
],
"homepage": "https://www.bynder.com",
"license": "MIT",
"authors": [
{
"name": "Bynder",
"homepage": "https://github.com/Bynder/bynder-php-sdk"
}
],
"require": {
"php": ">= 5.6",
"guzzlehttp/guzzle": "~6.0",
"guzzlehttp/oauth-subscriber": "0.3.*"
},
"require-dev": {
"phpunit/phpunit": "5.5.*",
"mikey179/vfsStream": "~1",
"phpdocumentor/phpdocumentor": "2.*"
},
"autoload": {
"psr-4": { "Bynder\\" : "src/Bynder" }
},
"autoload-dev": {
"psr-4": { "Bynder\\Tests\\": "tests" }
}
}
112 changes: 112 additions & 0 deletions sample/sample.php
@@ -0,0 +1,112 @@
<?php

require_once('vendor/autoload.php');

use Bynder\Api\BynderApiFactory;
use Bynder\Api\Impl\BynderApi;

$settings = array(
'consumerKey' => BYNDER_CONSUMER_KEY,
'consumerSecret' => BYNDER_CONSUMER_SECRET,
'token' => BYNDER_CLIENT_KEY,
'tokenSecret' => BYNDER_CLIENT_SECRET,
'baseUrl' => BYNDER_URL
);

$haveTokens = true;

try {

$bynderApi = BynderApiFactory::create($settings);

// Deprecated username/password login
// $tokens = $bynderApi->userLogin('username', 'password')->wait();

// If we want to test the Login functionality make sure we don't pass the access token and secret in the settings.
if (!$haveTokens && !isset($_GET['oauth_token'])) {
// Get the request token
$token = $bynderApi->getRequestToken()->wait();
$tokenArray = explode('&', $token);
// Storing this for later use because we're about to do a redirect.
file_put_contents('tokens.txt', json_encode($tokenArray));
$token = explode('=', $tokenArray[0])[1];
$tokenSecret = explode('=', $tokenArray[1])[1];
$query = array(
'oauth_token' => $token,
// Would be the url pointing to this script for example.
'callback' => 'CALLBACK URL'
);

// Authorise the request token and redirect the user to the login page.
$requestTokens = $bynderApi->authoriseRequestToken($query)->wait();
preg_match("/redirectToken=(.*)\"/", $requestTokens, $output_array);
header('Location: ' . BYNDER_URL . 'login/?redirectToken=' . $output_array[1]);
exit();
} // Here we're handling a redirect after a login.
elseif (!$haveTokens) {
// Get the request tokens we stored earlier.
$tokens = json_decode(file_get_contents('tokens.txt'), true);
$token = explode('=', $tokens[0])[1];
$tokenSecret = explode('=', $tokens[1])[1];
$settings = array(
'consumerKey' => BYNDER_CONSUMER_KEY,
'consumerSecret' => BYNDER_CONSUMER_SECRET,
'token' => $token,
'tokenSecret' => $tokenSecret,
'baseUrl' => BYNDER_URL
);
$bynderApi = BynderApiFactory::create($settings);

// Exchanging the authorised request token for an access token.
$token = $bynderApi->getAccessToken()->wait();
}

$assetBankManager = $bynderApi->getAssetBankManager();

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

// Get Media Items list.
// Optional filter.
$query = array(
'count' => true,
'limit' => 2,
'type' => 'image',
'versions' => 1
);

$mediaListPromise = $assetBankManager->getMediaList($query);
$mediaList = $mediaListPromise->wait();
var_dump($mediaList);

// Get specific Media Item info.
$mediaId = array_pop($mediaList['media'])['id'];
$mediaItemPromise = $assetBankManager->getMediaInfo($mediaId, $query);
$mediaItem = $mediaItemPromise->wait();
var_dump($mediaItem);

// Get Metaproperties.
$metapropertiesListPromise = $assetBankManager->getMetaproperties();
$metapropertiesList = $metapropertiesListPromise->wait();
var_dump($metapropertiesList);

// Get Tags.
$tagsListPromise = $assetBankManager->getTags();
$tagsList = $tagsListPromise->wait();
var_dump($tagsList);

$data = array(
'filePath' => 'test.jpg',
'brandId' => $brandsList[0]['id'],
'name' => 'Image name',
'description' => 'Image description'
);
$filePromise = $assetBankManager->uploadFileAsync($data);
$fileInfo = $filePromise->wait();
var_dump($fileInfo);
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
33 changes: 33 additions & 0 deletions src/Bynder/Api/BynderApiFactory.php
@@ -0,0 +1,33 @@
<?php

/**
* Copyright (c) Bynder. All rights reserved.
* Licensed under the MIT License. For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

// src/Bynder/Api/BynderApiFactory.php
namespace Bynder\Api;

use Bynder\Api\Impl\BynderApi;
use InvalidArgumentException;

/**
* Static Factory class used to create instances of BynderApi.
*/
class BynderApiFactory
{

/**
* Creates an instance of BynderApi using the given settings.
*
* @param array $settings Oauth credentials and settings to configure the BynderApi instance.
* @return BynderApi instance.
* @throws InvalidArgumentException Oauth settings not valid, consumer key or secret not in array.
*/
public static function create($settings)
{
return BynderApi::create($settings);
}

}

0 comments on commit 411fbe8

Please sign in to comment.