1 | <?php namespace DiegoCaprioli\Larachimp\Services; |
||
11 | class Larachimp { |
||
12 | |||
13 | /** |
||
14 | * Base URI for Mailchimp API v3 |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $baseuri; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $apikey; |
||
24 | |||
25 | /** |
||
26 | * The connection options |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $options; |
||
30 | |||
31 | /** |
||
32 | * @var \GuzzleHttp\Client |
||
33 | */ |
||
34 | protected $client; |
||
35 | |||
36 | /** |
||
37 | * Make this class use a logger and it's basic methods |
||
38 | */ |
||
39 | use BasicLogging; |
||
40 | |||
41 | /** |
||
42 | * Creates a new Larachimp instance |
||
43 | * |
||
44 | * @param Log The logger instance to use |
||
45 | */ |
||
46 | 6 | public function __construct(Log $log = null) |
|
50 | |||
51 | /** |
||
52 | * Initializes the instance with the proper configuration values |
||
53 | * |
||
54 | * @param string $apikey The API key to the Mailchimp API |
||
55 | * @param string $baseuri The base URI to use for the requests. Make sure it has a trailing/ending "/" |
||
56 | * @param array $clientOptions The options array in the Guzzle Client expected format * |
||
57 | * @see http://guzzle3.readthedocs.io/docs.html Guzzle Docs |
||
58 | */ |
||
59 | 6 | public function initialize($apikey = '', $baseuri = '', $clientOptions = []) |
|
68 | |||
69 | /** |
||
70 | * If there's a logger defined, it logs the request made |
||
71 | * |
||
72 | * @param string $method |
||
73 | * @param string $resource |
||
74 | * @param array $options |
||
75 | */ |
||
76 | 6 | protected function logRequest($method, $resource, array $options = []) |
|
82 | |||
83 | |||
84 | /** |
||
85 | * If there's a logger defined, it logs the response returned |
||
86 | * |
||
87 | * @param mixed $response |
||
88 | */ |
||
89 | 6 | protected function logResponse($response) |
|
93 | |||
94 | /** |
||
95 | * Makes a simple API request to Mailchimp. |
||
96 | * It uses the base URI used when initializing this service class, and |
||
97 | * concatenates the $resource to form the URL of the request. |
||
98 | * The optional $options array are the standar Guzzle Client request options. |
||
99 | * The request will automatically include an option setting the 'headers' to |
||
100 | * use the Authorization API KEY as configured. |
||
101 | * Finally calls the request method of the Guzzle client using $method, the |
||
102 | * generated URL of the request, and the $options. |
||
103 | * |
||
104 | * @param string $resource The resource |
||
105 | * @param string $method The request method (GET, POST, PATCH, PUT, DELETE) |
||
106 | * @param array $options An array of request options, as accepted by Guzzle Client request method |
||
107 | * @return mixed The json_decode'd version of the response body |
||
108 | * @see http://developer.mailchimp.com/documentation/mailchimp/guides/get-started-with-mailchimp-api-3/ Mailchimp API v3 Reference |
||
109 | * @see http://guzzle3.readthedocs.io/docs.html Guzzle Docs |
||
110 | */ |
||
111 | 6 | public function request($method, $resource, array $options = []) |
|
143 | |||
144 | /** |
||
145 | * Makes a simple GET API request to Mailchimp. |
||
146 | * |
||
147 | * @param string $resource The resource |
||
148 | * @param array $options An array of request options, as accepted by Guzzle Client request method |
||
149 | * @return mixed The json_decode'd version of the response body |
||
150 | * @see Larachimp::request() |
||
151 | */ |
||
152 | 6 | public function get($resource, array $options = []) |
|
156 | |||
157 | /** |
||
158 | * Makes a simple POST API request to Mailchimp. |
||
159 | * |
||
160 | * @param string $resource The resource |
||
161 | * @param array $options An array of request options, as accepted by Guzzle Client request method |
||
162 | * @return mixed The json_decode'd version of the response body |
||
163 | * @see Larachimp::request() |
||
164 | */ |
||
165 | 5 | public function post($resource, array $options = []) |
|
169 | |||
170 | /** |
||
171 | * Makes a simple PATCH API request to Mailchimp. |
||
172 | * |
||
173 | * @param string $resource The resource |
||
174 | * @param array $options An array of request options, as accepted by Guzzle Client request method |
||
175 | * @return mixed The json_decode'd version of the response body |
||
176 | * @see Larachimp::request() |
||
177 | */ |
||
178 | 1 | public function patch($resource, array $options = []) |
|
182 | |||
183 | /** |
||
184 | * Makes a simple PUT API request to Mailchimp. |
||
185 | * |
||
186 | * @param string $resource The resource |
||
187 | * @param array $options An array of request options, as accepted by Guzzle Client request method |
||
188 | * @return mixed The json_decode'd version of the response body |
||
189 | * @see Larachimp::request() |
||
190 | */ |
||
191 | public function put($resource, array $options = []) |
||
195 | |||
196 | /** |
||
197 | * Makes a simple DELETE API request to Mailchimp. |
||
198 | * |
||
199 | * @param string $resource The resource |
||
200 | * @param array $options An array of request options, as accepted by Guzzle Client request method |
||
201 | * @return mixed The json_decode'd version of the response body |
||
202 | * @see Larachimp::request() |
||
203 | */ |
||
204 | 5 | public function delete($resource, array $options = []) |
|
208 | |||
209 | |||
210 | } |