Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tistre committed Jan 10, 2024
2 parents 4429d7a + 1c6df21 commit 5ac101d
Show file tree
Hide file tree
Showing 25 changed files with 280 additions and 145 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,12 @@
# Change Log

## 6.0.0 - 2024-01-10

Backward incompatible API change: Introducing the AssetId class. Asset IDs must now be passed as an AssetId instance
instead of a string, and are returned as AssetId (in AssetResponse).

Added support for some Assets Admin functionality: MetricsRequest, ActiveUsersRequest (private API).

## 5.1.1 - 2023-11-27

PHP 8.3 is now supported.
Expand Down
29 changes: 29 additions & 0 deletions src/AssetId.php
@@ -0,0 +1,29 @@
<?php

namespace DerSpiegel\WoodWingAssetsClient;

use InvalidArgumentException;
use Stringable;


readonly class AssetId implements Stringable
{
public function __construct(public string $id)
{
if (!self::isValid($id)) {
throw new InvalidArgumentException('Not a valid asset ID');
}
}


public function __toString(): string
{
return $this->id;
}


public static function isValid(string $id): bool
{
return (strlen($id) === 22);
}
}
53 changes: 49 additions & 4 deletions src/AssetsClient.php
Expand Up @@ -257,17 +257,62 @@ public function rawServiceRequest(string $service, array $data = []): ResponseIn

/**
* @param string $method
* @param string $service
* @param string $urlPath
* @param array $data
* @return array
* @throws JsonException
*/
public function apiRequest(string $method, string $service, array $data = []): array
public function apiRequest(string $method, string $urlPath, array $data = []): array
{
$url = sprintf(
'%sapi/%s',
$this->config->url,
$service
$urlPath
);

try {
$httpResponse = $this->request($method, $url, $data, false);

$responseBbody = (string)$httpResponse->getBody();

if (empty($responseBbody)) {
return [];
}

return AssetsUtils::parseJsonResponse($responseBbody);
} catch (Exception $e) {
switch ($e->getCode()) {
case 401: // Unauthorized
// TODO: prevent a possible loop here?
// re-login
if (!$this->reLogin()) {
throw $e;
}

// try again
return $this->apiRequest($method, $urlPath, $data);
default:
// something went wrong
throw $e;
}
}
}


