Batch responses
About
Different operations can have a different result. The batch operation result is a collection of the original task’s result. Let’s have some example
Examples with PHP-SDK
Upload result
Upload process has Image
object as a result if the upload succeeded. In case of batch upload, it’s same just the Image
results are in an associative array where the key is the request ID and the value is the result.
use shardimage\shardimagephp\auth\Client;
use shardimage\shardimagephp\models\image\Image;
$client = new Client([
'apiKey' => <apiKey>,
'apiSecret' => <apiSecret>,
'imageSecret' => <imageSecret>,
'cloudId' => <cloudId>,
]);
$files = [
'file1.jpg',
'file2.jpg',
'file3.png',
'file4.webp',
'file5.gif',
];
$client->defer(true); // batch request turned on
// Handling the response
foreach ($files as $file) {
$client->getUploadService()->upload([
'file' => $file,
], [
'tags' => [
'batch',
'examples'
],
]);
}
$result = $client->defer(false); // batch request turned off and sending
$publicIds = [];
foreach ($result as $_result) {
if ($_result instanceof Image) {
// handle succeeded uploads, for example:
$publicIds[] = $_result->publicId;
} else {
// handle upload errors
}
}
// Yii2 config file:
[
'components' => [
// ...
'shardimage' => [
'class' => \shardimage\yii2shardimage\Component::class,
'apiKey' => <apiKey>,
'apiSecret' => <apiSecret>,
'imageSecret' => <imageSecret>,
'cloudId' => <cloudId>,
],
// ...
],
]
// Component usage:
use Yii;
use shardimage\shardimagephp\models\image\Image;
$files = [
'file1.jpg',
'file2.jpg',
'file3.png',
'file4.webp',
'file5.gif',
];
$shardimage = Yii::$app->shardimage;
$shardimage->defer(true); // batch request turned on
// Handling the response
$uploadService = $shardimage->upload();
foreach ($files as $file) {
$uploadService->upload([
'file' => $file,
], [
'tags' => [
'batch',
'examples'
],
]);
}
$shardimage->defer(false); // batch request turned off and sending
$publicIds = [];
foreach ($result as $_result) {
if ($_result instanceof Image) {
// handle succeeded uploads, for example:
$publicIds[] = $_result->publicId;
} else {
// handle upload errors
}
}
Image delete result
Deleting an image will result boolean
true
if the deletion succeeds. In case of any error, the response will be a Response
object containing the detailed error.
use shardimage\shardimagephp\auth\Client;
use shardimage\shardimagephpapi\api\Response;
$client = new Client([
'apiKey' => <apiKey>,
'apiSecret' => <apiSecret>,
'imageSecret' => <imageSecret>,
'cloudId' => <cloudId>,
]);
$publicIds = [
// collection of pictures to be deleted
];
$client->defer(true); // batch request turned on
foreach ($publicIds as $publicId) {
$client->getImageService()->delete([
'publicId' => $publicId,
], []);
}
$result = $client->defer(false); // batch request turned off and sending
// Handling the response
var_dump($result); // array(2) {["1365.1557466263.2256.1827546364"]=>bool(true)["1365.1557466263.2254.786066126"]=>bool(true)}
foreach ($result as $_result) {
if ($_result instanceof Response) {
// handle errors
} elseif ($_result === true) {
// handle succeeded deletion
} else {
// unexpected result
}
}
// Yii2 config file:
[
'components' => [
// ...
'shardimage' => [
'class' => \shardimage\yii2shardimage\Component::class,
'apiKey' => <apiKey>,
'apiSecret' => <apiSecret>,
'imageSecret' => <imageSecret>,
'cloudId' => <cloudId>,
],
// ...
],
]
// Component usage:
use Yii;
use shardimage\shardimagephpapi\api\Response;
$shardimage = Yii::$app->shardimage;
$publicIds = [
// collection of pictures to be deleted
];
$shardimage->defer(true); // batch request turned on
$imageService = $shardimage->image();
foreach ($publicIds as $publicId) {
$imageService->delete([
'publicId' => $publicId,
], []);
}
$result = $shardimage->defer(false); // batch request turned off and sending
// Handling the response
var_dump($result); // array(2) {["1365.1557466263.2256.1827546364"]=>bool(true)["1365.1557466263.2254.786066126"]=>bool(true)}
foreach ($result as $_result) {
if ($_result instanceof Response) {
// handle errors
} elseif ($_result === true) {
// handle succeeded deletion
} else {
// unexpected result
}
}
Mixed requests
Mixing different kind of requests in one batch requets.
use shardimage\shardimagephpapi\api\Response;
use shardimage\shardimagephp\auth\Client;
use shardimage\shardimagephp\models\image\Image;
$client = new Client([
'apiKey' => <apiKey>,
'apiSecret' => <apiSecret>,
'imageSecret' => <imageSecret>,
'cloudId' => <cloudId>,
]);
$files = [
'file1.jpg',
'file2.jpg',
'file3.png',
'file4.webp',
'file5.gif',
];
$viewIds = [
'<viewId1>',
'<viewId2>',
'<viewId3>',
];
$deleteIds = [
'<deleteId1>',
'<deleteId2>',
];
$client->defer(true); // batch request turned on
foreach ($files as $file) {
$client->getUploadService()->upload([
'file' => $file,
], []);
}
foreach ($viewIds as $viewId) {
$client->getImageService()->view([
'publicId' => $viewId,
],[]);
}
foreach ($deleteIds as $deleteId) {
$client->getImageService()->delete([
'publicId' => $deleteId,
],[]);
}
$result = self::getClient()->defer(false);
foreach ($result as $_result) {
if ($_result instanceof Response) {
echo sprintf("%s\n", $_result->getError()->getException()->getMessage());
} elseif (($_result instanceof Image) && !in_array($_result->publicId, $viewIds)) {
echo sprintf("Upload successful - %s\n", $_result->publicId);
} elseif ($_result === true) {
echo "Deletion successful!\n";
} elseif (($_result instanceof Image) && in_array($_result->publicId, $viewIds)) {
echo "View successful!\n";
}
}
// Yii2 config file:
[
'components' => [
// ...
'shardimage' => [
'class' => \shardimage\yii2shardimage\Component::class,
'apiKey' => <apiKey>,
'apiSecret' => <apiSecret>,
'imageSecret' => <imageSecret>,
'cloudId' => <cloudId>,
],
// ...
],
]
// Component usage:
use Yii;
use shardimage\shardimagephpapi\api\Response;
use shardimage\shardimagephp\models\image\Image;
$files = [
'file1.jpg',
'file2.jpg',
'file3.png',
'file4.webp',
'file5.gif',
];
$viewIds = [
'<viewId1>',
'<viewId2>',
'<viewId3>',
];
$deleteIds = [
'<deleteId1>',
'<deleteId2>',
];
$shardimage = Yii::$app->shardimage;
$imageService = $shardimage->getImageService();
$uploadService = $shardimage->getUploadService();
$shardimage->defer(true); // batch request turned on
foreach ($files as $file) {
$uploadService->upload([
'file' => $file,
], []);
}
foreach ($viewIds as $viewId) {
$imageService->view([
'publicId' => $viewId,
],[]);
}
foreach ($deleteIds as $deleteId) {
$imageService->delete([
'publicId' => $deleteId,
],[]);
}
$result = $shardimage->defer(false);
foreach ($result as $_result) {
if ($_result instanceof Response) {
echo sprintf("%s\n", $_result->getError()->getException()->getMessage());
} elseif (($_result instanceof Image) && !in_array($_result->publicId, $viewIds)) {
echo sprintf("Upload successful - %s\n", $_result->publicId);
} elseif ($_result === true) {
echo "Deletion successful!\n";
} elseif (($_result instanceof Image) && in_array($_result->publicId, $viewIds)) {
echo "View successful!\n";
}
}
Running the script first time:
Upload successful - <uploadPublicId1>
Deletion successful!
Upload successful - <uploadPublicId2>
Upload successful - <uploadPublicId3>
Upload successful - <uploadPublicId4>
View successful!
View successful!
View successful!
Upload successful - <uploadPublicId5>
Deletion successful!
Running the script second time:
View successful!
View successful!
View successful!
API error / publicId: Public ID doesn't exist!
API error / publicId: Public ID doesn't exist!
Upload successful - <uploadPublicId6>
Upload successful - <uploadPublicId7>
Upload successful - <uploadPublicId8>
Upload successful - <uploadPublicId9>
Upload successful - <uploadPublicId10>