Skip to content

Commit

Permalink
fixes #6 - allows for the retrieval of the full reponse by setting th…
Browse files Browse the repository at this point in the history
…e 'raw' option to true
  • Loading branch information
bshaffer committed Jun 15, 2011
1 parent 3a84c26 commit f3aed54
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 63 deletions.
61 changes: 45 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
# PHP Echo Nest API

A simple, Object Oriented API wrapper for the EchoNest Api written with PHP5.
A simple, Object Oriented API wrapper for the EchoNest Api written with PHP5.
This library is modeled after the [php-github-api](https://github.com/ornicar/php-github-api) library built by [ornicar](https://github.com/ornicar)

Uses [EchoNest API v4](http://developer.echonest.com/docs/v4/).
Expand Down Expand Up @@ -43,18 43,11 @@ Cancels authentication.

Next requests will not be authenticated

## Artists

For searching artists, getting artist information and music.
Wraps [EchoNest Artist API](http://developer.echonest.com/docs/v4/artist.html).
## The Response

$artistApi = $echonest->getArtistApi();
The API tries to return to you the information you typically need, and spare you information such as status codes, messages, etc, if
the response was successful. Below is an example of a fully rendered response.

### Search for artists by name

$results = $echonest->getArtistApi()->search(array('name' => 'Radiohead'));
print_r($results);

Array
(
[status] => Array
Expand All @@ -76,6 69,42 @@ Wraps [EchoNest Artist API](http://developer.echonest.com/docs/v4/artist.html).

)

Often times, the status information is not needed. However, if you would like the API to return the full response (this is often
needed when dealing with pagers, as the "total" and "start" parameters are passed outside of the "artists" array, for example),
set the *raw* option to `true` for your API.

// pass options to getter
$response = $echonest->getArtistApi(array('raw' => true))->search(array('name' => 'Radiohead'));

// set options manually
$artistApi = $echonest->getArtistApi();
$artistApi->setOption('raw', true);
$response = $artistApi->search(array('name' => 'Radiohead'));

If you think this is dumb, let me know and I will consider making `raw` the default.

## Artists

For searching artists, getting artist information and music.
Wraps [EchoNest Artist API](http://developer.echonest.com/docs/v4/artist.html).

$artistApi = $echonest->getArtistApi();

### Search for artists by name

$results = $echonest->getArtistApi()->search(array('name' => 'Radiohead'));
print_r($results);

Array
(
[0] => Array
(
[name] => Radiohead
[id] => ARH6W4X1187B99274F
)

)

Returns an array of results as described in [http://developer.echonest.com/docs/v4/artist.html#search](http://developer.echonest.com/docs/v4/artist.html#search)

### Get information about an artist
Expand All @@ -98,16 127,16 @@ Api calls for getting data about songs.
Wraps [EchoNest Song API](http://developer.echonest.com/docs/v4/song.html).

$songApi = $echonest->getSongApi();

Please view the documentation in this project or on http://echonest.com to see all the options available

## Playlists

Api calls for generating playlists.
Wraps [EchoNest Playlist API](http://developer.echonest.com/docs/v4/playlist.html).

$playlistApi = $echonest->getPlaylistApi();

Please view the documentation in this project or on http://echonest.com to see all the options available

## Catalogs
Expand All @@ -116,7 145,7 @@ API calls for managing personal catalogs.
Wraps [EchoNest Catalog API](http://developer.echonest.com/docs/v4/catalog.html).

$catalogApi = $echonest->getCatalogApi();

Please view the documentation in this project or on http://echonest.com to see all the options available

## Tracks
Expand All @@ -125,7 154,7 @@ Methods for analyzing or getting info about tracks.
Wraps [EchoNest Track API](http://developer.echonest.com/docs/v4/track.html).

$trackApi = $echonest->getTrackApi();

Please view the documentation in this project or on http://echonest.com to see all the options available

# To Do
Expand Down
11 changes: 10 additions & 1 deletion lib/EchoNest/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 12,7 @@ abstract class EchoNest_Api implements EchoNest_ApiInterface
* The core EchoNest Client
* @var EchoNest_Client
*/
protected
protected
$client,
$options = array();

Expand Down Expand Up @@ -76,4 76,13 @@ public function getOption($name, $default = null)
{
return isset($this->options[$name]) ? $this->options[$name] : $default;
}

protected function returnResponse($response, $key = null)
{
if (!is_null($key) && !$this->getOption('raw')) {
return $response[$key];
}

return $response;
}
}
34 changes: 17 additions & 17 deletions lib/EchoNest/Api/Artist.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 46,7 @@ public function getAudio($results = 15, $start = 0)
'start' => $start,
));

return $response['audio'];
return $this->returnResponse($response, 'audio');
}

/**
Expand All @@ -66,7 66,7 @@ public function getBiographies($results = 15, $start = 0, $license = null)
'license' => $license,
));

return $response['biographies'];
return $this->returnResponse($response, 'biographies');
}

/**
Expand All @@ -86,7 86,7 @@ public function getBlogs($results = 15, $start = 0, $high_relevance = false)
'high_relevance' => $high_relevance,
));

return $response['blogs'];
return $this->returnResponse($response, 'blogs');
}

/**
Expand All @@ -100,7 100,7 @@ public function getFamiliarity()
{
$response = $this->getForArtist('artist/familiarity');

return $response['artist']['familiarity'];
return $this->returnResponse($this->returnResponse($response, 'familiarity'), 'artist');
}

/**
Expand All @@ -116,7 116,7 @@ public function getHotttnesss($type = 'overall')
'type' => $type,
));

return $response['artist']['hotttnesss'];
return $this->returnResponse($this->returnResponse($response, 'hotttnesss'), 'artist');
}

/**
Expand All @@ -136,7 136,7 @@ public function getImages($results = 15, $start = 0, $license = null)
'license' => $license,
));

return $response['images'];
return $this->returnResponse($response, 'images');
}

/**
Expand All @@ -156,7 156,7 @@ public function getNews($results = 15, $start = 0, $high_relevance = false)
'high_relevance' => $high_relevance,
));

return $response['news'];
return $this->returnResponse($response, 'news');
}

/**
Expand Down Expand Up @@ -187,7 187,7 @@ public function getProfile($bucket = null)
'bucket' => $bucket,
));

return $response;
return $this->returnResponse($response, 'artist');
}

/**
Expand All @@ -205,7 205,7 @@ public function getReviews($results = 15, $start = 0)
'start' => $start,
));

return $response['reviews'];
return $this->returnResponse($response, 'reviews');
}

/**
Expand All @@ -228,7 228,7 @@ public function search($options = array())
{
$response = $this->get('artist/search', $options);

return $response;
return $this->returnResponse($response, 'artists');
}

/**
Expand All @@ -246,7 246,7 @@ public function getSongs($results = 15, $start = 0)
'start' => $start,
));

return $response['songs'];
return $this->returnResponse($response, 'songs');
}

/**
Expand Down Expand Up @@ -285,7 285,7 @@ public function getSimilar($results = 15, $min_results = 15, $start = 0, $bucket
'seed_catalog' => $seed_catalog
));

return $response['artists'];
return $this->returnResponse($response, 'artists');
}

/**
Expand All @@ -301,7 301,7 @@ public function getTerms($sort = 'frequency')
'sort' => $sort,
));

return $response['terms'];
return $this->returnResponse($response, 'terms');
}

/**
Expand All @@ -324,7 324,7 @@ public function getTopHottt($results = 15, $start = 0, $bucket = null, $limit =
'limit' => $limit,
));

return $response;
return $this->returnResponse($response, 'artists');
}

/**
Expand All @@ -343,7 343,7 @@ public function getTopTerms($results = 15)
'limit' => $limit,
));

return $response['terms'];
return $this->returnResponse($response, 'terms');
}

/**
Expand All @@ -356,7 356,7 @@ public function getUrls()
{
$response = $this->getForArtist('artist/urls');

return $response['urls'];
return $this->returnResponse($response, 'urls');
}

/**
Expand All @@ -374,7 374,7 @@ public function getVideo($results = 15, $start = 0)
'start' => $start,
));

return $response['video'];
return $this->returnResponse($response, 'video');
}

/**
Expand Down
16 changes: 8 additions & 8 deletions lib/EchoNest/Api/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 24,7 @@ public function create($name, $type)
'type' => $type
));

return $response;
return $this->returnResponse($response);
}

/**
Expand All @@ -45,7 45,7 @@ public function update($id, $data, $data_type = 'json')
'data_type' => $data_type,
));

return $response;
return $this->returnResponse($response);
}

/**
Expand All @@ -61,7 61,7 @@ public function status($ticket)
'ticket' => $ticket,
));

return $response;
return $this->returnResponse($response);
}

/**
Expand All @@ -78,7 78,7 @@ public function profile($id, $byName = false)
$byName ? 'name' : 'id' => $id,
));

return $response['catalog'];
return $this->returnResponse($response, 'catalog');
}

/**
Expand All @@ -100,7 100,7 @@ public function read($id, $results = 15, $start = 0, $bucket = null)
'bucket' => $bucket,
));

return $response['catalog'];
return $this->returnResponse($response, 'catalog');
}

/**
Expand All @@ -124,7 124,7 @@ public function feed($id, $results = 15, $start = 0, $bucket = null, $since = nu
'since' => $since,
));

return $response['feed'];
return $this->returnResponse($response, 'feed');
}

/**
Expand All @@ -140,7 140,7 @@ public function delete($id)
'id' => $id,
));

return $response;
return $this->returnResponse($response);
}

/**
Expand All @@ -158,6 158,6 @@ public function getList($results = 15, $start = 0)
'start' => $start,
));

return $response['catalogs'];
return $this->returnResponse($response, 'catalogs');
}
}
6 changes: 3 additions & 3 deletions lib/EchoNest/Api/Playlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 20,7 @@ public function getStatic(array $options = array())
{
$response = $this->client->get('playlist/static', $options);

return $response['songs'];
return $this->returnResponse($response, 'songs');
}

/**
Expand All @@ -34,7 34,7 @@ public function getDynamic(array $options = array())
{
$response = $this->client->get('playlist/dynamic', $options);

return $response;
return $this->returnResponse($response);
}

/**
Expand All @@ -50,6 50,6 @@ public function getSessionInfo($session_id)
'session_id' => $session_id,
));

return $response;
return $this->returnResponse($response);
}
}
Loading

0 comments on commit f3aed54

Please sign in to comment.