autoprotectgroup/rest-api-sdk

RESTful API 的 SDK

v1.2.1 2021-09-01 15:58 UTC

This package is auto-updated.

Last update: 2024-08-29 05:55:27 UTC


README

示例代码

<?php
// Load the Autoloader (Composer)
include __DIR__.'/vendor/autoload.php';

// Create your entities to path mapping (all at the top-level)
$pathMap = [
    'clients' => [
        'path' => 'clients',
    ],
    'transactionAggregates' => [
        'path' => 'transaction-aggregates',
    ],
    'reconciliations' => [
        'path' => 'reconciliations',
    ],
    'transactions' => [
        'path' => 'transactions',
    ],
];

// Create a Guzzle CLient
$guzzle = new GuzzleHttp\Client([
    // Set the base URI to the Restful endpoint (include path if needs be)
    'base_uri' => 'https://13.37.13.37:8000/web/index.php/',
    
    // Add any other Guzzle Options
    'timeout'  => 2.0,
    'headers'  => [
        // Add any other required options, for example api-key
        // is required for Transactional Billing API
        'api-key' => 'wizard',
        // Also this header must be set for the aforementioned API
        'content-type' => 'application/json'
    ]
]);

// Create a new HttpClient for the RestfulClient SDK, Guzzle is provided but
// others can be created by implementing the HttpClientInterface interface.
// Optionally add data and error message array index keys (if your data
// isn't at the top level).
$httpClient = new DealTrak\Packages\RestfulClient\HttpClient\Guzzle(
    $guzzle
);

// Create the Restful API SDK injecting the HttpClient and the pathMap
$client = new DealTrak\Packages\RestfulClient\Client($httpClient, $pathMap);

// Make some Requests
// GET /clients/2/transactions
$result = $client->resource('clients',2)->resource('transactions')->get();
var_dump($result);

// GET /clients/2/transaction-aggregates/6
$result = $client->resource('clients', 2)->resource('transactionAggregates', 6)->get();
var_dump($result);

// POST /clients
$result = $client->resource('clients')->post(['name' => 'New Client', 'code' => 'client123']);
var_dump($result);

// DELETE /clients/8
$result = $client->resource('clients', 8)->delete();
var_dump($result);

// This will fail as roles doesn't exist in the pathMap
$result = $client->resource('clients', 2)->resource('roles')->get();

优化

在内部,每次对 resource 的调用都会创建一个新的对象。换句话说,对客户端的每个不是 getpatchpostdelete 的调用都会创建一个新的客户端。所以如果我们有 $client->get(),那么我们就有一个 Client SDK 对象,如果我们有 $client->resource('clients', 123)->delete(),那么我们就有两个,最后如果我们有 $client->resource('clients', 123)->resource('transactions', 123)->resource('refunds')->get(),那么我们就有了四个。这并不是什么大问题,但如果我们将它放在循环中,它可能会成为问题,请参见下面的示例,了解好和不好的方式。在将来,我们可以添加缓存,以便如果它们是彼此的重复,则返回先前的对象,但这会增加复杂性,所以目前还没有包含,如果将来会...

// Do not do this
foreach($newTransactionsToMake as $newTransactionToMake) {
    $client->resource('clients')->resource('transactions')->post($newClientToMake);
}

// Do this
$transactionsClient = $client->resource('clients')->resource('transactions');
foreach($newTransactionsToMake as $newTransactionToMake) {
    $transactionsClient->post($newClientToMake);
}