Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 92 additions & 27 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ private function create_log(string $message)
*/
public function get_account()
{
$request = $this->api_version . '/account';
$request = 'account';

$options = [
'api_secret' => $this->api_secret,
];

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

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


Expand All @@ -147,15 +147,15 @@ public function get_account()
*/
public function get_sequences()
{
$request = $this->api_version . '/sequences';
$request = 'sequences';

$options = [
'api_key' => $this->api_key,
];

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

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


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

$options = [
'api_secret' => $this->api_secret,
Expand All @@ -185,7 +185,7 @@ public function get_sequence_subscriptions(int $sequence_id, string $sort_order
)
);

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


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

$options = [
'api_key' => $this->api_key,
Expand All @@ -216,7 +216,7 @@ public function add_subscriber_to_sequence(int $sequence_id, string $email)
)
);

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


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

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

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

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

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


Expand Down Expand Up @@ -271,11 +271,17 @@ public function get_resources(string $resource)
'Accept-Encoding' => 'gzip',
];

$request = sprintf('/%s/%s', $this->api_version, (($resource === 'landing_pages') ? 'forms' : $resource));
// Assign the resource to the request variable.
$request = $resource;

// Landing pages are included in the /forms endpoint.
if ($resource === 'landing_pages') {
$request = 'forms';
}

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

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

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

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

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

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

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


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

$request = $this->api_version . '/unsubscribe';
$request = 'unsubscribe';

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

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

return $this->make_request($request, 'PUT', $options);
return $this->put($request, $options);
}


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

$request = $this->api_version . '/subscribers';
$request = 'subscribers';

$options = [
'api_secret' => $this->api_secret,
Expand All @@ -431,7 +437,7 @@ public function get_subscriber_id(string $email_address)
)
);

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

if (!$subscribers) {
$this->create_log('No subscribers');
Expand Down Expand Up @@ -465,15 +471,15 @@ public function get_subscriber(int $subscriber_id)
throw new \InvalidArgumentException();
}

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

$options = [
'api_secret' => $this->api_secret,
];

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

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


Expand All @@ -492,15 +498,15 @@ public function get_subscriber_tags(int $subscriber_id)
throw new \InvalidArgumentException();
}

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

$options = [
'api_key' => $this->api_key,
];

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

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


Expand All @@ -519,13 +525,13 @@ public function list_purchases(array $options)
throw new \InvalidArgumentException();
}

$request = $this->api_version . '/purchases';
$request = 'purchases';

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

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

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


Expand All @@ -544,13 +550,13 @@ public function create_purchase(array $options)
throw new \InvalidArgumentException();
}

$request = $this->api_version . '/purchases';
$request = 'purchases';

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

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

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


Expand Down Expand Up @@ -694,6 +700,65 @@ private function strip_html_head_body_tags(string $markup)
return $markup;
}

/**
* Performs a GET request to the API.
*
* @param string $endpoint API Endpoint.
* @param array $args Request arguments.
*
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
*
* @return false|mixed
*/
public function get(string $endpoint, array $args = [])
{
return $this->make_request($endpoint, 'GET', $args);
}

/**
* Performs a POST request to the API.
*
* @param string $endpoint API Endpoint.
* @param array $args Request arguments.
*
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
*
* @return false|mixed
*/
public function post(string $endpoint, array $args = [])
{
return $this->make_request($endpoint, 'POST', $args);
}

/**
* Performs a PUT request to the API.
*
* @param string $endpoint API Endpoint.
* @param array $args Request arguments.
*
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
*
* @return false|mixed
*/
public function put(string $endpoint, array $args = [])
{
return $this->make_request($endpoint, 'PUT', $args);
}

/**
* Performs a DELETE request to the API.
*
* @param string $endpoint API Endpoint.
* @param array $args Request arguments.
*
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
*
* @return false|mixed
*/
public function delete(string $endpoint, array $args = [])
{
return $this->make_request($endpoint, 'DELETE', $args);
}

/**
* Performs an API request using Guzzle.
Expand All @@ -712,7 +777,7 @@ public function make_request(string $endpoint, string $method, array $args = [])
throw new \InvalidArgumentException();
}

$url = $this->api_url_base . $endpoint;
$url = $this->api_url_base . $this->api_version . '/' . $endpoint;

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

Expand Down