Name update for params/query

Order in methods
url: mandatory
payload: mandatory in post/put
header = []
query = ""

old "params" -> "payload"
This commit is contained in:
Clemens Schwaighofer
2024-10-21 09:52:49 +09:00
parent 934db50b3a
commit f781b5e55f
2 changed files with 35 additions and 33 deletions

View File

@@ -93,16 +93,16 @@ class Curl implements Interface\RequestsInterface
} }
/** /**
* Convert array params to json type string * Convert array payload data to json type string
* *
* @param string|array<string,mixed> $params * @param string|array<string,mixed> $payload
* @return string * @return string
*/ */
private function convertParams(string|array $params): string private function convertPayloadData(string|array $payload): string
{ {
// convert to string as JSON block if it is an array // convert to string as JSON block if it is an array
if (is_array($params)) { if (is_array($payload)) {
$params = Json::jsonConvertArrayTo($params); $params = Json::jsonConvertArrayTo($payload);
} }
return $params; return $params;
} }
@@ -341,22 +341,22 @@ class Curl implements Interface\RequestsInterface
* *
* @param string $url The URL being requested, * @param string $url The URL being requested,
* including domain and protocol * including domain and protocol
* @param string|array<string,mixed> $params String to pass on as POST * @param string|array<string,mixed> $payload Data to pass on as POST
* @param array<string> $headers Headers to be used in the request * @param array<string> $headers [default=[]] Headers to be used in the request
* @param null|string|array<string,mixed> $query Optinal query parameters, array will be converted * @param null|string|array<string,mixed> $query [default=null] Optinal query parameters, array will be converted
* @return array{code:string,content:string} Result code and content as array, content is json * @return array{code:string,content:string} Result code and content as array, content is json
*/ */
public function requestPost( public function requestPost(
string $url, string $url,
string|array $params, string|array $payload,
array $headers, array $headers = [],
null|string|array $query = null null|string|array $query = null
): array { ): array {
return $this->curlRequest( return $this->curlRequest(
"post", "post",
$this->convertQuery($url, $query), $this->convertQuery($url, $query),
$headers, $headers,
$this->convertParams($params) $this->convertPayloadData($payload)
); );
} }
@@ -366,22 +366,22 @@ class Curl implements Interface\RequestsInterface
* *
* @param string $url The URL being requested, * @param string $url The URL being requested,
* including domain and protocol * including domain and protocol
* @param string|array<string,mixed> $params String to pass on as POST * @param string|array<string,mixed> $payload String to pass on as POST
* @param array<string> $headers Headers to be used in the request * @param array<string> $headers [default=[]] Headers to be used in the request
* @param null|string|array<string,mixed> $query Optinal query parameters, array will be converted * @param null|string|array<string,mixed> $query [default=null] Optinal query parameters, array will be converted
* @return array{code:string,content:string} Result code and content as array, content is json * @return array{code:string,content:string} Result code and content as array, content is json
*/ */
public function requestPut( public function requestPut(
string $url, string $url,
string|array $params, string|array $payload,
array $headers, array $headers = [],
null|string|array $query = null null|string|array $query = null
): array { ): array {
return $this->curlRequest( return $this->curlRequest(
"put", "put",
$this->convertQuery($url, $query), $this->convertQuery($url, $query),
$headers, $headers,
$this->convertParams($params) $this->convertPayloadData($payload)
); );
} }

View File

@@ -17,11 +17,12 @@ interface RequestsInterface
* *
* @param string $url The URL being requested, * @param string $url The URL being requested,
* including domain and protocol * including domain and protocol
* @param array<string> $headers Headers to be used in the request * @param array<string> $headers [default=[]] Headers to be used in the request
* @param null|string|array<string,mixed> $query String to pass on as GET, if array will be converted * @param null|string|array<string,mixed> $query [default=null] String to pass on as GET,
* if array will be converted
* @return array{code:string,content:string} Result code and content as array, content is json * @return array{code:string,content:string} Result code and content as array, content is json
*/ */
public function requestGet(string $url, array $headers, null|string|array $query = null): array; public function requestGet(string $url, array $headers = [], null|string|array $query = null): array;
/** /**
* Makes an request to the target url via curl: POST * Makes an request to the target url via curl: POST
@@ -29,15 +30,15 @@ interface RequestsInterface
* *
* @param string $url The URL being requested, * @param string $url The URL being requested,
* including domain and protocol * including domain and protocol
* @param string|array<string,mixed> $params String to pass on as POST * @param string|array<string,mixed> $payload Data to pass on as POST
* @param array<string> $headers Headers to be used in the request * @param array<string> $headers [default=[]] Headers to be used in the request
* @param null|string|array<string,mixed> $query URL query parameters * @param null|string|array<string,mixed> $query [default=null] Optinal query parameters, array will be converted
* @return array{code:string,content:string} Result code and content as array, content is json * @return array{code:string,content:string} Result code and content as array, content is json
*/ */
public function requestPost( public function requestPost(
string $url, string $url,
string|array $params, string|array $payload,
array $headers, array $headers = [],
null|string|array $query = null null|string|array $query = null
): array; ): array;
@@ -47,15 +48,15 @@ interface RequestsInterface
* *
* @param string $url The URL being requested, * @param string $url The URL being requested,
* including domain and protocol * including domain and protocol
* @param string|array<string,mixed> $params String to pass on as POST * @param string|array<string,mixed> $payload Data to pass on as POST
* @param array<string> $headers Headers to be used in the request * @param array<string> $headers [default=[]] Headers to be used in the request
* @param null|string|array<string,mixed> $query String to pass on as GET, if array will be converted * @param null|string|array<string,mixed> $query [default=null] Optinal query parameters, array will be converted
* @return array{code:string,content:string} Result code and content as array, content is json * @return array{code:string,content:string} Result code and content as array, content is json
*/ */
public function requestPut( public function requestPut(
string $url, string $url,
string|array $params, string|array $payload,
array $headers, array $headers = [],
null|string|array $query = null null|string|array $query = null
): array; ): array;
@@ -65,11 +66,12 @@ interface RequestsInterface
* *
* @param string $url The URL being requested, * @param string $url The URL being requested,
* including domain and protocol * including domain and protocol
* @param array<string> $headers Headers to be used in the request * @param array<string> $headers [default=[]] Headers to be used in the request
* @param null|string|array<string,mixed> $query String to pass on as GET, if array will be converted * @param null|string|array<string,mixed> $query [default=null] String to pass on as GET,
* if array will be converted
* @return array{code:string,content:string} Result code and content as array, content is json * @return array{code:string,content:string} Result code and content as array, content is json
*/ */
public function requestDelete(string $url, array $headers, null|string|array $query = null): array; public function requestDelete(string $url, array $headers = [], null|string|array $query = null): array;
} }
// __END__ // __END__