项目作者: bigoen

项目描述 :
Bridges for api projects.
高级语言: PHP
项目地址: git://github.com/bigoen/api-bridge.git
创建时间: 2020-09-05T15:40:19Z
项目社区:https://github.com/bigoen/api-bridge

开源协议:MIT License

下载


Api Bridge

Install:

  1. composer require bigoen/api-bridge

SimpleClient

Create model:

  1. <?php
  2. namespace App\Model;
  3. class Example
  4. {
  5. public ?string $name = null;
  6. public ?string $email = null;
  7. }

If you want error class, create model:

  1. <?php
  2. namespace App\Model;
  3. class Error
  4. {
  5. public ?string $status = null;
  6. public ?string $message = null;
  7. }

Create client:

  1. use App\Model\Example;
  2. use App\Model\Error;
  3. use Symfony\Component\HttpClient\HttpClient;
  4. use Bigoen\ApiBridge\HttpClient\SimpleClient;
  5. $httpClient = HttpClient::create();
  6. $client = new SimpleClient($httpClient);
  7. $client
  8. ->setBaseUrl("http://example.com")
  9. ->setClass(Example::class)
  10. ->setThrowClass(Error:class)
  11. ->setOptions([
  12. // Set http client request options.
  13. ]);
  14. // all objects.
  15. $client->setPath("/api/examples")->getAll();
  16. // get object with id.
  17. $client->setPath("/api/examples/{id}")->setId(1)->get();
  18. // post object.
  19. $model = new Example();
  20. $model->name = 'Test';
  21. $model->email = 'test@example.com';
  22. $postModel = $client->setPath("/api/examples")->post($model);
  23. // put object.
  24. $model = $client->setPath("/api/examples/{id}")->setId(1)->get();
  25. $model->name = 'New Name';
  26. $model = $client->setPath("/api/examples/{id}")->put($model);
  27. // delete object.
  28. $isDelete = $client->setPath("/api/examples/{id}")->setId(1)->delete();

JsonldClient

Create model:

  1. <?php
  2. namespace App\Model;
  3. use Bigoen\ApiBridge\Bridge\ApiPlatform\Model\Traits\JsonldModelTrait;
  4. class Example
  5. {
  6. use JsonldModelTrait;
  7. public ?string $name = null;
  8. public ?string $email = null;
  9. }

Create client for ApiPlatform projects or jsonld apis:

  1. use App\Model\Example;
  2. use Symfony\Component\HttpClient\HttpClient;
  3. use Bigoen\ApiBridge\Bridge\ApiPlatform\HttpClient\JsonldClient;
  4. $httpClient = HttpClient::create();
  5. $client = new JsonldClient($httpClient);
  6. $client
  7. ->setBaseUrl("http://example.com")
  8. ->setClass(Example::class)
  9. ->setOptions([
  10. // Set http client request options.
  11. ]);
  12. // all objects.
  13. $pageOne = $client->setPath("/api/examples")->getAll();
  14. // get next page objects.
  15. $pageTwo = $client->setPath($pageOne->nextPagePath)->getAll();
  16. // get object with id.
  17. $client->setPath("/api/examples/{id}")->setId(1)->get();
  18. // post object.
  19. $model = new Example();
  20. $model->name = 'Test';
  21. $model->email = 'test@example.com';
  22. $postModel = $client->setPath("/api/examples")->post($model);
  23. // put object.
  24. $model = $client->setPath("/api/examples/{id}")->setId(1)->get();
  25. $model->name = 'New Name';
  26. $model = $client->setPath("/api/examples/{id}")->put($model);
  27. // delete object.
  28. $isDelete = $client->setPath("/api/examples/{id}")->setId(1)->delete();

Convert api values in tree. Details: https://github.com/bigoen/api-bridge-converter