|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Communique. |
|
5
|
|
|
* |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Communique; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Communique Pluggable REST Client |
|
17
|
|
|
* |
|
18
|
|
|
* |
|
19
|
|
|
* @author Robert Main |
|
20
|
|
|
* @package Communique |
|
21
|
|
|
* @copyright Robert Main |
|
22
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
class Communique { |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The base path of the API. All other API paths will be used relative to this |
|
29
|
|
|
* @var String |
|
30
|
|
|
*/ |
|
31
|
|
|
private $_BASE_URL; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* An array of interceptors used for processing the requests |
|
35
|
|
|
* @var Array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $_interceptors = array(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* An implementation of \Communique\HTTPClient |
|
41
|
|
|
* @var \Communique\HTTPClient |
|
42
|
|
|
*/ |
|
43
|
|
|
private $_http; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Constructs Communique REST library. |
|
47
|
|
|
* @param String $base_url The base URL of the API you wish to make requests to. All other paths referenced will be treated as relative to this. |
|
48
|
|
|
* @param array $interceptors An array of any interceptors you wish to use to modify the request. An interceptor could do anything from JSON parsing to OAuth request signing. |
|
49
|
|
|
* @param \Communique\HTTPClient $http_client The HTTP client you wish to make the request with |
|
50
|
|
|
* @throws \Communique\CommuniqueException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct($base_url = '', array $interceptors = array(), $http_client = null) { |
|
53
|
|
|
$this->_BASE_URL = $base_url; |
|
54
|
|
|
$this->_interceptors = $interceptors; |
|
55
|
|
|
if ($http_client) { |
|
56
|
|
|
$this->_http = $http_client; |
|
57
|
|
|
} else { |
|
58
|
|
|
//@codeCoverageIgnoreStart |
|
59
|
|
|
$this->_http = new \Communique\CurlHTTPClient(); |
|
60
|
|
|
//@codeCoverageIgnoreEnd |
|
61
|
|
|
} |
|
62
|
|
|
foreach ($this->_interceptors as $interceptor) { |
|
63
|
|
|
if (!$interceptor instanceof \Communique\Interceptor) { |
|
64
|
|
|
throw new \Communique\CommuniqueException('Invalid request interceptor provided'); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Makes the HTTP request using the chosen HTTP client. |
|
71
|
|
|
* @param \Communique\RESTClientRequest $request A RESTClientRequest object encapsulating the request |
|
72
|
|
|
* @param callable $debug A debugging callback to be run after the request has finished. This function is expected to accept two parameters, \Communique\RESTClientRequest and \Communique\RESTClientResponse |
|
73
|
|
|
* @return \Communique\RESTClientResponse A RESTClientResponse object encapsulating the response |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function _call(\Communique\RESTClientRequest $request, $debug = null) { |
|
76
|
|
|
foreach ($this->_interceptors as $request_interceptor) { |
|
77
|
|
|
$request = $request_interceptor->request($request); |
|
78
|
|
|
} |
|
79
|
|
|
$response = $this->_http->request($request); |
|
80
|
|
|
foreach ($this->_interceptors as $response_interceptor) { |
|
81
|
|
|
$response = $response_interceptor->response($response); |
|
82
|
|
|
} |
|
83
|
|
|
if ($debug) { |
|
84
|
|
|
$debug($request, $response); |
|
85
|
|
|
} |
|
86
|
|
|
return $response; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Make an HTTP GET request |
|
91
|
|
|
* @param String $url The API to make the request to |
|
92
|
|
|
* @param mixed $payload The request payload (this will be url encoded and added as query string parameters) |
|
93
|
|
|
* @param array $headers Any headers you want to add to the request(optional) |
|
94
|
|
|
* @param callable $debug A function to be used for request debugging. |
|
95
|
|
|
* This function should accept two parameters, one for the request object one for the response object. |
|
96
|
|
|
* @return \Communique\RESTClientResponse REST response encapsulation object |
|
97
|
|
|
*/ |
|
98
|
|
|
public function get($url, $payload = array(), array $headers = array(), $debug = null) { |
|
99
|
|
|
$request = new \Communique\RESTClientRequest('get', $this->_BASE_URL . $url, $payload, $headers); |
|
100
|
|
|
return $this->_call($request, $debug); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Make an HTTP PUT request |
|
105
|
|
|
* @param String $url The API to make the request to |
|
106
|
|
|
* @param mixed $payload The payload of the request(any data you wish to send across) |
|
107
|
|
|
* @param array $headers Any headers you want to add to the request(optional) |
|
108
|
|
|
* @param callable $debug A function to be used for request debugging. |
|
109
|
|
|
* This function should accept two parameters, one for the request object one for the response object. |
|
110
|
|
|
* @return \Communique\RESTClientResponse REST response encapsulation object |
|
111
|
|
|
*/ |
|
112
|
|
|
public function put($url, $payload, array $headers = array(), $debug = null) { |
|
113
|
|
|
$request = new \Communique\RESTClientRequest('put', $this->_BASE_URL . $url, $payload, $headers); |
|
114
|
|
|
return $this->_call($request, $debug); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Make an HTTP POST request |
|
119
|
|
|
* @param String $url The API to make the request to |
|
120
|
|
|
* @param mixed $payload The payload of the request(any data you wish to send across) |
|
121
|
|
|
* @param array $headers Any headers you want to add to the request(optional) |
|
122
|
|
|
* @param callable $debug A function to be used for request debugging. |
|
123
|
|
|
* This function should accept two parameters, one for the request object one for the response object. |
|
124
|
|
|
* @return \Communique\RESTClientResponse REST response encapsulation object |
|
125
|
|
|
*/ |
|
126
|
|
|
public function post($url, $payload, array $headers = array(), $debug = null) { |
|
127
|
|
|
$request = new \Communique\RESTClientRequest('post', $this->_BASE_URL . $url, $payload, $headers); |
|
128
|
|
|
return $this->_call($request, $debug); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Make an HTTP DELETE request |
|
133
|
|
|
* @param String $url The API to make the request to |
|
134
|
|
|
* @param mixed $payload The payload of the request(any data you wish to send across) |
|
135
|
|
|
* @param array $headers Any headers you want to add to the request(optional) |
|
136
|
|
|
* @param callable $debug A function to be used for request debugging. |
|
137
|
|
|
* This function should accept two parameters, one for the request object one for the response object. |
|
138
|
|
|
* @return \Communique\RESTClientResponse REST response encapsulation object |
|
139
|
|
|
*/ |
|
140
|
|
|
public function delete($url, $payload = array(), array $headers = array(), $debug = null) { |
|
141
|
|
|
$request = new \Communique\RESTClientRequest('delete', $this->_BASE_URL . $url, $payload, $headers); |
|
142
|
|
|
return $this->_call($request, $debug); |
|
143
|
|
|
} |
|
144
|
|
|
} |