Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first Requests HTTP client implementation #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified lib/EchoNest/Api.php
100755 → 100644
Empty file.
Empty file modified lib/EchoNest/Api/Artist.php
100755 → 100644
Empty file.
Empty file modified lib/EchoNest/Api/Catalog.php
100755 → 100644
Empty file.
Empty file modified lib/EchoNest/Api/Playlist.php
100755 → 100644
Empty file.
Empty file modified lib/EchoNest/Api/Song.php
100755 → 100644
Empty file.
Empty file modified lib/EchoNest/Api/Track.php
100755 → 100644
Empty file.
Empty file modified lib/EchoNest/Client.php
100755 → 100644
Empty file.
Empty file modified lib/EchoNest/HttpClient/Curl.php
100755 → 100644
Empty file.
Empty file modified lib/EchoNest/HttpClient/Exception.php
100755 → 100644
Empty file.
75 changes: 75 additions & 0 deletions lib/EchoNest/HttpClient/Requests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* Performs requests on EchoNest API. API documentation should be self-explanatory.
*
* @author Brent Shaffer <bshafs at gmail dot com>
* @license MIT License
*/
class EchoNest_HttpClient_Requests extends EchoNest_HttpClient
{
/**
* Send a request to the server, receive a response
*
* @param string $apiPath Request API path
* @param array $parameters Parameters
* @param string $httpMethod HTTP method to use
*
* @return string HTTP response
*/
protected function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array())
{
if($this->options['api_key'])
{
$parameters = array_merge(array(
'format' => $this->options['format'],
'api_key' => $this->options['api_key']
), $parameters);
}

$headers = array();
$headers['User-Agent'] = $this->options['user_agent'];

if ('GET' === $httpMethod && !empty($parameters)) {
$queryString = utf8_encode($this->buildQuery($parameters));
$url .= '?' . $queryString;

$this->debug('send ' . $httpMethod . ' request: ' . $url);
$request = Requests::get($url, $headers);
}
else {
$this->debug('send ' . $httpMethod . ' request: ' . $url);
$request = Requests::post($url, $headers, $parameters);
}

return $request->body;
}

protected function buildQuery($parameters)
{
$append = '';
foreach ($parameters as $key => $value)
{
// multiple parameter passed
if (is_array($value)) {
foreach ($value as $val) {
$append.=sprintf('&%s=%s', $key, $val);
}
unset($parameters[$key]);
}
elseif (is_bool($value)) {
$parameters[$key] = $value ? 'true' : 'false';
}
}

return http_build_query($parameters, '', '&') . $append;
}

protected function debug($message)
{
if($this->options['debug'])
{
print $message."\n";
}
}
}