sportrizer/sportysky-client-php

2.1.9 2020-05-07 15:01 UTC

README

How to integrate SportySKY within your PHP project

Build Status codecov

Requirements

Getting started

Install the SportySKY PHP client via the composer package manager :

composer require sportrizer/sportysky-client-php

API Client

getCountryForecastResponse

Example :

$response = $apiClient->getCountryForecastResponse('FR', new \DateTime('2020-01-14T17:36:00+00:00'));
$data = json_decode($response->getBody()->getContents(), true);

getRegionForecastResponse

Example :

$response = $apiClient->getRegionForecastResponse('FR-BRE', new \DateTime('2020-01-14T17:36:00+00:00'));
$data = json_decode($response->getBody()->getContents(), true);

getDepartmentForecastResponse

Example :

$response = $apiClient->getDepartmentForecastResponse('FR-29', new \DateTime('2020-01-14T17:36:00+00:00'));
$data = json_decode($response->getBody()->getContents(), true);

getSpotForecastResponse

Example :

$response = $apiClient->getSpotForecastResponse(
    '1234-1234-1234-1234',
    new \DateTime('2020-01-14T17:36:00+00:00'),
    new \DateTime('2020-01-14T17:36:00+00:00')
);
$data = json_decode($response->getBody()->getContents(), true);

getSpotForecastByCodeAndCountryResponse

Example :

$response = $apiClient->getSpotForecastByCodeAndCountryResponse(
    '29000',
    'FR',
    new \DateTime('2020-01-14T17:36:00+00:00'),
    new \DateTime('2020-01-14T17:36:00+00:00')
);
$data = json_decode($response->getBody()->getContents(), true);

getSpotsResponse

Example :

use Sportrizer\Sportysky\Utils\Geo\Box;
use Sportrizer\Sportysky\Utils\Geo\Point;

// search nearest spot
$response = $apiClient->getSpotsResponse(
    new Point(33.3, 44.4)
);
$data = json_decode($response->getBody()->getContents(), true);

// find all spots inside a bounding box
$response = $apiClient->getSpotsResponse(
    null,
    new Box(new Point(22.2, 33.3), new Point(33.3, 44.4))
);

// pagination
$response = $apiClient->getSpotsResponse(
    null,
    null,
    3 // get page 3
);

$data = json_decode($response->getBody()->getContents(), true);

getForecastResponse

Example :

$response = $apiClient->getForecastResponse(new \DateTime('2020-01-14T17:36:00+00:00'), null, null, null, 'FR');
$data = json_decode($response->getBody()->getContents(), true);

Integration with the SportySKY javascript library

This library is developed to work seamlessly with the SportySKY javascript library provided by SportRIZER.

Create a php script that will be called by the javascript library :

<?php

declare(strict_types=1);

use GuzzleHttp\Psr7\ServerRequest;
use Sportrizer\Sportysky\ApiClient;
use Sportrizer\Sportysky\Authenticator;
use Sportrizer\Sportysky\ServerRequestHandler;
use Laminas\HttpHandlerRunner\Emitter\SapiStreamEmitter;

require '../vendor/autoload.php';

// Authenticate the server to SportRIZER
$authenticator = new Authenticator(getenv('SPORTYSKY_CLIENT_ID'), getenv('SPORTYSKY_CLIENT_SECRET'));

// Create a new SportySKY API client
// with the JWT token provided by the authenticator
$apiClient = new ApiClient($authenticator->getToken());

// Handles the request made by the JS API
$apiResponse = (new ServerRequestHandler($apiClient))->handle(ServerRequest::fromGlobals());

// Outputs the SportySKY API response
(new SapiStreamEmitter())->emit($apiResponse);

Note : The SapiStreamEmitter comes with a suggested library.
Install it with this command :

composer require laminas/laminas-httphandlerrunner

You should set your client ID ( SPORTYSKY_CLIENT_ID ) and client secret ( SPORTYSKY_CLIENT_SECRET ) in environment variables.

This script will authenticate your sever and return json from the SportySKY API that will be consumed by the javascript library.

Caching

SportySKY API responses

API calls are made by Guzzle which can be configured with the Kevinrob's cache middleware

You can for example provide a PSR-16 compatible Redis cache to the second argument of ApiClient :

use Desarrolla2\Cache\Predis;
use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Storage\Psr16CacheStorage;
use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy;
use Predis\Client;
use Sportrizer\Sportysky\ApiClient;

// Authenticator ... 

$cacheHandler = HandlerStack::create();
$cacheHandler->push(new CacheMiddleware(
    new PublicCacheStrategy(
        new Psr16CacheStorage(
            new Predis(
                new Client(getenv('REDIS_URL')) // tcp://127.0.0.1:6379
            )
        )
    )
));

$apiClient = new ApiClient($authenticator->getToken(), $cacheHandler);

Note : The predis/predis package is required to make the Redis cache work.
Install it with this command :

composer require predis/predis

See full example

By default, the responses will be cached according to the cache headers provided by the API but you can define your own strategy : See more examples

Some other PSR-16 adapters are already shipped with this library :

https://github.com/desarrolla2/Cache#adapters

JWT Authentication token

By default, the JWT token is cached in the temporary system directory until its expiration but you can provide your own PSR-16 cache integration as the third argument of the Authenticator .

Exemple with Redis :

use Desarrolla2\Cache\Predis;
use Predis\Client;
use Sportrizer\Sportysky\Authenticator;

$redisCache = new Predis(new Client(getenv('REDIS_URL'))); // tcp://127.0.0.1:6379

$authenticator = new Authenticator(getenv('SPORTYSKY_CLIENT_ID'), getenv('SPORTYSKY_CLIENT_SECRET'), $redisCache);

Modifications

Modification on API return

API returns can be modified before processed or caching

use Sportrizer\Sportysky\ApiClient;
use Sportrizer\Sportysky\Authenticator;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\ResponseInterface;

$authenticator = new Authenticator(getenv('SPORTYSKY_CLIENT_ID'), getenv('SPORTYSKY_CLIENT_SECRET'));

$handler = HandlerStack::create();
$handler->push(
    Middleware::mapResponse(
        function (ResponseInterface $response) {
            $bodyContent                    = json_decode($response->getBody()->getContents(), true);
            $bodyContent['test_Middleware'] = true;

            return $response->withBody(
                \GuzzleHttp\Psr7\stream_for(
                    json_encode($bodyContent)
                )
            );
        }
    )
);

$apiClient = new ApiClient($authenticator->getToken(), $handler);

Modification on API return and caching

Example with a cache File on /tmp

use Sportrizer\Sportysky\ApiClient;
use Sportrizer\Sportysky\Authenticator;
use GuzzleHttp\HandlerStack;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Middleware;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy;
use Doctrine\Common\Cache\FilesystemCache;
use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage;

$authenticator = new Authenticator(getenv('SPORTYSKY_CLIENT_ID'), getenv('SPORTYSKY_CLIENT_SECRET'));

$handler = HandlerStack::create();
$handler->push(
    Middleware::mapResponse(
        function (ResponseInterface $response) {
            $bodyContent                    = json_decode($response->getBody()->getContents(), true);
            $bodyContent['test_Middleware'] = true;

            return $response->withBody(
                \GuzzleHttp\Psr7\stream_for(
                    json_encode($bodyContent)
                )
            );
        }
    )
);

$handler->push(
    new CacheMiddleware(
        new PublicCacheStrategy(
            new DoctrineCacheStorage(
                new FilesystemCache('/tmp/')
            )
        )
    )
);

$apiClient = new ApiClient($authenticator->getToken(), $handler);

Examples

Integration with the JS library

Testing

composer test