Skip to content
This repository was archived by the owner on Nov 4, 2025. It is now read-only.

Commit aacaec1

Browse files
author
Niek Brekelmans
committed
dont throw errors on 404
1 parent b039aa5 commit aacaec1

File tree

6 files changed

+62
-9
lines changed

6 files changed

+62
-9
lines changed

src/HetznerDnsClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
namespace DutchCodingCompany\HetznerDnsClient;
44

5+
use DutchCodingCompany\HetznerDnsClient\Traits\ThrowsOnErrorsExceptNotFound;
56
use Sammyjo20\Saloon\Http\SaloonConnector;
67
use Sammyjo20\Saloon\Traits\Plugins\AcceptsJson;
7-
use Sammyjo20\Saloon\Traits\Plugins\AlwaysThrowsOnErrors;
88

99
/**
1010
* @method RequestCollections\ZoneCollection zones()
1111
* @method RequestCollections\RecordCollection records()
1212
*/
1313
class HetznerDnsClient extends SaloonConnector
1414
{
15-
use AcceptsJson, AlwaysThrowsOnErrors;
15+
use AcceptsJson, ThrowsOnErrorsExceptNotFound;
1616
use Traits\ResolvesApiToken;
1717

1818
protected array $requests = [

src/RequestCollections/RecordCollection.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
class RecordCollection extends RequestCollection
2020
{
21-
public function all(?int $page = null, ?int $per_page = null, ?string $zone_id = null): Records
21+
public function all(?int $page = null, ?int $per_page = null, ?string $zone_id = null): ?Records
2222
{
2323
return $this->connector->request(new ListRecords(page: $page, per_page: $per_page, zone_id: $zone_id))->send()->dto();
2424
}
@@ -28,6 +28,21 @@ public function create(string $zone_id, RecordType $type, string $name, string $
2828
return $this->connector->request(new CreateRecord(zone_id: $zone_id, type: $type, name: $name, value: $value, ttl: $ttl))->send()->dto();
2929
}
3030

31+
public function createIfNotExists(string $zone_id, RecordType $type, string $name, string $value, ?int $ttl = null): Record
32+
{
33+
$records = $this->all(zone_id: $zone_id);
34+
$record = collect($records->records)
35+
->where('name', $name)
36+
->filter(fn (Record $record) => $record->type->value === $type->value)
37+
->first();
38+
39+
if (! is_null($record)) {
40+
return $record; // already exists
41+
}
42+
43+
return $this->create($zone_id, $type, $name, $value, $ttl);
44+
}
45+
3146
public function get(string $record_id): Record
3247
{
3348
return $this->connector->request(new GetRecord(record_id: $record_id))->send()->dto();

src/RequestCollections/ZoneCollection.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class ZoneCollection extends RequestCollection
1616
{
17-
public function all(string $name = null,?int $per_page = null, ?string $search_name = null): Zones
17+
public function all(string $name = null,?int $per_page = null, ?string $search_name = null): ?Zones
1818
{
1919
return $this->connector->request(new ListZones(name: $name, per_page: $per_page, search_name: $search_name))->send()->dto();
2020
}
@@ -24,11 +24,19 @@ public function create(string $name, ?int $ttl = null): Zone
2424
return $this->connector->request(new CreateZone(name: $name, ttl: $ttl))->send()->dto();
2525
}
2626

27-
public function get(string $zone_id): Zone
27+
public function get(string $zone_id): ?Zone
2828
{
2929
return $this->connector->request(new GetZone(zone_id: $zone_id))->send()->dto();
3030
}
3131

32+
public function getByName(string $name): ?Zone
33+
{
34+
$zones = $this->all(name: $name);
35+
36+
/** @var Zone $zone */
37+
return collect($zones?->zones)->where('name', $name)->first();
38+
}
39+
3240
public function update(string $zone_id, string $name, ?int $ttl = null): Zone
3341
{
3442
return $this->connector->request(new UpdateZone(zone_id: $zone_id, name: $name, ttl: $ttl))->send()->dto();

src/Requests/Records/CreateRecord.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public function __construct(
2222
protected string $name,
2323
protected string $value,
2424
protected ?int $ttl = null,
25-
)
26-
{}
25+
) {}
2726

2827
protected ?string $connector = HetznerDnsClient::class;
2928

src/Requests/Records/UpdateRecord.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public function __construct(
2424
protected string $name,
2525
protected string $value,
2626
protected ?int $ttl = null,
27-
)
28-
{}
27+
) {}
2928

3029
protected ?string $connector = HetznerDnsClient::class;
3130

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace DutchCodingCompany\HetznerDnsClient\Traits;
4+
5+
use Sammyjo20\Saloon\Http\SaloonRequest;
6+
use Sammyjo20\Saloon\Http\SaloonResponse;
7+
use Sammyjo20\Saloon\Traits\Plugins\AlwaysThrowsOnErrors;
8+
9+
trait ThrowsOnErrorsExceptNotFound
10+
{
11+
/**
12+
* Always throw if there is something wrong with the request.
13+
*
14+
* @param SaloonRequest $request
15+
* @return void
16+
* @throws \Sammyjo20\Saloon\Exceptions\SaloonInvalidConnectorException
17+
*/
18+
public function bootThrowsOnErrorsExceptNotFound(SaloonRequest $request): void
19+
{
20+
if ($this instanceof SaloonRequest && $this->traitExistsOnConnector(ThrowsOnErrorsExceptNotFound::class)) {
21+
return;
22+
}
23+
24+
$this->addResponseInterceptor(function (SaloonRequest $request, SaloonResponse $response) {
25+
if($response->status() !== 404) {
26+
$response->throw();
27+
}
28+
29+
return $response;
30+
});
31+
}
32+
}

0 commit comments

Comments
 (0)