Skip to content

openeuropa/cdt-client

Repository files navigation

OpenEuropa CDT Client

Build Status Packagist

Description

CDT Client is a library offering a PHP API to consume Translation Centre For the Bodies of the EU services.

Install

Use Composer to install the package:

$ composer require openeuropa/cdt-client

Usage

All calls should be done by instantiating the client class:

require_once 'vendor/autoload.php';

$client = new \OpenEuropa\CdtClient\ApiClient(
    new \GuzzleHttp\Client(),
    new \Http\Factory\Guzzle\RequestFactory(),
    new \Http\Factory\Guzzle\StreamFactory(),
    [
        // For a full list of options see "Configuration".
        'mainApiEndpoint' => 'https://example.com/v2/CheckConnection',
        'tokenApiEndpoint' => 'https://example.com/token',
        'referenceDataApiEndpoint' => 'https://example.com/v2/requests/businessReferenceData',' => 'https://example.com/v2/requests/businessReferenceData',
        'validateApiEndpoint' => 'https://example.com/v2/requests/validate',
        'requestsApiEndpoint' => 'https://example.com/v2/requests',
        'identifierApiEndpoint' => 'https://example.com/v2/requests/requestIdentifierByCorrelationId/:correlationId',
        'statusApiEndpoint' => 'https://example.com/v2/requests/:requestyear/:requestnumber',
        'username' => 'your-user-name',
        'password' => 'your-password',
        'client' => 'client-name',
    ]
);

In the above example, we're passing the Guzzle HTTP client, request, stream and URI factories. But these can be replaced by any similar factories that are implementing the PSR interfaces. The last parameter is the configuration.

Configuration

Possible configurations:

  • username (string): Used for authentication.
  • password (string): Used for authentication.
  • client (string): Used for authentication.
  • tokenApiEndpoint (string, valid URI): The Token API endpoint.
  • mainApiEndpoint (string, valid URI): The Main API endpoint.
  • referenceDataApiEndpoint (string, valid URI): The Reference Data API endpoint.
  • validateApiEndpoint (string, valid URI): The Validate API endpoint.
  • requestsApiEndpoint (string, valid URI): The Requests API endpoint.
  • identifierApiEndpoint (string, valid URI): The Identifier API endpoint.
  • statusApiEndpoint (string, valid URI): The Status API endpoint.

Check connection

$response = $client->checkConnection();

Will return true or false depending on the availability of the CDT service.

Get reference data

$response = $client->getReferenceData();

Will return an array of business reference data, serialized into OpenEuropa\CdtClient\Model\Response\ReferenceData.

Translation requests

To validate and send a translation request, run the following code:

use OpenEuropa\CdtClient\Model\Request\Translation;
use OpenEuropa\CdtClient\Exception\ValidationErrorsException;

$translationRequest = new Translation()
try {
    if ($client->validateTranslationRequest($translationRequest)) {
        $correlationId = $client->sendTranslationRequest($translationRequest);
    }
} catch (ValidationErrorsException $e) {
    $errors = $e->getValidationErrors();
    // Handle the errors.
}

On success, the sendTranslationRequest() method will return the temporary Correlation ID.

Get permanent request identifier

$permanentId = $client->getPermanentIdentifier($correlationId);

Will return a permanent string identifier for the translation request, based on correlation ID. Throws the ValidationErrorsException if the correlation ID is not found.

Get the status of a translation request

$translationStatus = $client->getRequestStatus($permanentId);

Will return information on the status of translation request, based on permanent ID. The information includes the list of files to download. Throws the ValidationErrorsException if the permanent ID is invalid.

Download the file (source or translated)

$file = $client->downloadFile($fileUrl);

Downloads the specified file, if available, based on the filename listed in getRequestStatus(). Throws a ValidationErrorsException if the filename is invalid.

Contributing

Please read the full documentation for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning.

Releases

No releases published

Packages

No packages published

Languages