sidigi/laravel-remote-models

使用 Laravel Eloquent 模型方式调用远程请求

1.0.6 2021-04-21 10:14 UTC

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

这里应该放置你的描述。尽量限制在一两段之内。可以考虑添加一个小的示例。

安装

您可以通过 composer 安装此包

composer require sidigi/laravel-remote-models

用法

配置

    'defaults' => [
        'response_key'        => 'data',
        'pagination_strategy' => 'page_based',
    ],

    'providers' => [
        'aws-lambda' => [
            'class' => Sidigi\LaravelRemoteModels\Providers\AwsLambdaProvider::class,
        ],
        'http' => [
            'class' => Sidigi\LaravelRemoteModels\Providers\HttpProvider::class,
        ],
    ],

    'pagination_strategies' => [
        'page_based' => [
            'class'               => Sidigi\LaravelRemoteModels\Pagination\PaginationBaseStrategy::class,
            'response_number_key' => 'meta.pages_count',
            'defaults'            => [
                'number' => 1,
                'size'   => 100,
            ],
        ],
    ],

    'clients' => [
        'comment-client' => [
            'client' =>  App\RemoteClients\CommentClient::class,
            'base_uri' => 'https://jsonplaceholder.typicode.com',
   |        'provider' => 'http',
            'pagination_strategy' => 'page_based',
            'paths' => [
                'index_comments' => 'comments',
                'index_comments_filter_by_post' => '/comments?postId={id}',
                'todo_detail' => 'todos/{id}',
            ],
        ],
        'aws-comment-client' => [
            'client' =>  App\RemoteClients\AwsCommentClient::class,
            'base_uri' => 'https://jsonplaceholder.typicode.com',
   |        'provider' => 'aws-lambda',
            'function_name' => 'user-service-api',
            'pagination_strategy' => 'page_based',
            'paths' => [
                'index_comments' => 'comments',
                'index_comments_filter_by_post' => '/comments?postId={id}',
                'todo_detail' => 'todos/{id}',
            ],
        ],
    ],

    'models' => [
        App\RemoteModels\Comment::class => 'comment-client',
        //or
        App\RemoteModels\Comment::class => App\RemoteClients\CommentClient::class,
        //or
        App\RemoteModels\Comment::class => [
            'aws' => 'aws-comment-client',
            'http' => 'comment-client',
         ]
    ],

客户端

use Sidigi\LaravelRemoteModels\Client;

class CommentClient extends Client
{
}

$comments = Comment::getRemoteClient()->get();
$comments = Comment::getRemoteClient()->get('/comments');
$comments = Comment::getRemoteClient()->get('/comments/{id}', ['id' => 1]);
$comments = Comment::getRemoteClient()->get('/comments/{id}', ['id' => 1, 'active' => true]);
$comments = Comment::getRemoteClient()->withPath('/comments/{id}', ['id' => 1])->get();
$comments = Comment::getRemoteClient()->withQuery(['active' => true])->get();
$comments = Comment::getRemoteClient()->get(['active' => true]);
use Sidigi\LaravelRemoteModels\Client;

class CommentClient extends Client
{
    public function getPaths() : array
    {
        return [
            'index_comments' => 'comments',
            'detail_comment' => 'comment/{id}',
        ]
    }
}

$comments = CommentClient::withPath('index_comments')->get();
$comments = CommentClient::indexComments()->get();
$comments = CommentClient::withPath('detail_comment', ['id' => 1])->get();
$comments = CommentClient::detailComment(['id' => 1])->get();
$comments = CommentClient::detailComment(['id' => 1])->withQuery(['active' => true])->get();
use Sidigi\LaravelRemoteModels\Client

class CommentClient extends Client
{
}

$comments = CommentClient::withPath('/comments/{id}', ['id' => 1])
                ->withQuery(['active' => true])
                ->filter(['id' => [1, 2, 3])
                ->include('posts.user')
                ->orderBy('created_at')
                ->paginate(['size' => 1, 'number' => 2])
                ->get();

模型

use Sidigi\LaravelRemoteModels\Client;

class CommentClient extends Client
{
}

class Comment extends Model
{
    use HasRemotes;

    protected $guarded = [];

    public function getRemoteClient(): string
    {
        return resolve(CommentClient::class);
    }
}

$comment = Comment::getRemoteClient()
    ->indexComments()
    ->get() //response with models
    ->mapModel(Comment::class, fn ($item) => ['id' => $item['id']])
    ->first();

//App\RemoteModels\Comment

客户端类扩展了 Illuminate\Http\Client\PendingReuqest。您可以使用所有 HTTP 客户端方法

$comment = Comment::getRemoteClient()
    ->indexComments()
    ->withHeaders(['X-Foo' => 'X-Baz']) //withToken, withAuth, etc.
    ->get() //response with models
    ->mapModel(Comment::class)
    ->first();
//App\RemoteModels\Comment
$builder = Comment::getRemoteClient()->indexComments();

foreach ($builder->perPage() as $response) {
    $comments = $response->mapModel(Note::class);
}
$builder = Post::getRemoteClient()->index();

foreach ($builder->perPage() as $response) {
    $comments = $response->mapModel(
        Comment::class,
        fn ($item) => ['id' => $item['id']],
        'data.*.comments'
    );
}
$builder = Post::getRemoteClient()->index();

foreach ($builder->perPage() as $response) {
    $commentIds = $response->get('data.*.comments.*.id');
}

有关 Laravel HTTP 客户端的详细信息,请参阅 这里

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近的变化。

贡献

请参阅 CONTRIBUTING 了解详情。

安全

如果您发现任何与安全相关的问题,请发送电子邮件至 sidigicoder@gmail.com 或使用问题跟踪器。

致谢

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。