Skip to content

Commit 8997b5f

Browse files
committed
added class
1 parent cc2fe62 commit 8997b5f

14 files changed

Lines changed: 684 additions & 10 deletions

Tests/GuzzleTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace Tests\Adapter;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use GuzzleHttp\Exception\RequestException;
7+
use Reactmore\SupportAdapter\Adapter\Guzzle;
8+
use Reactmore\SupportAdapter\Adapter\Auth\BearerToken;
9+
10+
class GuzzleTest extends TestCase
11+
{
12+
private $client;
13+
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
// Menggunakan BearerToken untuk autentikasi
19+
$auth = new BearerToken('test-token');
20+
$this->client = new Guzzle($auth, 'https://httpbin.org/');
21+
}
22+
23+
public function testGet()
24+
{
25+
$response = $this->client->get('https://httpbin.org/get', [], ['X-Testing' => 'Test']);
26+
$headers = $response->getHeaders();
27+
$this->assertEquals('application/json', $headers['Content-Type'][0]);
28+
29+
$body = json_decode($response->getBody());
30+
$this->assertEquals('Test', $body->headers->{'X-Testing'});
31+
$this->assertEquals('Bearer test-token', $body->headers->Authorization);
32+
33+
$response = $this->client->get('https://httpbin.org/get', [], ['X-Another-Test' => 'Test2']);
34+
$body = json_decode($response->getBody());
35+
$this->assertEquals('Test2', $body->headers->{'X-Another-Test'});
36+
}
37+
38+
public function testPost()
39+
{
40+
$response = $this->client->post('https://httpbin.org/post', ['X-Post-Test' => 'Testing a POST request.']);
41+
$headers = $response->getHeaders();
42+
$this->assertEquals('application/json', $headers['Content-Type'][0]);
43+
$body = json_decode($response->getBody()->getContents());
44+
$this->assertEquals('Testing a POST request.', $body->form->{'X-Post-Test'});
45+
}
46+
47+
public function testPut()
48+
{
49+
$response = $this->client->put('https://httpbin.org/put', ['X-Put-Test' => 'Testing a PUT request.']);
50+
$headers = $response->getHeaders();
51+
$this->assertEquals('application/json', $headers['Content-Type'][0]);
52+
53+
$body = json_decode($response->getBody()->getContents());
54+
$this->assertEquals('Testing a PUT request.', $body->form->{'X-Put-Test'});
55+
}
56+
57+
public function testPatch()
58+
{
59+
$response = $this->client->patch(
60+
'https://httpbin.org/patch',
61+
['X-Patch-Test' => 'Testing a PATCH request.']
62+
);
63+
64+
$headers = $response->getHeaders();
65+
$this->assertEquals('application/json', $headers['Content-Type'][0]);
66+
67+
$body = json_decode($response->getBody()->getContents());
68+
$this->assertEquals('Testing a PATCH request.', $body->form->{'X-Patch-Test'});
69+
}
70+
71+
public function testDelete()
72+
{
73+
$response = $this->client->delete(
74+
'https://httpbin.org/delete',
75+
['X-Delete-Test' => 'Testing a DELETE request.']
76+
);
77+
78+
$headers = $response->getHeaders();
79+
$this->assertEquals('application/json', $headers['Content-Type'][0]);
80+
81+
$body = json_decode($response->getBody()->getContents());
82+
$this->assertEquals('Testing a DELETE request.', $body->form->{'X-Delete-Test'});
83+
}
84+
85+
public function testNotFound()
86+
{
87+
$this->expectException(RequestException::class);
88+
$this->client->get('https://httpbin.org/status/404');
89+
}
90+
91+
public function testServerError()
92+
{
93+
$this->expectException(RequestException::class);
94+
$this->client->get('https://httpbin.org/status/500');
95+
}
96+
}

src/Adapter/AdapterInterface.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Reactmore\SupportAdapter\Adapter;
4+
5+
use Reactmore\SupportAdapter\Adapter\Auth\AuthInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
/**
9+
* The Adapter interface provides a common interface for all HTTP request adapters used in the Flip SDK.
10+
*/
11+
interface AdapterInterface
12+
{
13+
/**
14+
* Adapter constructor.
15+
*
16+
* @param Auth $auth The authentication credentials used for the request.
17+
* @param string $baseURI The base URI for the API endpoint.
18+
*/
19+
public function __construct(AuthInterface $auth, string $baseURI);
20+
21+
/**
22+
* Sends a GET request.
23+
*
24+
* Per the Robustness Principle, this method does not include the ability to send a body with a GET request (though
25+
* it is technically possible in the RFCs, it is never useful).
26+
*
27+
* @param string $uri The URI for the request.
28+
* @param array $data An array of data to send with the request.
29+
* @param array $headers An array of headers to send with the request.
30+
* @return ResponseInterface The response object from the API.
31+
*/
32+
public function get(string $uri, array $data = [], array $headers = []): ResponseInterface;
33+
34+
/**
35+
* Sends a POST request.
36+
*
37+
* @param string $uri The URI for the request.
38+
* @param array $data An array of data to send with the request.
39+
* @param array $headers An array of headers to send with the request.
40+
* @return ResponseInterface The response object from the API.
41+
*/
42+
public function post(string $uri, array $data = [], array $headers = []): ResponseInterface;
43+
44+
/**
45+
* Sends a PUT request.
46+
*
47+
* @param string $uri The URI for the request.
48+
* @param array $data An array of data to send with the request.
49+
* @param array $headers An array of headers to send with the request.
50+
* @return ResponseInterface The response object from the API.
51+
*/
52+
public function put(string $uri, array $data = [], array $headers = []): ResponseInterface;
53+
54+
/**
55+
* Sends a PATCH request.
56+
*
57+
* @param string $uri The URI for the request.
58+
* @param array $data An array of data to send with the request.
59+
* @param array $headers An array of headers to send with the request.
60+
* @return ResponseInterface The response object from the API.
61+
*/
62+
public function patch(string $uri, array $data = [], array $headers = []): ResponseInterface;
63+
64+
/**
65+
* Sends a DELETE request.
66+
*
67+
* @param string $uri The URI for the request.
68+
* @param array $data An array of data to send with the request.
69+
* @param array $headers An array of headers to send with the request.
70+
* @return ResponseInterface The response object from the API.
71+
*/
72+
public function delete(string $uri, array $data = [], array $headers = []): ResponseInterface;
73+
}

