vikingmaster/tampere-journeys-api-sdk

PHP SDK for Tampere Journeys API

0.1.2 2019-11-05 17:05 UTC

README

This is an unofficial SDK release for Tampere Journeys API.

Installation

Install using composer: composer require vikingmaster/tampere-journeys-api-sdk

Usage

Creating an API Client instance. Available config parameters:

  • baseUri Base URI to make requests to
  • timeout (optional) Http request timeout in seconds
  • userAgent (optional) User-Agent header
$api = new \Vikingmaster\TampereJourneysApiSdk\TampereJourneysApiClient([
    'baseUri' => 'http://data.itsfactory.fi/journeys/api'
]);

Fetching lines:

//Fetch lines
$request = $api->makeGetLinesRequest()
    ->setIndent(true)
    ->setDescription('Description')
;
$response = $request->send();
$lines = $response->getLines();

Fetching journey patterns

$request = $api->makeGetJourneyPatternsRequest()
    ->setFirstStopPointId(1)
    ->setLastStopPointId(2)
    ->setLineId(17)
    ->setName("Nokian asema C - Keho")
;
$response = $request->send();
$patterns = $response->getJourneyPatterns();

Exception handling

When there is a request error, TampereJourneyApiException will be thrown:

use \Vikingmaster\TampereJourneysApiSdk\Exceptions\TampereJourneyApiException;

try {
    $response = $request->send();
} catch (TampereJourneyApiException $e) {
    //These methods are available for problem tracing
    $apiError  = $e->getApiError();
    $request   = $e->getRequest();
    $response  = $e->getResponse();
    $apiClient = $e->getApiClient();
} catch (\Exception $e) {
    //Any other errors such as network or configuration error
}

Pagination

Some of the responses can be long, so multiple requests might be needed to fetch everything

/** @var array|\Vikingmaster\TampereJourneysApiSdk\Dto\Line[] $entries */
$entries = [];

$fetch      = true;
$startIndex = 0;
while ($fetch) {
    try {
        $response = $request->setStartIndex($startIndex)->send();
        $entries  = array_merge($entries, $response->getLines());
    } catch (\Exception $e) {
         //Handle exception / resend the request
        break;
    }

    $fetch      = $response->getPaging()->hasMoreData();
    $startIndex = $response->getPaging()->getPageSize();
}