Skip to content

Commit 2619b1b

Browse files
authored
Merge pull request #36 from ConvertKit/add-http-verb-functions
Add HTTP verb functions
2 parents e00000c + be25cfe commit 2619b1b

File tree

1 file changed

+92
-27
lines changed

1 file changed

+92
-27
lines changed

src/ConvertKit_API.php

Lines changed: 92 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,15 @@ private function create_log(string $message)
128128
*/
129129
public function get_account()
130130
{
131-
$request = $this->api_version . '/account';
131+
$request = 'account';
132132

133133
$options = [
134134
'api_secret' => $this->api_secret,
135135
];
136136

137137
$this->create_log(sprintf('GET account: %s, %s', $request, json_encode($options)));
138138

139-
return $this->make_request($request, 'GET', $options);
139+
return $this->get($request, $options);
140140
}
141141

142142

@@ -147,15 +147,15 @@ public function get_account()
147147
*/
148148
public function get_sequences()
149149
{
150-
$request = $this->api_version . '/sequences';
150+
$request = 'sequences';
151151

152152
$options = [
153153
'api_key' => $this->api_key,
154154
];
155155

156156
$this->create_log(sprintf('GET sequences: %s, %s', $request, json_encode($options)));
157157

158-
return $this->make_request($request, 'GET', $options);
158+
return $this->get($request, $options);
159159
}
160160

161161

@@ -169,7 +169,7 @@ public function get_sequences()
169169
*/
170170
public function get_sequence_subscriptions(int $sequence_id, string $sort_order = 'asc')
171171
{
172-
$request = $this->api_version . sprintf('/sequences/%s/subscriptions', $sequence_id);
172+
$request = sprintf('sequences/%s/subscriptions', $sequence_id);
173173

174174
$options = [
175175
'api_secret' => $this->api_secret,
@@ -185,7 +185,7 @@ public function get_sequence_subscriptions(int $sequence_id, string $sort_order
185185
)
186186
);
187187

188-
return $this->make_request($request, 'GET', $options);
188+
return $this->get($request, $options);
189189
}
190190

191191

@@ -199,7 +199,7 @@ public function get_sequence_subscriptions(int $sequence_id, string $sort_order
199199
*/
200200
public function add_subscriber_to_sequence(int $sequence_id, string $email)
201201
{
202-
$request = $this->api_version . sprintf('/courses/%s/subscribe', $sequence_id);
202+
$request = sprintf('courses/%s/subscribe', $sequence_id);
203203

204204
$options = [
205205
'api_key' => $this->api_key,
@@ -216,7 +216,7 @@ public function add_subscriber_to_sequence(int $sequence_id, string $email)
216216
)
217217
);
218218

219-
return $this->make_request($request, 'POST', $options);
219+
return $this->post($request, $options);
220220
}
221221

222222

@@ -236,13 +236,13 @@ public function add_tag(int $tag, array $options)
236236
throw new \InvalidArgumentException();
237237
}
238238

239-
$request = $this->api_version . sprintf('/tags/%s/subscribe', $tag);
239+
$request = sprintf('tags/%s/subscribe', $tag);
240240

241241
$options['api_key'] = $this->api_key;
242242

243243
$this->create_log(sprintf('POST add tag: %s, %s, %s', $request, json_encode($options), $tag));
244244

245-
return $this->make_request($request, 'POST', $options);
245+
return $this->post($request, $options);
246246
}
247247

248248

@@ -271,11 +271,17 @@ public function get_resources(string $resource)
271271
'Accept-Encoding' => 'gzip',
272272
];
273273

274-
$request = sprintf('/%s/%s', $this->api_version, (($resource === 'landing_pages') ? 'forms' : $resource));
274+
// Assign the resource to the request variable.
275+
$request = $resource;
276+
277+
// Landing pages are included in the /forms endpoint.
278+
if ($resource === 'landing_pages') {
279+
$request = 'forms';
280+
}
275281

276282
$this->create_log(sprintf('GET request %s, %s', $request, json_encode($options)));
277283

278-
$resources = $this->make_request($request, 'GET', $options);
284+
$resources = $this->get($request, $options);
279285

280286
if (!$resources) {
281287
$this->create_log('No resources');
@@ -363,13 +369,13 @@ public function form_subscribe(int $form_id, array $options)
363369
throw new \InvalidArgumentException();
364370
}
365371

366-
$request = $this->api_version . sprintf('/forms/%s/subscribe', $form_id);
372+
$request = sprintf('forms/%s/subscribe', $form_id);
367373

368374
$options['api_key'] = $this->api_key;
369375

370376
$this->create_log(sprintf('POST form subscribe: %s, %s, %s', $request, json_encode($options), $form_id));
371377

372-
return $this->make_request($request, 'POST', $options);
378+
return $this->post($request, $options);
373379
}
374380

375381

@@ -388,13 +394,13 @@ public function form_unsubscribe(array $options)
388394
throw new \InvalidArgumentException();
389395
}
390396

391-
$request = $this->api_version . '/unsubscribe';
397+
$request = 'unsubscribe';
392398

393399
$options['api_secret'] = $this->api_secret;
394400

395401
$this->create_log(sprintf('PUT form unsubscribe: %s, %s', $request, json_encode($options)));
396402