src/Adapter/Auth/AuthInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Reactmore\SupportAdapter\Adapter\Auth;
4+
5+
/**
6+
* The interface for all authentication implementations.
7+
*/
8+
interface AuthInterface
9+
{
10+
/**
11+
* Returns an array of headers required for authentication.
12+
*
13+
* @return array The headers required for authentication.
14+
*/
15+
public function getHeaders(): array;
16+
}

src/Adapter/Auth/BearerToken.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Reactmore\SupportAdapter\Adapter\Auth;
4+
5+
/**
6+
* Class BearerToken
7+
*
8+
* BearerToken class is an implementation of the Auth interface for authenticated requests using API token.
9+
*
10+
* @package ReactMoreTech\Support\Adapter\Auth
11+
*/
12+
13+
class BearerToken implements AuthInterface
14+
{
15+
/**
16+
* @var string The API token to be used for authentication
17+
*/
18+
private $bearerToken;
19+
20+
/**
21+
* BearerToken constructor.
22+
*
23+
* @param string $bearerToken The API token to be used for authentication
24+
*/
25+
public function __construct(string $bearerToken)
26+
{
27+
$this->bearerToken = $bearerToken;
28+
}
29+
30+
/**
31+
* Get the headers needed for API authentication
32+
*
33+
* @return array The headers needed for API authentication
34+
*/
35+
public function getHeaders(): array
36+
{
37+
return [
38+
'Authorization' => 'Bearer ' . $this->bearerToken
39+
];
40+
}
41+
}

src/Adapter/Auth/None.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Reactmore\SupportAdapter\Adapter\Auth;
4+
5+
/**
6+
* Class None
7+
* Implementation of the Auth interface for unauthenticated requests.
8+
*
9+
* This class returns an empty array for the headers as there is no authentication required for the API request.
10+
*
11+
* @package ReactMoreTech\Support\Adapter\Auth
12+
*/
13+
14+
class None implements AuthInterface
15+
{
16+
/**
17+
* Returns empty headers as no authentication is needed.
18+
*
19+
* @return array Empty headers.
20+
*/
21+
public function getHeaders(): array
22+
{
23+
return [];
24+
}
25+
}
26+

src/Adapter/Formatter/Response.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Reactmore\SupportAdapter\Adapter\Formatter;
4+
5+
use JsonSerializable;
6+
7+
/**
8+
* API Response Wrapper Class
9+
*/
10+
class Response implements JsonSerializable
11+
{
12+
private array $response;
13+
14+
/**
15+
* Constructor.
16+
*
17+
* @param bool $success Status keberhasilan request.
18+
* @param int $statusCode HTTP Status Code.
19+
* @param string $message Pesan response.
20+
* @param mixed $data Data response yang bisa dikustom.
21+
*/
22+
public function __construct(
23+
bool $success,
24+
int $statusCode,
25+
string $message,
26+
mixed $data
27+
) {
28+
$this->response = [
29+
'success' => $success,
30+
'status_code' => $statusCode,
31+
'message' => $message,
32+
'data' => $data
33+
];
34+
}
35+
36+
public function toArray(): array
37+
{
38+
return $this->response;
39+
}
40+
41+
public function jsonSerialize(): mixed
42+
{
43+
return $this->response;
44+
}
45+
46+
public function toJson(): string
47+
{
48+
return json_encode($this->response, JSON_PRETTY_PRINT);
49+
}
50+
51+
public function __get(string $key): mixed
52+
{
53+
return $this->response[$key] ?? null;
54+
}
55+
56+
public function __toString(): string
57+
{
58+
return $this->toJson();
59+
}
60+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Reactmore\SupportAdapter\Adapter\Formatter;
4+
5+
class ResponseFormatter
6+
{
7+
/**
8+
* Format response dari API dengan opsi custom data.
9+
*
10+
* @param string $responseBody
11+
* @param string|null $message
12+
* @param callable|null $dataFilter Callback function untuk memfilter data API.
13+
* @return Response
14+
*/
15+
public static function formatResponse($responseBody, $message = null, callable $dataFilter = null): Response
16+
{
17+
$response = json_decode($responseBody, true);
18+
$data = $response ?? null;
19+
20+
// Jika ada callback filter, gunakan filter tersebut untuk mengubah data
21+
if ($dataFilter && is_callable($dataFilter)) {
22+
$data = call_user_func($dataFilter, $data);
23+
}
24+
25+
return new Response(
26+
success: true,
27+
statusCode: $response['statusCode'] ?? 200,
28+
message: $response['messages'] ?? ($message ?? 'Request berhasil'),
29+
data: $data
30+
);
31+
}
32+
33+
/**
34+
* Format error response
35+
*
36+
* @param string $message
37+
* @param int $statusCode
38+
* @return Response
39+
*/
40+
public static function formatErrorResponse($message, $statusCode = 500): Response
41+
{
42+
return new Response(
43+
success: false,
44+
statusCode: $statusCode,
45+
message: $message,
46+
data: null
47+
);
48+
}
49+
}

0 commit comments

Comments
 (0)