vikingmaster / tampere-journeys-api-sdk
PHP SDK for Tampere Journeys API
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 2
Language:HTML
Requires
- php: ^7.2
- ext-json: *
- guzzlehttp/guzzle: ^6.3
Requires (Dev)
- phpunit/phpunit: ^8.4
This package is auto-updated.
Last update: 2024-12-21 23:59:13 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 totimeout
(optional) Http request timeout in secondsuserAgent
(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(); }