Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit ca44604

Browse files
committed
Add delete and put methods
1 parent 2f38e66 commit ca44604

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/Api/AbstractApi.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ protected function constructRequest()
4949
/**
5050
* Send a GET request with query parameters.
5151
*
52-
* @param string $path
53-
* @param string $parameters
52+
* @param string $path
53+
* @param string $parameters
5454
*
5555
* @return \Illuminate\Http\Client\Response
5656
*/
@@ -63,8 +63,8 @@ protected function get($path, $parameters = [])
6363
/**
6464
* Send a POST request with query parameters.
6565
*
66-
* @param string $path
67-
* @param string $parameters
66+
* @param string $path
67+
* @param string $parameters
6868
*
6969
* @return \Illuminate\Http\Client\Response
7070
*/
@@ -74,12 +74,40 @@ protected function post($path, $parameters = [])
7474
->post($this->apiRequest.$path, $parameters));
7575
}
7676

77+
/**
78+
* Send a PUT request
79+
*
80+
* @param $path
81+
* @param array $parameters
82+
*
83+
* @return \Illuminate\Http\Client\Response|void
84+
*/
85+
protected function put($path, $parameters = [])
86+
{
87+
return $this->checkExceptions(Http::withToken($this->magento->token)
88+
->put($this->apiRequest.$path, $parameters));
89+
}
90+
91+
/**
92+
* Send a DELETE request
93+
*
94+
* @param $path
95+
* @param array $parameters
96+
*
97+
* @return \Illuminate\Http\Client\Response|void
98+
*/
99+
protected function delete($path, $parameters = [])
100+
{
101+
return $this->checkExceptions(Http::withToken($this->magento->token)
102+
->delete($this->apiRequest.$path, $parameters));
103+
}
104+
77105
/**
78106
* Check for any type of invalid API Responses.
79107
*
80108
* @param \Illuminate\Http\Client\Response $response
81-
* @throws \Exception
82109
* @return void
110+
* @throws \Exception
83111
*/
84112
protected function checkExceptions($response)
85113
{
@@ -93,8 +121,8 @@ protected function checkExceptions($response)
93121
/**
94122
* Validates the usage of the store code as needed.
95123
*
96-
* @throws \Exception
97124
* @return void
125+
* @throws \Exception
98126
*/
99127
protected function validateSingleStoreCode()
100128
{

src/Api/Custom.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class Custom extends AbstractApi
88
{
9+
const HTTP_METHODS = ['get', 'post', 'put', 'delete'];
10+
911
/**
1012
* @var string Magento API endpoint
1113
*/
@@ -18,7 +20,7 @@ class Custom extends AbstractApi
1820
*/
1921
public function __construct(string $endpoint, Magento $magento)
2022
{
21-
$this->endpoint = $endpoint;
23+
$this->endpoint = '/'.rtrim(ltrim($endpoint, '/'), '/').'/';
2224

2325
parent::__construct($magento);
2426
}
@@ -32,8 +34,8 @@ public function __construct(string $endpoint, Magento $magento)
3234
*/
3335
public function __call($method, $args)
3436
{
35-
if ($method == 'get' || $method == 'post') {
36-
$args[0] = rtrim($this->endpoint, '/').'/'.$args[0];
37+
if (in_array($method, self::HTTP_METHODS)) {
38+
$args[0] = $this->endpoint . ltrim($args[0], '/');
3739
}
3840

3941
return call_user_func_array([$this, $method], $args);

0 commit comments

Comments
 (0)