/**
* @param string $method
* @param string $urlPath
* @param array $data
* @return array
* @throws JsonException
*/
public function privateApiRequest(string $method, string $urlPath, array $data = []): array
{
$url = sprintf(
'%sprivate-api/%s',
$this->config->url,
$urlPath
);

try {
Expand All @@ -291,7 +336,7 @@ public function apiRequest(string $method, string $service, array $data = []): a
}

// try again
return $this->apiRequest($method, $service, $data);
return $this->privateApiRequest($method, $urlPath, $data);
default:
// something went wrong
throw $e;
Expand Down
11 changes: 0 additions & 11 deletions src/AssetsUtils.php
Expand Up @@ -19,17 +19,6 @@
*/
class AssetsUtils
{

/**
* @param string $id
* @return bool
*/
public static function isAssetsId(string $id): bool
{
return (strlen($id) === 22);
}


/**
* Parse JSON response string into array, throw exception on error response
*
Expand Down
5 changes: 3 additions & 2 deletions src/Helper/AddToContainerRequest.php
Expand Up @@ -2,6 +2,7 @@

namespace DerSpiegel\WoodWingAssetsClient\Helper;

use DerSpiegel\WoodWingAssetsClient\AssetId;
use DerSpiegel\WoodWingAssetsClient\AssetsClient;
use DerSpiegel\WoodWingAssetsClient\RelationType;
use DerSpiegel\WoodWingAssetsClient\Request;
Expand All @@ -15,8 +16,8 @@ class AddToContainerRequest extends Request
{
public function __construct(
AssetsClient $assetsClient,
readonly string $assetId,
readonly string $containerId
readonly AssetId $assetId,
readonly AssetId $containerId
)
{
parent::__construct($assetsClient);
Expand Down
9 changes: 5 additions & 4 deletions src/Helper/DownloadOriginalFileRequest.php
Expand Up @@ -3,6 +3,7 @@
namespace DerSpiegel\WoodWingAssetsClient\Helper;

use BadFunctionCallException;
use DerSpiegel\WoodWingAssetsClient\AssetId;
use DerSpiegel\WoodWingAssetsClient\AssetsClient;
use DerSpiegel\WoodWingAssetsClient\Exception\AssetsException;
use DerSpiegel\WoodWingAssetsClient\Request;
Expand All @@ -12,9 +13,9 @@
class DownloadOriginalFileRequest extends Request
{
public function __construct(
AssetsClient $assetsClient,
readonly string $targetPath,
readonly ?string $assetId = null,
AssetsClient $assetsClient,
readonly string $targetPath,
readonly ?AssetId $assetId = null,
readonly ?AssetResponse $assetResponse = null
)
{
Expand All @@ -27,7 +28,7 @@ public function validate(): void
if ($this->assetResponse === null) {
if ($this->assetId === null) {
throw new BadFunctionCallException(sprintf("%s: Both assetId and assetResponse are null - one of them must be given", __METHOD__));
} elseif (trim($this->assetId) === '') {
} elseif (trim($this->assetId->id) === '') {
throw new BadFunctionCallException(sprintf("%s: assetId is empty", __METHOD__));
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/Helper/RemoveByIdRequest.php
Expand Up @@ -2,6 +2,7 @@

namespace DerSpiegel\WoodWingAssetsClient\Helper;

use DerSpiegel\WoodWingAssetsClient\AssetId;
use DerSpiegel\WoodWingAssetsClient\AssetsClient;
use DerSpiegel\WoodWingAssetsClient\Request;
use DerSpiegel\WoodWingAssetsClient\Service\ProcessResponse;
Expand All @@ -14,9 +15,9 @@
class RemoveByIdRequest extends Request
{
public function __construct(
AssetsClient $assetsClient,
readonly string $assetId,
readonly bool $async = false
AssetsClient $assetsClient,
readonly AssetId $assetId,
readonly bool $async = false
)
{
parent::__construct($assetsClient);
Expand Down
11 changes: 6 additions & 5 deletions src/Helper/RemoveFromContainerRequest.php
Expand Up @@ -2,6 +2,7 @@

namespace DerSpiegel\WoodWingAssetsClient\Helper;

use DerSpiegel\WoodWingAssetsClient\AssetId;
use DerSpiegel\WoodWingAssetsClient\AssetsClient;
use DerSpiegel\WoodWingAssetsClient\Exception\AssetsException;
use DerSpiegel\WoodWingAssetsClient\RelationType;
Expand All @@ -14,9 +15,9 @@
class RemoveFromContainerRequest extends Request
{
public function __construct(
AssetsClient $assetsClient,
readonly string $assetId,
readonly string $containerId
AssetsClient $assetsClient,
readonly AssetId $assetId,
readonly AssetId $containerId
)
{
parent::__construct($assetsClient);
Expand Down Expand Up @@ -52,8 +53,8 @@ public function __invoke(): ProcessResponse
$this->logger->info('Relation removed',
[
'method' => __METHOD__,
'assetId' => $this->assetId,
'containerId' => $this->containerId,
'assetId' => $this->assetId->id,
'containerId' => $this->containerId->id,
'relationId' => $relationId
]
);
Expand Down
3 changes: 2 additions & 1 deletion src/Helper/SearchAssetIdRequest.php
Expand Up @@ -2,6 +2,7 @@

namespace DerSpiegel\WoodWingAssetsClient\Helper;

use DerSpiegel\WoodWingAssetsClient\AssetId;
use DerSpiegel\WoodWingAssetsClient\AssetsClient;
use DerSpiegel\WoodWingAssetsClient\Exception\AssetsException;
use DerSpiegel\WoodWingAssetsClient\Request;
Expand All @@ -23,7 +24,7 @@ public function __construct(
}


public function __invoke(): string
public function __invoke(): AssetId
{
$response = (new SearchRequest($this->assetsClient,
q: $this->q,
Expand Down
11 changes: 6 additions & 5 deletions src/Helper/SearchAssetRequest.php
Expand Up @@ -2,6 +2,7 @@

namespace DerSpiegel\WoodWingAssetsClient\Helper;

use DerSpiegel\WoodWingAssetsClient\AssetId;
use DerSpiegel\WoodWingAssetsClient\AssetsClient;
use DerSpiegel\WoodWingAssetsClient\Exception\AssetsException;
use DerSpiegel\WoodWingAssetsClient\Request;
Expand All @@ -15,9 +16,9 @@
class SearchAssetRequest extends Request
{
public function __construct(
AssetsClient $assetsClient,
readonly string $assetId,
readonly array $metadataToReturn = []
AssetsClient $assetsClient,
readonly AssetId $assetId,
readonly array $metadataToReturn = []
)
{
parent::__construct($assetsClient);
Expand All @@ -27,12 +28,12 @@ public function __construct(
public function __invoke(): AssetResponse
{
$response = (new SearchRequest($this->assetsClient,
q: 'id:' . $this->assetId,
q: 'id:' . $this->assetId->id,
metadataToReturn: empty($metadataToReturn) ? [SearchRequest::METADATA_TO_RETURN_DEFAULT] : $metadataToReturn
))();

if ($response->totalHits === 0) {
throw new AssetsException(sprintf('%s: Asset with ID <%s> not found', __METHOD__, $this->assetId), 404);
throw new AssetsException(sprintf('%s: Asset with ID <%s> not found', __METHOD__, $this->assetId->id), 404);
}

if ($response->totalHits > 1) {
Expand Down
1 change: 1 addition & 0 deletions src/MapFromJson.php
Expand Up @@ -11,6 +11,7 @@ class MapFromJson
{
public const INT_TO_DATETIME = 'intToDateTime';
public const STRING_TO_ACTION = 'stringToAction';
public const STRING_TO_ID = 'stringToId';


public function __construct(
Expand Down
31 changes: 0 additions & 31 deletions src/PrivateApi/AssetsPrivateApiClient.php

This file was deleted.

14 changes: 14 additions & 0 deletions src/PrivateApi/System/ActiveUsersRequest.php
@@ -0,0 +1,14 @@
<?php

namespace DerSpiegel\WoodWingAssetsClient\PrivateApi\System;

use DerSpiegel\WoodWingAssetsClient\Request;


class ActiveUsersRequest extends Request
{
public function __invoke(): array
{
return $this->assetsClient->privateApiRequest('GET', 'system/active-users');
}
}
2 changes: 2 additions & 0 deletions src/Response.php
Expand Up @@ -34,6 +34,8 @@ protected static function applyJsonMapping(array $json): array
);
} elseif ($mapFromJson->conversion === MapFromJson::STRING_TO_ACTION) {
$value = AssetsAction::tryFrom($value) ?? AssetsAction::Other;
} elseif ($mapFromJson->conversion === MapFromJson::STRING_TO_ID) {
$value = new AssetId($value);
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/Service/AssetResponse.php
Expand Up @@ -2,6 +2,7 @@

namespace DerSpiegel\WoodWingAssetsClient\Service;

use DerSpiegel\WoodWingAssetsClient\AssetId;
use DerSpiegel\WoodWingAssetsClient\MapFromJson;
use DerSpiegel\WoodWingAssetsClient\Response;
use ReflectionClass;
Expand All @@ -13,15 +14,15 @@
class AssetResponse extends Response
{
public function __construct(
#[MapFromJson] readonly string $id = '',
#[MapFromJson] readonly string $permissions = '',
#[MapFromJson] readonly array $metadata = [],
#[MapFromJson] readonly string $highlightedText = '',
#[MapFromJson] readonly string $originalUrl = '',
#[MapFromJson] readonly string $previewUrl = '',
#[MapFromJson] readonly string $thumbnailUrl = '',
#[MapFromJson] readonly array $relation = [],
readonly ?AssetResponseList $thumbnailHits = null
#[MapFromJson(conversion: 'stringToId')] readonly ?AssetId $id = null,
#[MapFromJson] readonly string $permissions = '',
#[MapFromJson] readonly array $metadata = [],
#[MapFromJson] readonly string $highlightedText = '',
#[MapFromJson] readonly string $originalUrl = '',
#[MapFromJson] readonly string $previewUrl = '',
#[MapFromJson] readonly string $thumbnailUrl = '',
#[MapFromJson] readonly array $relation = [],
readonly ?AssetResponseList $thumbnailHits = null
)
{
}
Expand Down

0 comments on commit 5ac101d

Please sign in to comment.