Skip to content

Commit

Permalink
Added Contracts for Request and Response
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrulhazim committed Nov 5, 2024
1 parent b8598b4 commit 767e95c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Api/ApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CleaniqueCoders\KongAdminApi\Api;

use CleaniqueCoders\KongAdminApi\Contracts\Request as RequestContract;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
Expand All @@ -13,7 +14,7 @@
* Represents a generic API request that can be customized with HTTP methods,
* endpoints, and JSON body handling.
*/
class ApiRequest extends Request implements HasBody
class ApiRequest extends Request implements HasBody, RequestContract
{
use HasJsonBody;

Expand Down
3 changes: 2 additions & 1 deletion src/Api/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace CleaniqueCoders\KongAdminApi\Api;

use CleaniqueCoders\KongAdminApi\Contracts\Response;
use DateTimeInterface;

/**
* Class ApiResponse
*
* Encapsulates the API response structure and data.
*/
class ApiResponse
class ApiResponse implements Response
{
/**
* The HTTP status code of the response.
Expand Down
50 changes: 50 additions & 0 deletions src/Contracts/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace CleaniqueCoders\KongAdminApi\Contracts;

use Saloon\Enums\Method;

/**
* Interface Request
*
* Defines the contract for API request objects, outlining the methods required
* to configure and retrieve details about an API request, such as the HTTP method
* and endpoint.
*/
interface Request
{
/**
* Set the HTTP method for the request.
*
* @param Method $method The HTTP method (e.g., GET, POST, PATCH, DELETE)
*/
public function setMethod(Method $method): self;

/**
* Get the HTTP method for the request.
*
* @return Method The HTTP method
*/
public function getMethod(): Method;

/**
* Set the endpoint for the API request.
*
* @param string $endpoint The API endpoint
*/
public function setEndpoint(string $endpoint): self;

/**
* Get the endpoint for the API request.
*
* @return string The endpoint for the API request
*/
public function getEndpoint(): string;

/**
* Resolve and retrieve the complete endpoint path for the API request.
*
* @return string The resolved endpoint path
*/
public function resolveEndpoint(): string;
}
43 changes: 43 additions & 0 deletions src/Contracts/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace CleaniqueCoders\KongAdminApi\Contracts;

use DateTimeInterface;

/**
* Interface Response
*
* Defines the contract for API response objects, specifying the structure and methods
* required for interacting with the response data, such as status, data, and timestamps.
*/
interface Response
{
/**
* Get the HTTP status code of the response.
*/
public function getStatusCode(): string;

/**
* Get the HTTP status phrase of the response.
*/
public function getStatusPhrase(): string;

/**
* Get the data payload from the API response.
*
* @return array<string, mixed>
*/
public function getData(): array;

/**
* Get the timestamp when the response was processed.
*/
public function getRespondedAt(): DateTimeInterface;

/**
* Convert the response data to an array format.
*
* @return array<string, mixed>
*/
public function toArray(): array;
}

0 comments on commit 767e95c

Please sign in to comment.