nmirceac/api-client-tools

Laravel 的 API 客户端工具

0.1.3.2 2024-09-02 09:19 UTC

README

API 客户端工具

内容

  1. 简介
  2. 示例

1. 简介

如何安装?

  • composer require nmirceac/api-client-tools
  • php artisan vendor:publish
  • 检查 config/api-client.php(以防万一)
  • 将您的 API 详细信息添加到 .env
  • php artisan apitools:publish - 生成模型
  • 查看以下示例
  • 享受吧!

示例

.env 示例配置

API_CLIENT_BASE_NAMESPACE="Api"
API_CLIENT_BASE_URL="https://admin-collaboration.weanswer.it/"
API_CLIENT_SECRET="FTPU6jA3YIqELg8XKI*****************"
API_CLIENT_COLOR_TOOLS_AUTODETECT=true
API_CLIENT_COLOR_TOOLS_PUBLIC_PATTERN="images/%hash%"
API_CLIENT_SEND_AUTH=true
API_CLIENT_SEND_LOCALE=true
API_CLIENT_IGNORE_SSL_HOST=false
API_CLIENT_IGNORE_SSL_ERRORS=false

2. 示例

ColorTools 图像支持

对象将扫描 \ColorTools\ImageStore 对象,并将其转换为 \ApiClientTools\App\ApiImageStore 对象

return \App\Api\Project::get(3)['thumbnail'];
// ApiClientTools\App\ApiImageStore {#284 ▼
//  +modifierString: null
//  +"id": 22
//  +"hash": "db16b0f3fe1533135914df5fa455e3c5"
//  +"name": "Collaboration Maersk 001"
//  +"type": "jpeg"
//  +"size": 48227
//  +"width": 471
//  +"height": 471
//  +"metadata": array:12 [▶]
//  +"colors": array:5 [▶]
//  +"created_at": "2019-12-12 14:37:06"
//  +"updated_at": "2019-12-12 14:37:08"
//  +"canDelete": true
//  +"orientation": "L"
//  +"basename": "Collaboration Maersk 001.jpeg"
//  +"details": array:1 [▶]
//  +"pivot": array:6 [▶]
//  +"url": "https://admin-collaboration.weanswer.it/images/db16b0f3fe1533135914df5fa455e3c5.jpeg"
//}

所有对象都有一个 url 属性,指向其完整大小的 URL

return \App\Api\Project::get(3)['thumbnail']->url;
// https://admin-collaboration.weanswer.it/images/db16b0f3fe1533135914df5fa455e3c5.jpeg

对象将具有类似于 \ColorTools\ImageStore 对象的修改和发布方法

您可以应用修饰符然后发布

$thumbnail=\App\Api\Project::get(3)['thumbnail'];
$thumbnail->modifyImage(function(\ColorTools\Image $img) {
    $img->fit(100, 100);
});
return $thumbnail->publish();
// https://admin-collaboration.weanswer.it/images/db16b0f3fe1533135914df5fa455e3c5-ft=100+100.jpeg

修饰符仅是一个返回对象的修改器

$thumbnail=\App\Api\Project::get(3)['thumbnail'];
return $thumbnail->modifyImage(function(\ColorTools\Image $img) {
    $img->fit(100, 100);
})->publish();
// https://admin-collaboration.weanswer.it/images/db16b0f3fe1533135914df5fa455e3c5-ft=100+100.jpeg

可以覆盖发布格式

$thumbnail=\App\Api\Project::get(3)['thumbnail'];
return $thumbnail->modifyImage(function(\ColorTools\Image $img) {
    $img->fit(100, 100);
})->publish('png');
// https://admin-collaboration.weanswer.it/images/db16b0f3fe1533135914df5fa455e3c5-ft=100+100.png

您也可以一次性修改和发布

$thumbnail=\App\Api\Project::get(3)['thumbnail'];
$thumbnail->modifyImagePublish(function(\ColorTools\Image $img) {
    $img->fit(100, 100);
});
// https://admin-collaboration.weanswer.it/images/db16b0f3fe1533135914df5fa455e3c5-ft=100+100.png

同时指定发布格式

$thumbnail=\App\Api\Project::get(3)['thumbnail'];
$thumbnail->modifyImagePublish(function(\ColorTools\Image $img) {
    $img->fit(100, 100);
}, 'png');
// https://admin-collaboration.weanswer.it/images/db16b0f3fe1533135914df5fa455e3c5-ft=100+100.png

缩略图支持

非图像缩略图也支持类似的语法。只需确保删除传递给 close 的对象的类型提示,因为传递的对象可能不是图像。

$thumbnail=\App\Api\Project::get(2)['thumbnail'];
$thumbnail->modifyImagePublish(function($img) {
    $img->fit(100, 100);
});
// https://admin-collaboration.weanswer.it/thumbnail/project/2-s=100x100.jpeg

或者

$thumbnail=\App\Api\Project::get(2)['thumbnail'];
$thumbnail->modifyImagePublish(function($img) {
    $img->fit(400, 270);
}, 'png');
// https://admin-collaboration.weanswer.it/thumbnail/project/2-s=400x270.png

请注意,目前只支持 fit 方法 - 所有其他方法都将优雅地忽略

$thumbnail=\App\Api\Project::get(2)['thumbnail'];
$thumbnail->modifyImagePublish(function($img) {
    $img->fit(400, 270);
    $img->applyFilter(\ColorTools\Image::FILTER_GRAYSCALE);
});
// https://admin-collaboration.weanswer.it/thumbnail/project/2-s=400x270.jpeg

$thumbnail=\App\Api\Project::get(4)['thumbnail'];
$thumbnail->modifyImagePublish(function($img) {
    $img->fit(400, 270);
    $img->applyFilter(\ColorTools\Image::FILTER_GRAYSCALE);
});
// https://admin-collaboration.weanswer.it/images/45771d1cdfe109aa6e5b3fd48c925ebd-fi=1+-ft=400+270.jpeg

文件有效载荷辅助方法

现在您可以使用两个辅助方法轻松地附加与 FileTools 兼容的文件有效载荷

// you can create the payload from a file path
$filePayloadFromPath=\App\Api\Project::createFileFromPath('/path/to/file.extension');
// or from a request
$filePayloadFromRequest=\App\Api\Extension::createFileFromRequest(request(), 'file');


// you can also optionally specify the individual file role or order 
$anotherFilePayload==\App\Api\Project::createFileFromPath('/path/to/another.file', 'guide', 1);

// you can then just upload the files to the proper endpoing
\App\Api\Endpoint::attachFile($id, ['files'=>[$filePayloadFromPath, $filePayloadFromRequest, $anotherFilePayload]]);

// you can also create the file from string and populate your own metadata
\App\Api\Endpoint::createFilePayload($metadata, $fileContents);

$metadata should contain at least name, mime, extension and size