Skip to content

Commit 76a4a34

Browse files
committed
initial commits - API abstract, request abstract, and ArtistApi classes complete
0 parents  commit 76a4a34

7 files changed

+1043
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2010 Brent Shaffer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# PHP Echo Nest API

lib/EchoNestApi.php

+231
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
<?php
2+
3+
/**
4+
* Simple PHP GitHub API
5+
*
6+
* @tutorial http://github.com/ornicar/php-github-api/blob/master/README.markdown
7+
* @version 2.6
8+
* @author Brent Shaffer <bshafs at gmail dot com>
9+
* @license MIT License
10+
*
11+
* Website: http://github.com/ornicar/php-github-api
12+
* Tickets: http://github.com/ornicar/php-github-api/issues
13+
*/
14+
class EchoNestApi
15+
{
16+
/**
17+
* The request instance used to communicate with GitHub
18+
* @var EchoNestApiRequest
19+
*/
20+
protected $request = null;
21+
22+
/**
23+
* The list of loaded API instances
24+
* @var array
25+
*/
26+
protected $apis = array();
27+
28+
/**
29+
* Use debug mode (prints debug messages)
30+
* @var bool
31+
*/
32+
protected $debug;
33+
34+
/**
35+
* Instanciate a new GitHub API
36+
*
37+
* @param bool $debug print debug messages
38+
*/
39+
public function __construct($debug = false)
40+
{
41+
$this->debug = $debug;
42+
}
43+
44+
/**
45+
* Authenticate a user for all next requests
46+
*
47+
* @param string $apiKey EchoNest API key
48+
* @return EchoNestApi fluent interface
49+
*/
50+
public function authenticate($apiKey)
51+
{
52+
$this->getRequest()
53+
->setOption('api_key', $apiKey);
54+
55+
return $this;
56+
}
57+
58+
/**
59+
* Deauthenticate a user for all next requests
60+
*
61+
* @return EchoNestApi fluent interface
62+
*/
63+
public function deAuthenticate()
64+
{
65+
return $this->authenticate(null);
66+
}
67+
68+
/**
69+
* Call any route, GET method
70+
* Ex: $api->get('repos/show/my-username/my-repo')
71+
*
72+
* @param string $route the GitHub route
73+
* @param array $parameters GET parameters
74+
* @param array $requestOptions reconfigure the request
75+
* @return array data returned
76+
*/
77+
public function get($route, array $parameters = array(), $requestOptions = array())
78+
{
79+
return $this->getRequest()->get($route, $parameters, $requestOptions);
80+
}
81+
82+
/**
83+
* Call any route, POST method
84+
* Ex: $api->post('repos/show/my-username', array('email' => '[email protected]'))
85+
*
86+
* @param string $route the GitHub route
87+
* @param array $parameters POST parameters
88+
* @param array $requestOptions reconfigure the request
89+
* @return array data returned
90+
*/
91+
public function post($route, array $parameters = array(), $requestOptions = array())
92+
{
93+
return $this->getRequest()->post($route, $parameters, $requestOptions);
94+
}
95+
96+
/**
97+
* Get the request
98+
*
99+
* @return EchoNestApiRequest a request instance
100+
*/
101+
public function getRequest()
102+
{
103+
if(!isset($this->request))
104+
{
105+
require_once(dirname(__FILE__).'/request/EchoNestApiRequest.php');
106+
$this->request = new EchoNestApiRequest(array(
107+
'debug' => $this->debug
108+
));
109+
}
110+
111+
return $this->request;
112+
}
113+
114+
/**
115+
* Inject another request
116+
*
117+
* @param EchoNestApiRequest a request instance
118+
* @return EchoNestApi fluent interface
119+
*/
120+
public function setRequest(EchoNestApiRequest $request)
121+
{
122+
$this->request = $request;
123+
124+
return $this;
125+
}
126+
127+
/**
128+
* Get the artist API
129+
*
130+
* @return EchoNestApiArtist the artist API
131+
*/
132+
public function getArtistApi()
133+
{
134+
if(!isset($this->apis['artist']))
135+
{
136+
require_once(dirname(__FILE__).'/api/EchoNestApiArtist.php');
137+
$this->apis['artist'] = new EchoNestApiArtist($this);
138+
}
139+
140+
return $this->apis['artist'];
141+
}
142+
143+
/**
144+
* Get the song API
145+
*
146+
* @return EchoNestApiSong the song API
147+
*/
148+
public function getSongApi()
149+
{
150+
if(!isset($this->apis['song']))
151+
{
152+
require_once(dirname(__FILE__).'/api/EchoNestApiSong.php');
153+
$this->apis['song'] = new EchoNestApiSong($this);
154+
}
155+
156+
return $this->apis['song'];
157+
}
158+
159+
/**
160+
* Get the track API
161+
*
162+
* @return EchoNestApiTrack the track API
163+
*/
164+
public function getCommitApi()
165+
{
166+
if(!isset($this->apis['track']))
167+
{
168+
require_once(dirname(__FILE__).'/api/EchoNestApiTrack.php');
169+
$this->apis['track'] = new EchoNestApiTrack($this);
170+
}
171+
172+
return $this->apis['track'];
173+
}
174+
175+
/**
176+
* Get the playlist API
177+
*
178+
* @return EchoNestApiPlaylist the playlist API
179+
*/
180+
public function getPlaylistApi()
181+
{
182+
if(!isset($this->apis['playlist']))
183+
{
184+
require_once(dirname(__FILE__).'/api/EchoNestApiPlaylist.php');
185+
$this->apis['playlist'] = new EchoNestApiPlaylist($this);
186+
}
187+
188+
return $this->apis['playlist'];
189+
}
190+
191+
/**
192+
* Get the catalog API
193+
*
194+
* @return EchoNestApiCatalog the catalog API
195+
*/
196+
public function getCatalogApi()
197+
{
198+
if(!isset($this->apis['catalog']))
199+
{
200+
require_once(dirname(__FILE__).'/api/EchoNestApiCatalog.php');
201+
$this->apis['catalog'] = new EchoNestApiCatalog($this);
202+
}
203+
204+
return $this->apis['catalog'];
205+
}
206+
207+
/**
208+
* Inject another API instance
209+
*
210+
* @param string $name the API name
211+
* @param EchoNestApiAbstract $api the API instance
212+
* @return EchoNestApi fluent interface
213+
*/
214+
public function setApi($name, EchoNestApiAbstract $instance)
215+
{
216+
$this->apis[$name] = $instance;
217+
218+
return $this;
219+
}
220+
221+
/**
222+
* Get any API
223+
*
224+
* @param string $name the API name
225+
* @return EchoNestApiAbstract the API instance
226+
*/
227+
public function getApi($name)
228+
{
229+
return $this->apis[$name];
230+
}
231+
}

lib/api/EchoNestApiAbstract.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Abstract class for EchoNestApi classes
5+
*
6+
* @author Brent Shaffer <bshafs at gmail dot com>
7+
* @license MIT License
8+
*/
9+
abstract class EchoNestApiAbstract
10+
{
11+
/**
12+
* The core API
13+
* @var EchoNestApiAbstract
14+
*/
15+
protected
16+
$api,
17+
$options = array();
18+
19+
public function __construct(EchoNestApi $api, $options = array())
20+
{
21+
$this->api = $api;
22+
$this->options = $options;
23+
}
24+
25+
/**
26+
* Change an option value.
27+
*
28+
* @param string $name The option name
29+
* @param mixed $value The value
30+
*
31+
* @return EchoNestApiAbstract the current object instance
32+
*/
33+
public function setOption($name, $value)
34+
{
35+
$this->options[$name] = $value;
36+
37+
return $this;
38+
}
39+
40+
/**
41+
* Get an option value.
42+
*
43+
* @param string $name The option name
44+
*
45+
* @return mixed The option value
46+
*/
47+
public function getOption($name, $default = null)
48+
{
49+
return isset($this->options[$name]) ? $this->options[$name] : $default;
50+
}
51+
}

0 commit comments

Comments
 (0)