This is an unofficial SDK release for Tampere Journeys API.
Install using composer: composer require vikingmaster/tampere-journeys-api-sdk
Creating an API Client instance. Available config parameters:
baseUri
Base URI to make requests totimeout
(optional) Http request timeout in secondsuserAgent
(optional) User-Agent header
$api = new \Vikingmaster\TampereJourneysApiSdk\TampereJourneysApiClient([
'baseUri' => 'http://data.itsfactory.fi/journeys/api'
]);
//Fetch lines
$request = $api->makeGetLinesRequest()
->setIndent(true)
->setDescription('Description')
;
$response = $request->send();
$lines = $response->getLines();
$request = $api->makeGetJourneyPatternsRequest()
->setFirstStopPointId(1)
->setLastStopPointId(2)
->setLineId(17)
->setName("Nokian asema C - Keho")
;
$response = $request->send();
$patterns = $response->getJourneyPatterns();
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
}
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();
}