Skip to content

v4 API: Expose Response interface #78

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 4 commits into from
Mar 25, 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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ $api = new \ConvertKit_API\ConvertKit_API(
);
```

API requests may then be performed:

```php
$result = $api->add_subscriber_to_form(12345, '[email protected]');
```

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, '[email protected]');
$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, '[email protected]');
$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.
Expand Down
30 changes: 24 additions & 6 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ class ConvertKit_API
*/
protected $client;

/**
* Guzzle Http Response
*
* @var \Psr\Http\Message\ResponseInterface
*/
protected $response;


/**
* Constructor for ConvertKitAPI instance
Expand Down Expand Up @@ -1750,21 +1757,32 @@ 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();
// Get response.
$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', $response_body));
$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 getResponseInterface()
{
return $this->response;
}
}
29 changes: 29 additions & 0 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -95,6 +118,12 @@ public function testClientInterfaceInjection()
$this->assertSame('Test Account for Guzzle Mock', $result->name);
$this->assertSame('free', $result->plan_type);
$this->assertSame('[email protected]', $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());
}

/**
Expand Down