397-
return $this->make_request($request, 'PUT', $options);
403+
return $this->put($request, $options);
398404
}
399405

400406

@@ -414,7 +420,7 @@ public function get_subscriber_id(string $email_address)
414420
throw new \InvalidArgumentException();
415421
}
416422

417-
$request = $this->api_version . '/subscribers';
423+
$request = 'subscribers';
418424

419425
$options = [
420426
'api_secret' => $this->api_secret,
@@ -431,7 +437,7 @@ public function get_subscriber_id(string $email_address)
431437
)
432438
);
433439

434-
$subscribers = $this->make_request($request, 'GET', $options);
440+
$subscribers = $this->get($request, $options);
435441

436442
if (!$subscribers) {
437443
$this->create_log('No subscribers');
@@ -463,15 +469,15 @@ public function get_subscriber(int $subscriber_id)
463469
throw new \InvalidArgumentException();
464470
}
465471

466-
$request = $this->api_version . sprintf('/subscribers/%s', $subscriber_id);
472+
$request = sprintf('subscribers/%s', $subscriber_id);
467473

468474
$options = [
469475
'api_secret' => $this->api_secret,
470476
];
471477

472478
$this->create_log(sprintf('GET subscriber tags: %s, %s, %s', $request, json_encode($options), $subscriber_id));
473479

474-
return $this->make_request($request, 'GET', $options);
480+
return $this->get($request, $options);
475481
}
476482

477483

@@ -490,15 +496,15 @@ public function get_subscriber_tags(int $subscriber_id)
490496
throw new \InvalidArgumentException();
491497
}
492498

493-
$request = $this->api_version . sprintf('/subscribers/%s/tags', $subscriber_id);
499+
$request = sprintf('subscribers/%s/tags', $subscriber_id);
494500

495501
$options = [
496502
'api_key' => $this->api_key,
497503
];
498504

499505
$this->create_log(sprintf('GET subscriber tags: %s, %s, %s', $request, json_encode($options), $subscriber_id));
500506

501-
return $this->make_request($request, 'GET', $options);
507+
return $this->get($request, $options);
502508
}
503509

504510

@@ -517,13 +523,13 @@ public function list_purchases(array $options)
517523
throw new \InvalidArgumentException();
518524
}
519525

520-
$request = $this->api_version . '/purchases';
526+
$request = 'purchases';
521527

522528
$options['api_secret'] = $this->api_secret;
523529

524530
$this->create_log(sprintf('GET list purchases: %s, %s', $request, json_encode($options)));
525531

526-
return $this->make_request($request, 'GET', $options);
532+
return $this->get($request, $options);
527533
}
528534

529535

@@ -542,13 +548,13 @@ public function create_purchase(array $options)
542548
throw new \InvalidArgumentException();
543549
}
544550

545-
$request = $this->api_version . '/purchases';
551+
$request = 'purchases';
546552

547553
$options['api_secret'] = $this->api_secret;
548554

549555
$this->create_log(sprintf('POST create purchase: %s, %s', $request, json_encode($options)));
550556

551-
return $this->make_request($request, 'POST', $options);
557+
return $this->post($request, $options);
552558
}
553559

554560

@@ -692,6 +698,65 @@ private function strip_html_head_body_tags(string $markup)
692698
return $markup;
693699
}
694700

701+
/**
702+
* Performs a GET request to the API.
703+
*
704+
* @param string $endpoint API Endpoint.
705+
* @param array $args Request arguments.
706+
*
707+
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
708+
*
709+
* @return false|mixed
710+
*/
711+
public function get(string $endpoint, array $args = [])
712+
{
713+
return $this->make_request($endpoint, 'GET', $args);
714+
}
715+
716+
/**
717+
* Performs a POST request to the API.
718+
*
719+
* @param string $endpoint API Endpoint.
720+
* @param array $args Request arguments.
721+
*
722+
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
723+
*
724+
* @return false|mixed
725+
*/
726+
public function post(string $endpoint, array $args = [])
727+
{
728+
return $this->make_request($endpoint, 'POST', $args);
729+
}
730+
731+
/**
732+
* Performs a PUT request to the API.
733+
*
734+
* @param string $endpoint API Endpoint.
735+
* @param array $args Request arguments.
736+
*
737+
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
738+
*
739+
* @return false|mixed
740+
*/
741+
public function put(string $endpoint, array $args = [])
742+
{
743+
return $this->make_request($endpoint, 'PUT', $args);
744+
}
745+
746+
/**
747+
* Performs a DELETE request to the API.
748+
*
749+
* @param string $endpoint API Endpoint.
750+
* @param array $args Request arguments.
751+
*
752+
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
753+
*
754+
* @return false|mixed
755+
*/
756+
public function delete(string $endpoint, array $args = [])
757+
{
758+
return $this->make_request($endpoint, 'DELETE', $args);
759+
}
695760

696761
/**
697762
* Performs an API request using Guzzle.
@@ -710,7 +775,7 @@ public function make_request(string $endpoint, string $method, array $args = [])
710775
throw new \InvalidArgumentException();
711776
}
712777

713-
$url = $this->api_url_base . $endpoint;
778+
$url = $this->api_url_base . $this->api_version . '/' . $endpoint;
714779

715780
$this->create_log(sprintf('Making request on %s.', $url));
716781

0 commit comments

Comments
 (0)