From b0588cef94346211246845b9bda35787d71e058b Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Thu, 21 Mar 2024 15:40:26 +0000 Subject: [PATCH 1/3] Add `get_response_interface` and `get_response_status_code` helper methods --- src/ConvertKit_API.php | 48 +++++++++++++++++++++++++++++++++---- tests/ConvertKitAPITest.php | 39 ++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 3075af4..f3f215f 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -95,6 +95,20 @@ class ConvertKit_API */ protected $client; + /** + * Guzzle Http Response + * + * @var \Psr\Http\Message\ResponseInterface + */ + protected $response; + + /** + * Guzzle Http Status Code + * + * @var integer + */ + protected $status_code = 0; + /** * Constructor for ConvertKitAPI instance @@ -1636,21 +1650,45 @@ public function make_request(string $endpoint, string $method, array $args = []) } // Send request. - $response = $this->client->send( + $this->response = $this->client->send( $request, ['exceptions' => false] ); // Inspect response. - $status_code = $response->getStatusCode(); - $response_body = $response->getBody()->getContents(); + $this->status_code = $this->response->getStatusCode(); + $response_body = $this->response->getBody()->getContents(); // Log response. - $this->create_log(sprintf('Response Status Code: %s', $response->getStatusCode())); - $this->create_log(sprintf('Response Body: %s', $response->getBody()->getContents())); + $this->create_log(sprintf('Response Status Code: %s', $this->response->getStatusCode())); + $this->create_log(sprintf('Response Body: %s', $this->response->getBody()->getContents())); $this->create_log('Finish request successfully'); // Return response. return json_decode($response_body); } + + /** + * Returns the response interface used for the last API request. + * + * @since 2.0.0 + * + * @return \Psr\Http\Message\ResponseInterface + */ + public function get_response_interface() + { + return $this->response; + } + + /** + * Returns the HTTP status code for the last successful API request. + * + * @since 2.0.0 + * + * @return integer + */ + public function get_response_status_code() + { + return $this->status_code; + } } diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 9391fcd..fab0e7b 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -223,6 +223,45 @@ public function testDebugDisabled() $this->assertEmpty($this->getLogFileContents()); } + /** + * Test that a Response instance is returned when calling get_response_interface() + * after making an API request. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetResponseInterface() + { + // Assert response interface is null, as no API request made. + $this->assertNull($this->api->get_response_interface()); + + // Perform an API request. + $result = $this->api->get_account(); + + // Assert response interface is of a valid type. + $this->assertInstanceOf(Response::class, $this->api->get_response_interface()); + } + + /** + * Test that the get_response_status_code() returns the expected HTTP status code. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetResponseStatusCode() + { + // Assert no status code has yet been set in get_response_status_code, as no API request made. + $this->assertEquals(0, $this->api->get_response_status_code()); + + // Perform an API request. + $result = $this->api->get_account(); + + // Assert the correct status code defined in get_response_status_code() + $this->assertEquals(200, $this->api->get_response_status_code()); + } + /** * Test that get_oauth_url() returns the correct URL to begin the OAuth process. * From ba62560684d5e5e45364b0b4392b578dc33e3c11 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Thu, 21 Mar 2024 16:01:58 +0000 Subject: [PATCH 2/3] Remove `status_code`, as response object includes it --- README.md | 20 ++++++++++++++++++++ src/ConvertKit_API.php | 28 ++++------------------------ tests/ConvertKitAPITest.php | 26 +++++--------------------- 3 files changed, 29 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 7b2e6ae..4579b3d 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,26 @@ $api = new \ConvertKit_API\ConvertKit_API( ); ``` +API requests may then be performed: + +```php +$result = $api->add_subscriber_to_form(12345, 'joe.bloggs@convertkit.com'); +``` + +To determine whether a new entity / relationship was created, or an existing entity / relationship updated, inspect the HTTP code of the last request: + +```php +$result = $api->add_subscriber_to_form(12345, 'joe.bloggs@convertkit.com'); +$code = $api->getResponseInterface()->getStatusCode(); // 200 OK if e.g. a subscriber already added to the specified form, 201 Created if the subscriber added to the specified form for the first time. +``` + +The PSR-7 response can be fetched and further inspected, if required - for example, to check if a header exists: + +```php +$result = $api->add_subscriber_to_form(12345, 'joe.bloggs@convertkit.com'); +$api->getResponseInterface()->hasHeader('Content-Length'); // Check if the last API request included a `Content-Length` header +``` + ### 1.x (v3 API, API Key and Secret, PHP 7.4+) Get your ConvertKit API Key and API Secret [here](https://app.convertkit.com/account/edit) and set it somewhere in your application. diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index f3f215f..ef08595 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -102,13 +102,6 @@ class ConvertKit_API */ protected $response; - /** - * Guzzle Http Status Code - * - * @var integer - */ - protected $status_code = 0; - /** * Constructor for ConvertKitAPI instance @@ -1655,13 +1648,12 @@ public function make_request(string $endpoint, string $method, array $args = []) ['exceptions' => false] ); - // Inspect response. - $this->status_code = $this->response->getStatusCode(); - $response_body = $this->response->getBody()->getContents(); + // Get response. + $response_body = $this->response->getBody()->getContents(); // Log response. $this->create_log(sprintf('Response Status Code: %s', $this->response->getStatusCode())); - $this->create_log(sprintf('Response Body: %s', $this->response->getBody()->getContents())); + $this->create_log(sprintf('Response Body: %s', $response_body)); $this->create_log('Finish request successfully'); // Return response. @@ -1675,20 +1667,8 @@ public function make_request(string $endpoint, string $method, array $args = []) * * @return \Psr\Http\Message\ResponseInterface */ - public function get_response_interface() + public function getResponseInterface() { return $this->response; } - - /** - * Returns the HTTP status code for the last successful API request. - * - * @since 2.0.0 - * - * @return integer - */ - public function get_response_status_code() - { - return $this->status_code; - } } diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index fab0e7b..083aa36 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -224,7 +224,7 @@ public function testDebugDisabled() } /** - * Test that a Response instance is returned when calling get_response_interface() + * Test that a Response instance is returned when calling getResponseInterface() * after making an API request. * * @since 2.0.0 @@ -234,32 +234,16 @@ public function testDebugDisabled() public function testGetResponseInterface() { // Assert response interface is null, as no API request made. - $this->assertNull($this->api->get_response_interface()); + $this->assertNull($this->api->getResponseInterface()); // Perform an API request. $result = $this->api->get_account(); // Assert response interface is of a valid type. - $this->assertInstanceOf(Response::class, $this->api->get_response_interface()); - } - - /** - * Test that the get_response_status_code() returns the expected HTTP status code. - * - * @since 2.0.0 - * - * @return void - */ - public function testGetResponseStatusCode() - { - // Assert no status code has yet been set in get_response_status_code, as no API request made. - $this->assertEquals(0, $this->api->get_response_status_code()); - - // Perform an API request. - $result = $this->api->get_account(); + $this->assertInstanceOf(Response::class, $this->api->getResponseInterface()); - // Assert the correct status code defined in get_response_status_code() - $this->assertEquals(200, $this->api->get_response_status_code()); + // Assert the correct status code was returned. + $this->assertEquals(200, $this->api->getResponseInterface()->getStatusCode()); } /** From f116beb4de0494947f5a8cd5234a7b47e636abaa Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Thu, 21 Mar 2024 16:08:49 +0000 Subject: [PATCH 3/3] Moved test --- tests/ConvertKitAPITest.php | 52 +++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 083aa36..4cf6f6b 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -61,6 +61,29 @@ protected function setUp(): void ); } + /** + * Test that a Response instance is returned when calling getResponseInterface() + * after making an API request. + * + * @since 2.0.0 + * + * @return void + */ + public function testGetResponseInterface() + { + // Assert response interface is null, as no API request made. + $this->assertNull($this->api->getResponseInterface()); + + // Perform an API request. + $result = $this->api->get_account(); + + // Assert response interface is of a valid type. + $this->assertInstanceOf(Response::class, $this->api->getResponseInterface()); + + // Assert the correct status code was returned. + $this->assertEquals(200, $this->api->getResponseInterface()->getStatusCode()); + } + /** * Test that a ClientInterface can be injected. * @@ -95,6 +118,12 @@ public function testClientInterfaceInjection() $this->assertSame('Test Account for Guzzle Mock', $result->name); $this->assertSame('free', $result->plan_type); $this->assertSame('mock@guzzle.mock', $result->primary_email_address); + + // Assert response interface is of a valid type when using `set_http_client`. + $this->assertInstanceOf(Response::class, $this->api->getResponseInterface()); + + // Assert the correct status code was returned. + $this->assertEquals(200, $this->api->getResponseInterface()->getStatusCode()); } /** @@ -223,29 +252,6 @@ public function testDebugDisabled() $this->assertEmpty($this->getLogFileContents()); } - /** - * Test that a Response instance is returned when calling getResponseInterface() - * after making an API request. - * - * @since 2.0.0 - * - * @return void - */ - public function testGetResponseInterface() - { - // Assert response interface is null, as no API request made. - $this->assertNull($this->api->getResponseInterface()); - - // Perform an API request. - $result = $this->api->get_account(); - - // Assert response interface is of a valid type. - $this->assertInstanceOf(Response::class, $this->api->getResponseInterface()); - - // Assert the correct status code was returned. - $this->assertEquals(200, $this->api->getResponseInterface()->getStatusCode()); - } - /** * Test that get_oauth_url() returns the correct URL to begin the OAuth process. *