Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
Initial setup with Guzzle, added getArtist, getArtistReleases, search
Browse files Browse the repository at this point in the history
  • Loading branch information
ricbra committed Jul 29, 2014
1 parent 768a242 commit 7283697
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 6 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2010-2011 Richard van den Brand
Copyright (c) 2010-2014 Richard van den Brand

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"keywords": ["discogs", "api"],
"license": "MIT",
"require": {
"kriswallsmith/buzz": ">=0.5",
"php": ">=5.3.0"
"php": ">=5.4.0",
"guzzlehttp/guzzle": "~4.0",
"guzzlehttp/guzzle-services": "~0.3.0"
},
"require-dev": {
"phpunit/phpunit": "~4"
Expand All @@ -19,5 +20,7 @@
],
"autoload": {
"psr-0": { "Discogs": "lib/" }
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
33 changes: 33 additions & 0 deletions lib/Discogs/ClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/*
* This file is part of the php-discogs-api.
*
* (c) Richard van den Brand <richard@vandenbrand.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Discogs;

use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;

class ClientFactory
{
public static function factory(array $config = [])
{
$defaultConfig = [
'defaults' => [
'headers' => ['User-Agent' => 'php-discogs-api'],
]
];

$client = new Client(array_merge($defaultConfig, $config));
$service = include __DIR__ . '/../../resources/service.php';
$description = new Description($service);

return new GuzzleClient($client, $description);
}
}
154 changes: 154 additions & 0 deletions resources/service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php
/*
* This file is part of the php-discogs-api.
*
* (c) Richard van den Brand <richard@vandenbrand.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return [
'baseUrl' => 'http://api.discogs.com/',
'operations' => [
'getArtist' => [
'httpMethod' => 'GET',
'uri' => 'artists/{id}',
'responseModel' => 'GetResponse',
'parameters' => [
'id' => [
'type' => 'string',
'location' => 'uri',
'required' => true
]
]
],
'getArtistReleases' => [
'httpMethod' => 'GET',
'uri' => 'artists/{id}/releases',
'responseModel' => 'GetResponse',
'parameters' => [
'id' => [
'type' => 'string',
'location' => 'uri',
'required' => true
],
'per_page' => [
'type' => 'integer',
'location' => 'query',
'required' => false
],
'page' => [
'type' => 'integer',
'location' => 'query',
'required' => false
]
]
],
'search' => [
'httpMethod' => 'GET',
'uri' => 'database/search',
'responseModel' => 'GetResponse',
'parameters' => [
'q' => [
'type' => 'string',
'location' => 'query',
'required' => false
],
'type' => [
'type' => 'string',
'location' => 'query',
'required' => false
],
'title' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'release_title' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'credit' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'artist' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'anv' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'label' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'genre' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'style' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'country' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'year' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'format' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'catno' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'barcode' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'track' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'submitter' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
],
'contributor' => [
'type' => 'boolean',
'location' => 'query',
'required' => false
]
]
]
],
'models' => [
'GetResponse' => [
'type' => 'object',
'additionalProperties' => [
'location' => 'json'
],
]
]
];
71 changes: 71 additions & 0 deletions tests/Discogs/Test/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/*
* This file is part of the php-discogs-api.
*
* (c) Richard van den Brand <richard@vandenbrand.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Discogs\Test;

use Discogs\ClientFactory;
use GuzzleHttp\Subscriber\Mock;

class ClientTest extends \PHPUnit_Framework_TestCase
{
public function testGetArtist()
{
$client = $this->createClient('get_artist');
$response = $client->getArtist([
'id' => 45
]);
$this->assertSame($response['id'], 45);
$this->assertSame($response['name'], 'Aphex Twin');
$this->assertSame($response['realname'], 'Richard David James');
$this->assertInternalType('array', $response['images']);
$this->assertCount(9, $response['images']);
}

public function testGetArtistReleases()
{
$client = $this->createClient('get_artist_releases');

$response = $client->getArtistReleases([
'id' => 45,
'per_page' => 50,
'page' => 1
]);
$this->assertCount(50, $response['releases']);
$this->assertArrayHasKey('pagination', $response);
$this->assertArrayHasKey('per_page', $response['pagination']);
}

public function testSearch()
{
$client = $this->createClient('search');

$response = $client->search([
'q' => 'prodigy',
'type' => 'release',
'title' => true
]);
$this->assertCount(50, $response['results']);
$this->assertArrayHasKey('pagination', $response);
$this->assertArrayHasKey('per_page', $response['pagination']);
}

protected function createClient($mock)
{
$path = sprintf('%s/../../fixtures/%s', __DIR__, $mock);
$client = ClientFactory::factory();
$httpClient = $client->getHttpClient();
$mock = new Mock([
$path
]);
$httpClient->getEmitter()->attach($mock);

return $client;
}
}
11 changes: 11 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/*
* This file is part of the php-discogs-api.
*
* (c) Richard van den Brand <richard@vandenbrand.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require_once __DIR__.'/../vendor/autoload.php';
14 changes: 14 additions & 0 deletions tests/fixtures/get_artist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
HTTP/1.1 200 OK
Reproxy-Status: yes
Access-Control-Allow-Origin: *
Cache-Control: public, must-revalidate
Content-Type: application/json
Server: lighttpd
Content-Length: 5103
Date: Mon, 28 Jul 2014 18:40:39 GMT
X-Varnish: 1913846781 1913780733
Age: 259
Via: 1.1 varnish
Connection: close

{"profile": "British electronic musician and composer (born August 18, 1971 in Limerick, Ireland).\r\nAfter having released a number of albums and EPs on Warp Records, Rephlex and other labels under many aliases, he gained more and more success from the mid-90s with releases such as \"Come to Daddy\" in 1997 (#36 on UK charts) and \"Windowlicker\" in 1999 (#16 on UK charts).\r\nIn 1991, he co-founded the label [l=Rephlex] with Grant Wilson-Claridge.\r\n", "realname": "Richard David James", "releases_url": "http://api.discogs.com/artists/45/releases", "name": "Aphex Twin", "uri": "http://www.discogs.com/artist/45-Aphex-Twin", "urls": ["http://www.drukqs.net/", "http://warp.net/records/aphex-twin", "http://www.littlebig-agency.net/artists/aphex-twin/", "http://www.facebook.com/aphextwinafx", "http://twitter.com/AphexTwin", "http://aphextwin.sandbag.uk.com/", "http://en.wikipedia.org/wiki/Aphex_twin", "http://www.whosampled.com/Aphex-Twin/", "http://soundcloud.com/aphex-twin-official", "http://www.youtube.com/user/soundofworldcontrol"], "images": [{"uri": "http://api.discogs.com/image/A-45-1176664580.jpeg", "height": 704, "width": 486, "resource_url": "http://api.discogs.com/image/A-45-1176664580.jpeg", "type": "primary", "uri150": "http://api.discogs.com/image/A-150-45-1176664580.jpeg"}, {"uri": "http://api.discogs.com/image/A-45-1347812770-2937.jpeg", "height": 665, "width": 500, "resource_url": "http://api.discogs.com/image/A-45-1347812770-2937.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-45-1347812770-2937.jpeg"}, {"uri": "http://api.discogs.com/image/A-45-1209415630.jpeg", "height": 500, "width": 454, "resource_url": "http://api.discogs.com/image/A-45-1209415630.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-45-1209415630.jpeg"}, {"uri": "http://api.discogs.com/image/A-45-1126949091.jpeg", "height": 600, "width": 416, "resource_url": "http://api.discogs.com/image/A-45-1126949091.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-45-1126949091.jpeg"}, {"uri": "http://api.discogs.com/image/A-45-1126949071.jpeg", "height": 280, "width": 250, "resource_url": "http://api.discogs.com/image/A-45-1126949071.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-45-1126949071.jpeg"}, {"uri": "http://api.discogs.com/image/A-45-1388347702-9375.png", "height": 324, "width": 500, "resource_url": "http://api.discogs.com/image/A-45-1388347702-9375.png", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-45-1388347702-9375.png"}, {"uri": "http://api.discogs.com/image/A-45-1389288802-7408.png", "height": 366, "width": 274, "resource_url": "http://api.discogs.com/image/A-45-1389288802-7408.png", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-45-1389288802-7408.png"}, {"uri": "http://api.discogs.com/image/A-45-1403892052-9961.jpeg", "height": 389, "width": 541, "resource_url": "http://api.discogs.com/image/A-45-1403892052-9961.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-45-1403892052-9961.jpeg"}, {"uri": "http://api.discogs.com/image/A-45-1403892037-5712.jpeg", "height": 458, "width": 306, "resource_url": "http://api.discogs.com/image/A-45-1403892037-5712.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-45-1403892037-5712.jpeg"}], "resource_url": "http://api.discogs.com/artists/45", "aliases": [{"resource_url": "http://api.discogs.com/artists/42476", "id": 42476, "name": "Blue Calx (2)"}, {"resource_url": "http://api.discogs.com/artists/32985", "id": 32985, "name": "Bradley Strider"}, {"resource_url": "http://api.discogs.com/artists/803581", "id": 803581, "name": "Brian Tregaskin"}, {"resource_url": "http://api.discogs.com/artists/48", "id": 48, "name": "Caustic Window"}, {"resource_url": "http://api.discogs.com/artists/820", "id": 820, "name": "Dice Man, The"}, {"resource_url": "http://api.discogs.com/artists/46", "id": 46, "name": "GAK"}, {"resource_url": "http://api.discogs.com/artists/829972", "id": 829972, "name": "Karen Tregaskin"}, {"resource_url": "http://api.discogs.com/artists/3054120", "id": 3054120, "name": "Patrick Tregaskin"}, {"resource_url": "http://api.discogs.com/artists/1645212", "id": 1645212, "name": "PBoD"}, {"resource_url": "http://api.discogs.com/artists/2931", "id": 2931, "name": "Polygon Window"}, {"resource_url": "http://api.discogs.com/artists/599", "id": 599, "name": "Power-Pill"}, {"resource_url": "http://api.discogs.com/artists/37272", "id": 37272, "name": "Q-Chastic"}, {"resource_url": "http://api.discogs.com/artists/435132", "id": 435132, "name": "Richard D. James"}, {"resource_url": "http://api.discogs.com/artists/286337", "id": 286337, "name": "Smojphace"}, {"resource_url": "http://api.discogs.com/artists/3671244", "id": 3671244, "name": "Soit - P.P."}, {"resource_url": "http://api.discogs.com/artists/798219", "id": 798219, "name": "Tuss, The"}], "id": 45, "data_quality": "Correct", "namevariations": ["A-F-X Twin", "A.F.X.", "A.Twin", "AFX", "Apex Twin", "Aphex Twin, The", "Aphex Twins", "TheAphexTwin"]}
Loading

0 comments on commit 7283697

Please sign in to comment.