Skip to content

Latest commit

 

History

History
162 lines (142 loc) · 5.21 KB

README.md

File metadata and controls

162 lines (142 loc) · 5.21 KB

DTF DBS API Client

Tests Coverage Status

Requirements

Installation via Composer

Run in command line:

composer require ybelenko/dtf-dbs-client

Setup

Via PHP-DI container

<?php
// config.dev.php
// contains sensitive data
// should be excluded from source base in .gitignore file
return [
    'DtfDbsApi.dealerId' => 'test01',
    'DtfDbsApi.clientId' => 'xxxxxxxxxxxxxxxxx',
    'DtfDbsApi.clientSecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'DtfDbsApi.environment' => 'cert',// cert|qual|prod
];
<?php
// config.php
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use GuzzleHttp\RequestOptions;
use Psr\Http\Client\ClientInterface;
use Ybelenko\DtfDbsClient\ApiClient;
use Ybelenko\DtfDbsClient\ApiClientConfig;

return [
    // Guzzle Client with DTF DBS API
    ClientInterface::class => \DI\autowire(Client::class)
        ->constructor([
            RequestOptions::HTTP_ERRORS => false,// important to handle non 2xx statuses properly
        ]),

    ApiClient::class => \DI\autowire(),
    ApiClientConfig::class => \DI\autowire()
        ->constructorParameter('requestFactory', \DI\create(HttpFactory::class))
        ->constructorParameter('uriFactory', \DI\create(HttpFactory::class))
        ->constructorParameter('streamFactory', \DI\create(HttpFactory::class))
        ->constructorParameter('dealerId', \DI\get('DtfDbsApi.dealerId'))
        ->constructorParameter('clientId', \DI\get('DtfDbsApi.clientId'))
        ->constructorParameter('clientSecret', \DI\get('DtfDbsApi.clientSecret'))
        ->constructorParameter('environment', \DI\get('DtfDbsApi.environment'))
        ->constructorParameter('authScope', 'dtf:dbs:file:write dtf:dbs:file:read'),
];
<?php
// index.php
require_once(__DIR__ . '/vendor/autoload.php');

use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;

$builder = new ContainerBuilder();

// Main configuration
$builder->addDefinitions("config.php");

// Config file for the environment
$builder->addDefinitions("config.$environment.php");

/** @var ContainerInterface */
$container = $builder->build();

Manual

<?php
// index.php
require_once(__DIR__ . '/vendor/autoload.php');

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use GuzzleHttp\RequestOptions;
use Ybelenko\DtfDbsClient\ApiClient;
use Ybelenko\DtfDbsClient\ApiClientConfig;

$client = new ApiClient(
    new ApiClientConfig(
        new Client([RequestOptions::HTTP_ERRORS => false]),// httpClient
        new HttpFactory(),// requestFactory 
        new HttpFactory(),// uriFactory
        new HttpFactory(),// streamFactory
        'xxxxxxxxxxxxxxxxx',// clientId 
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',// clientSecret 
        'test01',// dealerId 
        'cert',// environment cert|qual|prod 
        'dtf:dbs:file:write dtf:dbs:file:read'// scopes
    )
);

// ready to call API services

API services

File List Service

// $client initialization omitted
try {
    /** @var array[] */
    $filesList = $client->callFileListService();
    // Approx shape [{"name": "order.dat", "links": [{"rel": "download", "href": "http:"}, {"rel": "details", "href": "http:"}]}, ...]
    foreach ($filesList as $file) {
        // do something
    }
} catch (\Throwable $e) {
    // echo or log exception for following investigation
}

File Upload Service

// $client initialization omitted
try {
    $factory = new \GuzzleHttp\Psr7\HttpFactory();
    $testFile = $factory->createStreamFromFile(__DIR__ . '/tests/samplecommonfile.txt', 'r');
    /** @var bool */
    $success = $client->callFileUploadService(
        $testFile, 
        null,// filename, optional
        true// overwrite param
    );
    if (!$success) {
        throw new \Exception('Unable to upload file');
    }
} catch (\Throwable $e) {
    // echo or log exception for following investigation
}

File Download Service

// $client initialization omitted
try {
    // can be retrieved from File List Service above
    $filename = 'samplecommonfile.txt';
    /** @var \Psr\Http\Message\StreamInterface */
    $fileStream = $client->callFileDownloadService($filename);
    $uploaded = new \GuzzleHttp\Psr7\UploadedFile($fileStream, $fileStream->getSize(), \UPLOAD_ERR_OK, $filename);
    $uploaded->moveTo(__DIR__ . '\/output\/' . $filename);
    // saved to output folder
} catch (\Throwable $e) {
    // echo or log exception for following investigation
}

Author

Yuriy Belenko