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