Skip to content

Commit 2da4bb9

Browse files
committed
Added: HTTP API Model methods
Added new configuration options in microservice.php so the HTTP methods used for updating and deleting models can be customized via models.update_method and models.delete_method
1 parent f1c0ac0 commit 2da4bb9

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Smart error handling based on the request's expected format.
1212
- Error propagation improvements with tests for all status codes.
1313
- Introduced `ParsesApiResponse` trait to centralize API response parsing logic.
14+
- Configurable HTTP methods for model updates and deletions.
1415

1516
### Changed
1617
- Refactored `QueryBuilder` and `ApiModel` to use the new `ParsesApiResponse` trait, removing duplicate `parseResponse` implementations.

src/Models/Model.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public static function create(array $attributes = []): ?self
109109
public function save(array $options = []): bool
110110
{
111111
if ($this->exists) {
112-
$response = static::client()->put(static::endpoint().'/'.$this->getKey(), $this->attributesToArray());
112+
$method = strtolower(config('microservice.models.update_method', 'put'));
113+
$response = static::client()->{$method}(static::endpoint().'/'.$this->getKey(), $this->attributesToArray());
113114
} else {
114115
$response = static::client()->post(static::endpoint(), $this->attributesToArray());
115116
}
@@ -129,7 +130,8 @@ public function save(array $options = []): bool
129130
*/
130131
public function delete(): bool
131132
{
132-
$response = static::client()->delete(static::endpoint().'/'.$this->getKey());
133+
$method = strtolower(config('microservice.models.delete_method', 'delete'));
134+
$response = static::client()->{$method}(static::endpoint().'/'.$this->getKey());
133135

134136
if (is_object($response) && method_exists($response, 'successful')) {
135137
return $response->successful();

src/config/microservice.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,17 @@
167167
'gateway_auth' => [
168168
'default_redirect' => env('GATEWAY_AUTH_DEFAULT_REDIRECT', '/'),
169169
],
170+
171+
/*
172+
|--------------------------------------------------------------------------
173+
| Model HTTP Methods
174+
|--------------------------------------------------------------------------
175+
|
176+
| Configure which HTTP verbs are used when models are updated or deleted
177+
| through the API gateway. Defaults align with Laravel's expectations.
178+
*/
179+
'models' => [
180+
'update_method' => env('MODEL_UPDATE_METHOD', 'put'),
181+
'delete_method' => env('MODEL_DELETE_METHOD', 'delete'),
182+
],
170183
];

tests/Models/ApiModelTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,21 @@ public function update_users_gateway()
197197
], $this->gateway->getCalls());
198198
}
199199

200+
/** @test */
201+
public function update_users_respects_configured_method()
202+
{
203+
$this->app['config']->set('microservice.models.update_method', 'post');
204+
205+
$user = new RemoteUser(['id' => 12, 'name' => 'Old']);
206+
$user->exists = true;
207+
$user->name = 'New';
208+
$user->save();
209+
210+
$this->assertSame([
211+
['method' => 'POST', 'uri' => '/users/12', 'data' => ['id' => 12, 'name' => 'New']],
212+
], $this->gateway->getCalls());
213+
}
214+
200215
/** @test */
201216
public function delete_users_gateway()
202217
{
@@ -209,6 +224,20 @@ public function delete_users_gateway()
209224
], $this->gateway->getCalls());
210225
}
211226

227+
/** @test */
228+
public function delete_users_respects_configured_method()
229+
{
230+
$this->app['config']->set('microservice.models.delete_method', 'post');
231+
232+
$user = new RemoteUser(['id' => 5]);
233+
$user->exists = true;
234+
$user->delete();
235+
236+
$this->assertSame([
237+
['method' => 'POST', 'uri' => '/users/5', 'data' => []],
238+
], $this->gateway->getCalls());
239+
}
240+
212241
/** @test */
213242
public function where_get_filters_results()
214243
{

0 commit comments

Comments
 (0)