Skip to content

Commit bd3d9f3

Browse files
authored
php: Add application getOrCreate (#2101)
was not added in the original PHP SDK release.
2 parents 3e78d20 + 2c8ced4 commit bd3d9f3

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/** Get or create a new application */
2+
public function getOrCreate(
3+
ApplicationIn $applicationIn,
4+
?ApplicationCreateOptions $options = null,
5+
): ApplicationOut {
6+
$request = $this->client->newReq('POST', '/api/v1/app');
7+
if (null !== $options) {
8+
$request->setHeaderParam('idempotency-key', $options->idempotencyKey);
9+
}
10+
$request->setQueryParam("get_if_exists", "true");
11+
$request->setBody(json_encode($applicationIn));
12+
$res = $this->client->send($request);
13+
14+
return ApplicationOut::fromJson($res);
15+
}

codegen/templates/php/api_resource.php.jinja

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,8 @@ class {{ resource_type_name }}
7575
{% endif -%}
7676
}
7777

78+
{% set api_operation_extra -%}api_extra/{{ resource.name | to_snake_case }}_{{ op.name | to_snake_case }}.php{% endset %}
79+
{% include api_operation_extra ignore missing %}
80+
7881
{% endfor -%}
7982
}

php/src/Api/Application.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ public function create(
4848
return ApplicationOut::fromJson($res);
4949
}
5050

51+
/** Get or create a new application */
52+
public function getOrCreate(
53+
ApplicationIn $applicationIn,
54+
?ApplicationCreateOptions $options = null,
55+
): ApplicationOut {
56+
$request = $this->client->newReq('POST', '/api/v1/app');
57+
if (null !== $options) {
58+
$request->setHeaderParam('idempotency-key', $options->idempotencyKey);
59+
}
60+
$request->setQueryParam('get_if_exists', 'true');
61+
$request->setBody(json_encode($applicationIn));
62+
$res = $this->client->send($request);
63+
64+
return ApplicationOut::fromJson($res);
65+
}
66+
5167
/** Get an application. */
5268
public function get(
5369
string $appId,

php/tests/MockTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,18 @@ public function testEmptyAppPortalAccessInBody(): void
455455

456456
$this->assertEquals('{}', $rawBody);
457457
}
458+
459+
public function testGetOrCreate(): void
460+
{
461+
$this->mockHandler->append(new Response(200, [], AppOut));
462+
463+
$svx = new \Svix\Svix("super_secret", httpClient: $this->httpClient);
464+
465+
$svx->application->getOrCreate(ApplicationIn::create("asd"));
466+
467+
$req = $this->requestHistory[0]['request'];
468+
469+
$this->assertEquals('get_if_exists=true', $req->getUri()->getQuery());
470+
}
471+
458472
}

0 commit comments

Comments
 (0)