Commit cf0c087a authored by Björn Hjortsten's avatar Björn Hjortsten

Added base functions as well as folder functionality

parent 6dad5d1c
{
"name": "qbnk/templafy-apiwrapper",
"description": "An API wrapper for Templafy",
"minimum-stability": "stable",
"prefer-stable": true,
"license": "MIT",
"authors": [
{
"name": "Björn Hjortsten",
"email": "bjorn.hjortsten@qbank.se"
}
],
"autoload": {
"psr-4": {
"QBNK\\TemplafyApi\\": "src"
}
},
"require": {
"php" : "~7.2",
"guzzlehttp/guzzle": "~6.1",
"sainsburys/guzzle-oauth2-plugin": "~3.0"
},
"require-dev": {
"roave/security-advisories": "dev-master"
}
}
\ No newline at end of file
This diff is collapsed.
<?php
declare(strict_types=1);
namespace QBNK\TemplafyApi\Controller;
use GuzzleHttp\Client;
class BaseController {
/** @var Client */
protected $apiHttpClient;
public function __construct($apiHttpClient) {
$this->apiHttpClient = $apiHttpClient;
}
}
\ No newline at end of file
<?php
declare(strict_types=1);
namespace QBNK\TemplafyApi\Controller;
use QBNK\TemplafyApi\Model\Folder;
use QBNK\TemplafyApi\Model\FolderResponse;
class FolderController extends BaseController {
/**
* @param string|null $parentFolderId
* @param string|null $navigationPath Hierarchical path identifying a parent folder
* @return FolderResponse[]
*/
public function list(string $parentFolderId = null, string $navigationPath = null): array {
$queryParameters = ['libraryId' => 'Image'];
if ($parentFolderId !== null) {
$queryParameters['parentFolderId'] = $parentFolderId;
}
if ($navigationPath !== null) {
$queryParameters['navigationPath'] = $navigationPath;
}
$response = $this->apiHttpClient->get('/folders', ['query' => $queryParameters]);
$responseData = \GuzzleHttp\json_decode($response->getBody(), true);
$result = [];
foreach ($responseData as $rawFolder) {
$result[] = FolderResponse::fromArray($rawFolder);
}
return $result;
}
public function create(Folder $folder) {
$response = $this->apiHttpClient->post('/folders', ['json' => $folder]);
$responseData = \GuzzleHttp\json_decode($response->getBody(), true);
var_dump($response, $responseData); // TODO examine result and return appropriate data
}
public function get(string $id): FolderResponse {
$response = $this->apiHttpClient->get('/folders/'.$id);
$responseData = \GuzzleHttp\json_decode($response->getBody(), true);
return FolderResponse::fromArray($responseData);
}
public function update(string $id, Folder $folder): void {
$this->apiHttpClient->put('folders/'.$id, ['json' => $folder]);
}
public function delete(string $id): void {
$this->apiHttpClient->delete('/folders/'.$id);
}
}
\ No newline at end of file
<?php
declare(strict_types=1);
namespace QBNK\TemplafyApi;
class Credentials {
/** @var string */
protected $clientId;
/** @var string */
protected $username;
/**
* @param string $clientId
* @param string $clientSecret
*/
public function __construct($clientId, $clientSecret) {
$this->clientId = $clientId;
$this->clientSecret($clientSecret);
}
/**
* Gets or sets the internal value
* @internal Hack to hide value from dumping and possibly exposing by mistake.
* @param string|null $newClientSecret
* @return string
*/
protected function clientSecret($newClientSecret = null): string {
static $clientSecret;
if ($newClientSecret !== null) {
$clientSecret = $newClientSecret;
}
return $clientSecret ?? '';
}
public function getClientId(): string {
return $this->clientId;
}
public function getClientSecret(): string {
return $this->clientSecret();
}
}
\ No newline at end of file
<?php
namespace QBNK\TemplafyApi\Model;
use JsonSerializable;
class Folder implements JsonSerializable {
/** @var string */
protected $name;
/** @var string */
protected $parentFolderId;
/**
* @param string $name
* @return self
*/
public function setName(string $name) {
$this->name = $name;
return $this;
}
/**
* @return string
*/
public function getName(): string {
return $this->name ?? '';
}
/**
* @param string $parentFolderId
* @return self
*/
public function setParentFolderId(string $parentFolderId) {
$this->parentFolderId = $parentFolderId;
return $this;
}
/**
* @return string
*/
public function getParentFolderId(): string {
return $this->parentFolderId ?? '';
}
public static function fromArray(array $data) {
$instance = new static();
$instance
->setName($data['name'])
->setParentFolderId($data['parentFolderId'])
;
return $instance;
}
/**
* Specify data which should be serialized to JSON
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize() {
return [
'name' => $this->getName(),
'parentFolderId' => $this->getParentFolderId()
];
}
}
\ No newline at end of file
<?php
namespace QBNK\TemplafyApi\Model;
class FolderResponse extends Folder {
/** @var string */
protected $id;
/** @var string */
protected $navigationPath;
/**
* @param string $id
* @return self
*/
public function setId(string $id) {
$this->id = $id;
return $this;
}
/**
* @return string
*/
public function getId(): string {
return $this->id ?? '';
}
/**
* @param string $navigationPath
* @return self
*/
public function setNavigationPath(string $navigationPath) {
$this->navigationPath = $navigationPath;
return $this;
}
/**
* @return string
*/
public function getNavigationPath(): string {
return $this->navigationPath ?? '';
}
public static function fromArray(array $data) {
$instance = parent::fromArray($data);
$instance
->setId($data['id'])
->setNavigationPath($data['navigationPath'])
;
return $instance;
}
}
\ No newline at end of file
<?php
declare(strict_types=1);
namespace QBNK\TemplafyApi;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use QBNK\TemplafyApi\Controller\FolderController;
use Sainsburys\Guzzle\Oauth2\GrantType\ClientCredentials;
use Sainsburys\Guzzle\Oauth2\GrantType\RefreshToken;
use Sainsburys\Guzzle\Oauth2\Middleware\OAuthMiddleware;
class TemplafyApi {
public const API_URL = 'https://api-v1.templafy.com';
public const API_TOKEN_URL = 'oauth2/token';
/** @var Credentials */
protected $credentials;
/** @var Client */
protected $apiClient;
/** @var OAuthMiddleware */
protected $oauth2Middleware;
/** @var FolderController */
protected $folderController;
public function __construct(Credentials $credentials, array $options = []) {
$this->credentials = $credentials;
}
public function getHttpClient(): Client {
if (!($this->apiClient instanceof Client)) {
$handlerStack = HandlerStack::create();
$oauthClient = new Client([
'base_uri' => self::API_URL,
'verify' => true,
'headers' => [
'User-Agent' => 'templafy-apiwrapper/1 (guzzle: 6)',
],
]);
$config = [
ClientCredentials::CONFIG_CLIENT_ID => $this->credentials->getClientId(),
ClientCredentials::CONFIG_CLIENT_SECRET => $this->credentials->getClientSecret(),
ClientCredentials::CONFIG_TOKEN_URL => self::API_TOKEN_URL
];
$this->oauth2Middleware = new OAuthMiddleware(
$oauthClient,
new ClientCredentials($oauthClient, $config),
new RefreshToken($oauthClient, $config)
);
$handlerStack->push($this->oauth2Middleware->onBefore());
$handlerStack->push($this->oauth2Middleware->onFailure(3));
$apiClient = new Client([
'handler' => $handlerStack,
'auth' => 'oauth2',
'base_uri' => self::API_URL,
'headers' => [
'Accept' => 'application/json',
'Content-type' => 'application/json',
'User-Agent' => 'templafy-apiwrapper/1 (guzzle: 6)',
]
]);
$this->apiClient = $apiClient;
}
return $this->apiClient;
}
public function folders() {
if (!($this->folderController instanceof FolderController)) {
$this->folderController = new FolderController($this->getHttpClient());
}
return $this->folderController;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment