iammordaty / beatport-oauth-middleware
Guzzle 6.x OAuth middleware for Beatport API. Allows server-side querying and access token caching.
Requires
- php: >=7.1
- ext-json: *
- guzzlehttp/guzzle: >=6.0
- guzzlehttp/oauth-subscriber: ^0.3.0
- iammordaty/guzzle-urlencoded-response-middleware: ^0.1.0
- iammordaty/guzzlehttp-factory: ^1.0
- psr/simple-cache: ^1.0
Requires (Dev)
- cache/array-adapter: ^1.0
- phpstan/phpstan: ^0.11.19
- phpunit/phpunit: ^7
- shrikeh/teapot: ^2.3
This package is auto-updated.
Last update: 2025-03-29 00:43:17 UTC
README
Guzzle 6.x OAuth middleware for Beatport API. Allows server-side querying and access token caching.
Table of Contents
Installation
The easiest way to install this middleware is via composer:
$ composer require iammordaty/beatport-oauth-middleware
Requirements
- PHP 7.1+
- Beatport's Consumer Key, Consumer Secret and account credentials
For more information about Consumer Key, Consumer Secret see Beatport announcement and visit Beatport API documentation.
Usage
The following example demonstrates how to initialize a Guzzle client with middleware, and then retrieve information from Beatport API about the track based on its ID.
use BeatportOauth\AccessTokenProvider; use BeatportOauth\OauthMiddlewareFactory; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; $oauthParams = [ 'consumer_key' => $_ENV['consumer_key'], 'consumer_secret' => $_ENV['consumer_secret'], 'username' => $_ENV['username'], 'password' => $_ENV['password'], ]; $middleware = OauthMiddlewareFactory::create($oauthParams); $stack = HandlerStack::create(); $stack->push($middleware); $client = new Client([ 'auth' => 'oauth', 'base_uri' => AccessTokenProvider::BASE_URI, 'handler' => $stack, ]); $response = $client->get('catalog/3/tracks', [ 'query' => [ 'id' => 12387959 ], ]); $contents = json_decode($response->getBody()->getContents(), true);
In addition, it is also possible to cache access token for later use.
use BeatportOauth\OauthMiddlewareFactory; use Cache\Adapter\PHPArray\ArrayCachePool; use GuzzleHttp\HandlerStack; $oauthParams = [/* */]; $cache = new ArrayCachePool(); // or any other PSR-16 compatible cache pool $cacheConfig = [ 'key' => 'my-access-token-info' ]; // optional $middleware = OauthMiddlewareFactory::createWithCachedToken( $oauthParams, $cache, $cacheConfig ); $stack = HandlerStack::create(); $stack->push($middleware);
Tests
Copy phpunit.xml.dist
file to phpunit.xml
and fill in the missing parameters.
Now you can test middleware by running the following command:
$ ./vendor/bin/phpunit
Further information
- Beatport API documentation
- Beatport announcement of API key acquisition
License
iammordaty/beatport-oauth-middleware is licensed under the MIT License.