Skip to content

Commit b07a92f

Browse files
authored
Merge pull request #78 from ConvertKit/v4-api-get-response-interface
v4 API: Expose `Response` interface
2 parents 37a29f5 + fc64de9 commit b07a92f

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,26 @@ $api = new \ConvertKit_API\ConvertKit_API(
114114
);
115115
```
116116

117+
API requests may then be performed:
118+
119+
```php
120+
$result = $api->add_subscriber_to_form(12345, '[email protected]');
121+
```
122+
123+
To determine whether a new entity / relationship was created, or an existing entity / relationship updated, inspect the HTTP code of the last request:
124+
125+
```php
126+
$result = $api->add_subscriber_to_form(12345, '[email protected]');
127+
$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.
128+
```
129+
130+
The PSR-7 response can be fetched and further inspected, if required - for example, to check if a header exists:
131+
132+
```php
133+
$result = $api->add_subscriber_to_form(12345, '[email protected]');
134+
$api->getResponseInterface()->hasHeader('Content-Length'); // Check if the last API request included a `Content-Length` header
135+
```
136+
117137
### 1.x (v3 API, API Key and Secret, PHP 7.4+)
118138

119139
Get your ConvertKit API Key and API Secret [here](https://app.convertkit.com/account/edit) and set it somewhere in your application.

src/ConvertKit_API.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ class ConvertKit_API
9595
*/
9696
protected $client;
9797

98+
/**
99+
* Guzzle Http Response
100+
*
101+
* @var \Psr\Http\Message\ResponseInterface
102+
*/
103+
protected $response;
104+
98105

99106
/**
100107
* Constructor for ConvertKitAPI instance
@@ -1750,21 +1757,32 @@ public function make_request(string $endpoint, string $method, array $args = [])
17501757
}
17511758

17521759
// Send request.
1753-
$response = $this->client->send(
1760+
$this->response = $this->client->send(
17541761
$request,
17551762
['exceptions' => false]
17561763
);
17571764

1758-
// Inspect response.
1759-
$status_code = $response->getStatusCode();
1760-
$response_body = $response->getBody()->getContents();
1765+
// Get response.
1766+
$response_body = $this->response->getBody()->getContents();
17611767

17621768
// Log response.
1763-
$this->create_log(sprintf('Response Status Code: %s', $response->getStatusCode()));
1764-
$this->create_log(sprintf('Response Body: %s', $response->getBody()->getContents()));
1769+
$this->create_log(sprintf('Response Status Code: %s', $this->response->getStatusCode()));
1770+
$this->create_log(sprintf('Response Body: %s', $response_body));
17651771
$this->create_log('Finish request successfully');
17661772

17671773
// Return response.
17681774
return json_decode($response_body);
17691775
}
1776+
1777+
/**
1778+
* Returns the response interface used for the last API request.
1779+
*
1780+
* @since 2.0.0
1781+
*
1782+
* @return \Psr\Http\Message\ResponseInterface
1783+
*/
1784+
public function getResponseInterface()
1785+
{
1786+
return $this->response;
1787+
}
17701788
}

tests/ConvertKitAPITest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ protected function setUp(): void
6161
);
6262
}
6363

64+
/**
65+
* Test that a Response instance is returned when calling getResponseInterface()
66+
* after making an API request.
67+
*
68+
* @since 2.0.0
69+
*
70+
* @return void
71+
*/
72+
public function testGetResponseInterface()
73+
{
74+
// Assert response interface is null, as no API request made.
75+
$this->assertNull($this->api->getResponseInterface());
76+
77+
// Perform an API request.
78+
$result = $this->api->get_account();
79+
80+
// Assert response interface is of a valid type.
81+
$this->assertInstanceOf(Response::class, $this->api->getResponseInterface());
82+
83+
// Assert the correct status code was returned.
84+
$this->assertEquals(200, $this->api->getResponseInterface()->getStatusCode());
85+
}
86+
6487
/**
6588
* Test that a ClientInterface can be injected.
6689
*
@@ -95,6 +118,12 @@ public function testClientInterfaceInjection()
95118
$this->assertSame('Test Account for Guzzle Mock', $result->name);
96119
$this->assertSame('free', $result->plan_type);
97120
$this->assertSame('[email protected]', $result->primary_email_address);
121+
122+
// Assert response interface is of a valid type when using `set_http_client`.
123+
$this->assertInstanceOf(Response::class, $this->api->getResponseInterface());
124+
125+
// Assert the correct status code was returned.
126+
$this->assertEquals(200, $this->api->getResponseInterface()->getStatusCode());
98127
}
99128

100129
/**

0 commit comments

Comments
 (0)