Skip to content

v4 API: Get HTML Resources #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
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
84 changes: 62 additions & 22 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,8 @@ public function __construct(
$this->access_token = $accessToken;
$this->debug = $debug;

// Set headers.
$headers = [
'Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8',
'User-Agent' => 'ConvertKitPHPSDK/' . self::VERSION . ';PHP/' . phpversion(),
];
if (!empty($this->access_token)) {
$headers['Authorization'] = 'Bearer ' . $this->access_token;
}

// Set the Guzzle client.
$this->client = new Client(
['headers' => $headers]
);
$this->client = new Client();

if ($debug) {
// If no debug log file location specified, define a default.
Expand Down Expand Up @@ -243,6 +231,9 @@ public function get_access_token(string $authCode, string $redirectURI)
$request = new Request(
method: 'POST',
uri: $this->oauth_token_url,
headers: $this->request_headers(
auth: false
),
body: (string) json_encode(
[
'code' => $authCode,
Expand Down Expand Up @@ -278,6 +269,9 @@ public function refresh_token(string $refreshToken, string $redirectURI)
$request = new Request(
method: 'POST',
uri: $this->oauth_token_url,
headers: $this->request_headers(
auth: false
),
body: (string) json_encode(
[
'refresh_token' => $refreshToken,
Expand Down Expand Up @@ -1851,8 +1845,9 @@ public function get_segments(
* Get markup from ConvertKit for the provided $url.
*
* Supports legacy forms and legacy landing pages.
*
* Forms and Landing Pages should be embedded using the supplied JS embed script in
* the API response when using get_resources().
* the API response when using get_forms() or get_landing_pages().
*
* @param string $url URL of HTML page.
*
Expand All @@ -1873,9 +1868,12 @@ public function get_resource(string $url)

// Fetch the resource.
$request = new Request(
'GET',
$url,
['Accept-Encoding' => 'gzip']
method: 'GET',
uri: $url,
headers: $this->request_headers(
type: 'text/html',
auth: false
),
);
$response = $this->client->send($request);

Expand Down Expand Up @@ -2100,18 +2098,20 @@ public function make_request(string $endpoint, string $method, array $args = [])

$request = new Request(
method: $method,
uri: $url
uri: $url,
headers: $this->request_headers(),
);
break;

default:
$request = new Request(
method: $method,
uri: $url,
body: (string) json_encode($args),
method: $method,
uri: $url,
headers: $this->request_headers(),
body: (string) json_encode($args),
);
break;
}
}//end switch

// Send request.
$this->response = $this->client->send(
Expand Down Expand Up @@ -2142,4 +2142,44 @@ public function getResponseInterface()
{
return $this->response;
}

/**
* Returns the headers to use in an API request.
*
* @param string $type Accept and Content-Type Headers.
* @param boolean $auth Include authorization header.
*
* @since 2.0.0
*
* @return array<string,string>
*/
private function request_headers(string $type = 'application/json', bool $auth = true)
{
$headers = [
'Accept' => $type,
'Content-Type' => $type . '; charset=utf-8',
'User-Agent' => $this->user_agent(),
];

// If no authorization header required, return now.
if (!$auth) {
return $headers;
}

// Add authorization header and return.
$headers['Authorization'] = 'Bearer ' . $this->access_token;
return $headers;
}

/**
* Returns the user agent string to use in all HTTP requests.
*
* @since 2.0.0
*
* @return string
*/
private function user_agent()
{
return 'ConvertKitPHPSDK/' . self::VERSION . ';PHP/' . phpversion();
}
}
98 changes: 88 additions & 10 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,94 @@ public function testDebugDisabled()
$this->assertEmpty($this->getLogFileContents());
}

/**
* Test that calling request_headers() returns the expected array of headers
*
* @since 2.0.0
*
* @return void
*/
public function testRequestHeadersMethod()
{
$headers = $this->callPrivateMethod($this->api, 'request_headers', []);
$this->assertArrayHasKey('Accept', $headers);
$this->assertArrayHasKey('Content-Type', $headers);
$this->assertArrayHasKey('User-Agent', $headers);
$this->assertArrayHasKey('Authorization', $headers);
$this->assertEquals($headers['Accept'], 'application/json');
$this->assertEquals($headers['Content-Type'], 'application/json; charset=utf-8');
$this->assertEquals($headers['User-Agent'], 'ConvertKitPHPSDK/' . $this->api::VERSION . ';PHP/' . phpversion());
$this->assertEquals($headers['Authorization'], 'Bearer ' . $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN']);
}

/**
* Test that calling request_headers() with a different `type` parameter
* returns the expected array of headers
*
* @since 2.0.0
*
* @return void
*/
public function testRequestHeadersMethodWithType()
{
$headers = $this->callPrivateMethod($this->api, 'request_headers', [
'type' => 'text/html',
]);
$this->assertArrayHasKey('Accept', $headers);
$this->assertArrayHasKey('Content-Type', $headers);
$this->assertArrayHasKey('User-Agent', $headers);
$this->assertArrayHasKey('Authorization', $headers);
$this->assertEquals($headers['Accept'], 'text/html');
$this->assertEquals($headers['Content-Type'], 'text/html; charset=utf-8');
$this->assertEquals($headers['User-Agent'], 'ConvertKitPHPSDK/' . $this->api::VERSION . ';PHP/' . phpversion());
$this->assertEquals($headers['Authorization'], 'Bearer ' . $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN']);
}

/**
* Test that calling request_headers() with the `auth` parameter set to false
* returns the expected array of headers
*
* @since 2.0.0
*
* @return void
*/
public function testRequestHeadersMethodWithAuthDisabled()
{
$headers = $this->callPrivateMethod($this->api, 'request_headers', [
'auth' => false,
]);
$this->assertArrayHasKey('Accept', $headers);
$this->assertArrayHasKey('Content-Type', $headers);
$this->assertArrayHasKey('User-Agent', $headers);
$this->assertArrayNotHasKey('Authorization', $headers);
$this->assertEquals($headers['Accept'], 'application/json');
$this->assertEquals($headers['Content-Type'], 'application/json; charset=utf-8');
$this->assertEquals($headers['User-Agent'], 'ConvertKitPHPSDK/' . $this->api::VERSION . ';PHP/' . phpversion());
}

/**
* Test that calling request_headers() with a different `type` parameter
* and the `auth` parameter set to false returns the expected array of headers
*
* @since 2.0.0
*
* @return void
*/
public function testRequestHeadersMethodWithTypeAndAuthDisabled()
{
$headers = $this->callPrivateMethod($this->api, 'request_headers', [
'type' => 'text/html',
'auth' => false,
]);
$this->assertArrayHasKey('Accept', $headers);
$this->assertArrayHasKey('Content-Type', $headers);
$this->assertArrayHasKey('User-Agent', $headers);
$this->assertArrayNotHasKey('Authorization', $headers);
$this->assertEquals($headers['Accept'], 'text/html');
$this->assertEquals($headers['Content-Type'], 'text/html; charset=utf-8');
$this->assertEquals($headers['User-Agent'], 'ConvertKitPHPSDK/' . $this->api::VERSION . ';PHP/' . phpversion());
}

/**
* Test that get_oauth_url() returns the correct URL to begin the OAuth process.
*
Expand Down Expand Up @@ -4867,8 +4955,6 @@ public function testGetSegmentsPagination()
*/
public function testGetResourceLegacyForm()
{
$this->markTestIncomplete();

$markup = $this->api->get_resource($_ENV['CONVERTKIT_API_LEGACY_FORM_URL']);

// Assert that the markup is HTML.
Expand All @@ -4887,8 +4973,6 @@ public function testGetResourceLegacyForm()
*/
public function testGetResourceLandingPage()
{
$this->markTestIncomplete();

$markup = $this->api->get_resource($_ENV['CONVERTKIT_API_LANDING_PAGE_URL']);

// Assert that the markup is HTML.
Expand All @@ -4907,8 +4991,6 @@ public function testGetResourceLandingPage()
*/
public function testGetResourceLegacyLandingPage()
{
$this->markTestIncomplete();

$markup = $this->api->get_resource($_ENV['CONVERTKIT_API_LEGACY_LANDING_PAGE_URL']);

// Assert that the markup is HTML.
Expand All @@ -4928,8 +5010,6 @@ public function testGetResourceLegacyLandingPage()
*/
public function testGetResourceInvalidURL()
{
$this->markTestIncomplete();

$this->expectException(InvalidArgumentException::class);
$markup = $this->api->get_resource('not-a-url');
}
Expand All @@ -4944,8 +5024,6 @@ public function testGetResourceInvalidURL()
*/
public function testGetResourceInaccessibleURL()
{
$this->markTestIncomplete();

$this->expectException(ClientException::class);
$markup = $this->api->get_resource('https://convertkit.com/a/url/that/does/not/exist');
}
Expand Down
Loading