diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 5b43cbe..6c5e6eb 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -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. @@ -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, @@ -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, @@ -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. * @@ -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); @@ -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( @@ -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 + */ + 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(); + } } diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index c123fd9..2d95c73 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -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. * @@ -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. @@ -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. @@ -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. @@ -4928,8 +5010,6 @@ public function testGetResourceLegacyLandingPage() */ public function testGetResourceInvalidURL() { - $this->markTestIncomplete(); - $this->expectException(InvalidArgumentException::class); $markup = $this->api->get_resource('not-a-url'); } @@ -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'); }