krzysztof-moskalik/api-client
最新稳定版本:2.0.0
Composer 安装命令:
composer require krzysztof-moskalik/api-client
包简介
Client for consuming REST APIs
README 文档
README
This library makes an abstraction layer for REST APIs.
It allows mapping API resources to your application models.
Note
This library is under development!
Many things can be changed, and many new features can be added.
Symfony Note
This library is not tied to a Symfony framework, but there is a Symfony bundle available: KrzysztofMoskalik/api-client-bundle.
Features:
- Easy to use
- Flexible
- Easy to extend
- Works the best with standard JSON REST APIs
- Supports multiple APIs
- Supports multiple authentication methods
- Supports multiple endpoints per same resource (with the same HTTP method)
Simple configuration
Api Client does not come with a builtin serializer, so first you need to define your own one.
For example:
use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; $serializer = new Serializer( [ new ObjectNormalizer(), new ArrayDenormalizer(), ], [ new JsonEncoder(), ] );
Same thing with HTTP Client:
$httpClient = new GuzzleHttp\Client();
Finally, you can create an Api Client instance with a simple configuration:
use App\Model\User; use KrzysztofMazur\Api\Api; use KrzysztofMazur\Api\Client; $client = new Client(); $client->globals ->setSerializer($serializer) ->setHttpClient($httpClient) $client->addApi( new Api('my_api_name') ->setBaseUrl('https://my-api.com/api/') ->generic(User::class) );
This will create repository for all CRUD operations on User resource that will be represented by App\Model\User class.
Usage
With this simple configuration you can now use Api Client to interact with your API:
$repository = $client->getRepository(User::class); $user = $repository->get(2340); // fetch user with id 2340 $users = $repository->list(); // fetch all users $repository->delete(3671); // delete user with id 3671 $user = new User( 'John Doe', 'john.doe@example.com' ); $repository->create($user); // create new user $user->setEmail('john.doe@lorem.com'); $repository->update($user); // update user $user->setEmail('john.doe@ipsum.com'); $repository->patch($user); // partial update user
Full config reference
Work in progress...
Todos
- handling filtering and sorting on list operations
- handling nesting data paths
- implement bearer auth
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-20