Skip to content

Commit ba62560

Browse files
committed
Remove status_code, as response object includes it
1 parent b0588ce commit ba62560

File tree

3 files changed

+29
-45
lines changed

3 files changed

+29
-45
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: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,6 @@ class ConvertKit_API
102102
*/
103103
protected $response;
104104

105-
/**
106-
* Guzzle Http Status Code
107-
*
108-
* @var integer
109-
*/
110-
protected $status_code = 0;
111-
112105

113106
/**
114107
* Constructor for ConvertKitAPI instance
@@ -1655,13 +1648,12 @@ public function make_request(string $endpoint, string $method, array $args = [])
16551648
['exceptions' => false]
16561649
);
16571650

1658-
// Inspect response.
1659-
$this->status_code = $this->response->getStatusCode();
1660-
$response_body = $this->response->getBody()->getContents();
1651+
// Get response.
1652+
$response_body = $this->response->getBody()->getContents();
16611653

16621654
// Log response.
16631655
$this->create_log(sprintf('Response Status Code: %s', $this->response->getStatusCode()));
1664-
$this->create_log(sprintf('Response Body: %s', $this->response->getBody()->getContents()));
1656+
$this->create_log(sprintf('Response Body: %s', $response_body));
16651657
$this->create_log('Finish request successfully');
16661658

16671659
// Return response.
@@ -1675,20 +1667,8 @@ public function make_request(string $endpoint, string $method, array $args = [])
16751667
*
16761668
* @return \Psr\Http\Message\ResponseInterface
16771669
*/
1678-
public function get_response_interface()
1670+
public function getResponseInterface()
16791671
{
16801672
return $this->response;
16811673
}
1682-
1683-
/**
1684-
* Returns the HTTP status code for the last successful API request.
1685-
*
1686-
* @since 2.0.0
1687-
*
1688-
* @return integer
1689-
*/
1690-
public function get_response_status_code()
1691-
{
1692-
return $this->status_code;
1693-
}
16941674
}

tests/ConvertKitAPITest.php

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function testDebugDisabled()
224224
}
225225

226226
/**
227-
* Test that a Response instance is returned when calling get_response_interface()
227+
* Test that a Response instance is returned when calling getResponseInterface()
228228
* after making an API request.
229229
*
230230
* @since 2.0.0
@@ -234,32 +234,16 @@ public function testDebugDisabled()
234234
public function testGetResponseInterface()
235235
{
236236
// Assert response interface is null, as no API request made.
237-
$this->assertNull($this->api->get_response_interface());
237+
$this->assertNull($this->api->getResponseInterface());
238238

239239
// Perform an API request.
240240
$result = $this->api->get_account();
241241

242242
// Assert response interface is of a valid type.
243-
$this->assertInstanceOf(Response::class, $this->api->get_response_interface());
244-
}
245-
246-
/**
247-
* Test that the get_response_status_code() returns the expected HTTP status code.
248-
*
249-
* @since 2.0.0
250-
*
251-
* @return void
252-
*/
253-
public function testGetResponseStatusCode()
254-
{
255-
// Assert no status code has yet been set in get_response_status_code, as no API request made.
256-
$this->assertEquals(0, $this->api->get_response_status_code());
257-
258-
// Perform an API request.
259-
$result = $this->api->get_account();
243+
$this->assertInstanceOf(Response::class, $this->api->getResponseInterface());
260244

261-
// Assert the correct status code defined in get_response_status_code()
262-
$this->assertEquals(200, $this->api->get_response_status_code());
245+
// Assert the correct status code was returned.
246+
$this->assertEquals(200, $this->api->getResponseInterface()->getStatusCode());
263247
}
264248

265249
/**

0 commit comments

Comments
 (0)