Skip to content

Commit

Permalink
Return response data in Ping endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AlfredoRamos committed Jun 15, 2022
1 parent 216a3ae commit 64c92b5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/Api/Ping.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ class Ping extends AbstractApi {
* @return array Response data.
*/
public function info() {
return $this->request->get('ping');
$response = $this->request->raw('ping');
$statusCode = $response->getStatusCode();
$statusMessage = '';

switch ($statusCode) {
case 204:
$statusMessage = 'API key is valid.';
break;

case 401:
$statusMessage = 'The API key wasn\'t sent or is invalid.';
break;

case 404:
$statusMessage = 'Account not found.';
break;

case 500:
$statusMessage = 'An internal error happened. Try again later.';
break;

default:
$statusMessage = 'Unknown error.';
break;
}

$body = ['status' => $statusCode, 'message' => $statusMessage];

return $body;
}
}
13 changes: 13 additions & 0 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ public function delete(string $url = '', array $parameters = []) {
return $this->sendRequest($url, $parameters, 'DELETE');
}

/**
* {@inheritDoc}
*/
public function raw(string $url = '', array $parameters = [], string $method = 'GET') {
$method = trim(strtoupper($method));

if (empty($method) || !in_array($method, HttpRequestInterface::ALLOWED_METHODS, true)) {
return;
}

return $this->sendRequest($url, $parameters, $method);
}

/**
* {@inheritDoc}
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,17 @@ public function delete(string $url = '', array $parameters = []) {

return $this->httpClient->parseResponse($response);
}

/**
* {@inheritDoc}
*/
public function raw(string $url = '', array $parameters = [], string $method = 'GET') {
$method = trim(strtoupper($method));

if (empty($method) || !in_array($method, RequestInterface::ALLOWED_METHODS, true)) {
return;
}

return $this->httpClient->sendRequest($url, $parameters, $method);
}
}
13 changes: 13 additions & 0 deletions src/Http/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
namespace AlfredoRamos\Mailrelay\Http;

interface RequestInterface {
/** @var array */
const ALLOWED_METHODS = ['GET', 'POST', 'PATCH', 'DELETE'];

/**
* Send HTTP GET request.
*
Expand Down Expand Up @@ -50,4 +53,14 @@ public function patch(string $url = '', array $parameters = []);
* @return \Psr7\Http\Message\RequestInterface The HTTP request.
*/
public function delete(string $url = '', array $parameters = []);

/**
* Send dynamic HTTP raw request.
* @param string $url The URL to send the request to.
* @param array $parameters The request parameters.
* @param string $method The HTTP method (GET, POST, PATCH, DELETE).
*
* @return null|\Psr7\Http\Message\RequestInterface The HTTP request.
*/
public function raw(string $url = '', array $parameters = [], string $method = 'GET');
}
19 changes: 18 additions & 1 deletion tests/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;

class ClientTest extends TestCase {
Expand Down Expand Up @@ -128,4 +127,22 @@ public function testDeleteRequest() {

$this->assertSame(204, $response->getStatusCode());
}

public function testRawRequest() {
$options = [
'api_account' => 'test_account',
'api_token' => 'invalid_token'
];

$mock = new MockHandler([
new Response(204)
]);

$handler = HandlerStack::create($mock);
$client = new GuzzleClient(['handler' => $handler]);
$httpClient = new HttpClient($options, $client);
$response = $httpClient->raw('/path', ['param' => 'value'], 'GET');

$this->assertSame(204, $response->getStatusCode());
}
}

0 comments on commit 64c92b5

Please sign in to comment.