randock/vignette-api-client

此包的最新版本(0.0.3)没有可用的许可信息。

Vignette PHP API 客户端

0.0.3 2021-06-11 11:49 UTC

This package is auto-updated.

Last update: 2024-09-30 01:16:30 UTC


README

API 的工作原理是获取 JWT 并将其发送到 api。为了加快速度,我们需要通过实现持久化策略来存储 JWT 响应。

最简单的方法是使用 PSR-16 兼容的缓存适配器。大多数框架都有这些。

参见 https://github.com/eljam/guzzle-jwt-middleware#persistence

使用 API 所需的最小代码量

<?php
use Softonic\GraphQL\Client;
use Softonic\GraphQL\ResponseBuilder;
use Randock\Vignette\Api\Client\ApiClient;
use Randock\Vignette\Api\Util\ClientBuilder;
use Randock\Vignette\Api\Client\Object\Enum\OrderStatus;
use Randock\Vignette\Api\Client\Object\Input\ListOrdersInput;

// This example uses "null" as persistence strategy. DO NOT DO THIS IN PROD.
$client = new Client(
    ClientBuilder::newClient(
        'ENDPOINT',
        'JWT_ENDPOINT',
        [
            'username' => 'XXX',
            'password' => 'XXX'
        ],
        null
    ),
    new ResponseBuilder()
);

// The API is strongly typed, so all responses are easy to use.
// You need to select all fields that you are going to use in the response.
// The API uses GRAPHQL underneath, so not selecting a field means it won't be available.
// See the docs for all possible fields.
$apiClient = new ApiClient($client);
$ordersResponse = $apiClient->orders(
    [
        'paging' => ['total'],
        'orders' => ['id', 'status']
    ],
    new ListOrdersInput(1, 10, null, null)
);

// The total number of orders can be found in the paging section.
echo sprintf(
    "Found %d orders\n\n",
    $ordersResponse->getPaging()->getTotal()
);

// Loop through the orders and print some data.
foreach ($ordersResponse->getOrders() as $order) {
    echo sprintf(
        "Status of order %s is %s. Order is completed: %d\n",
        $order->getId(),
        $order->getStatus(),
        $order->getStatus() === OrderStatus::COMPLETED
    );
}

// If you try to access a field that you didn't request, an exception is thrown.
// FieldNotSelectedException: The field orderId has not been selected in the query. Add it to the query if you need it.
echo $ordersResponse->getOrders()[0]->getOrderId();