Skip to content

Commit

Permalink
Add webhook support (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
silas authored Feb 19, 2024
1 parent 39db932 commit a5819c9
Show file tree
Hide file tree
Showing 121 changed files with 2,036 additions and 438 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.php]
indent_size = 4

[Makefile]
indent_size = 4
indent_style = tab
8 changes: 5 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
- name: Install dependencies
run: composer install

- name: Run phpunit
run: |
./vendor/bin/phpunit tests
- name: Lint
run: make lint

- name: Test
run: make test
3 changes: 3 additions & 0 deletions .php-cs-fixer.dist.php → .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
;

return (new PhpCsFixer\Config())
->setUsingCache(false)
->setRiskyAllowed(true)
->setRules([
'@PSR12' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'@PHP81Migration' => true,
'@PHP80Migration:risky' => true,
'php_unit_strict' => false,
])
->setFinder($finder)
;
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: fmt
fmt:
./vendor/bin/php-cs-fixer fix -v
# fully_qualified_strict_types not fully applied until
# run twice, hopefully we can remove this in the
# future
./vendor/bin/php-cs-fixer fix -v

.PHONY: lint
lint:
@./vendor/bin/php-cs-fixer check -v

.PHONY: test
test:
@./vendor/bin/phpunit tests
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@

Stability: alpha

## Usage
### Requirements

* PHP 8.1 or later

### Getting Started

Install SDK

```sh
composer require userhub/sdk
```

Example

```php
<?php

require __DIR__.'/vendor/autoload.php';

use UserHub\AdminApi;

$adminApi = new AdminApi('sk_123...');
Expand All @@ -17,3 +31,5 @@ foreach ($res->users as $user) {
echo $user->id.' '.$user->displayName.PHP_EOL;
}
```

See the `examples` directory for more examples.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ext-json": "*"
},
"require-dev": {
"donatj/mock-webserver": "^2.7",
"friendsofphp/php-cs-fixer": "^3.49",
"phpunit/phpunit": "^10"
},
Expand Down
4 changes: 2 additions & 2 deletions example/main.php → examples/api/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

require __DIR__.'/../vendor/autoload.php';
require __DIR__.'/../../vendor/autoload.php';

use UserHub\AdminApi;
use UserHub\UserApi;
Expand All @@ -16,7 +16,7 @@
}

$userKey = getenv('USER_KEY');
if (empty($adminKey)) {
if (empty($userKey)) {
echo 'USER_KEY required'.PHP_EOL;

exit(1);
Expand Down
44 changes: 44 additions & 0 deletions examples/webhook/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

require __DIR__.'/../../vendor/autoload.php';

use UserHub\EventsV1\Event;
use UserHub\Webhook;

if (PHP_SAPI === 'cli') {
$port = getenv('PORT') ?? '8000';
echo "php -S localhost:{$port} ".__FILE__.PHP_EOL;

exit(1);
}

$signingSecret = getenv('SIGNING_SECRET');
if (empty($signingSecret)) {
echo 'SIGNING_SECRET required'.PHP_EOL;

exit(1);
}

$wh = new Webhook($signingSecret);

$wh->onEvent(static function (Event $event): void {
error_log('Event: '.$event->type);

switch ($event->type) {
case 'organizations.changed':
$organization = $event->organizationsChanged->organization;
error_log(" - Organization: {$organization->id} {$organization->displayName}");

break;

case 'users.changed':
$user = $event->usersChanged->user;
error_log(" - User: {$user->id} {$user->displayName}");

break;
}
});

$wh->handleFromGlobals();
28 changes: 14 additions & 14 deletions lib/AdminApi/Flows.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ public function list(
$req = new Request('admin.flows.list', 'GET', '/admin/v1/flows');
$req->setIdempotent(true);

if (isset($organizationId)) {
if (!empty($organizationId)) {
$req->setQuery('organizationId', $organizationId);
}
if (isset($userId)) {
if (!empty($userId)) {
$req->setQuery('userId', $userId);
}
if (isset($type)) {
if (!empty($type)) {
$req->setQuery('type', $type);
}
if (isset($pageSize)) {
if (!empty($pageSize)) {
$req->setQuery('pageSize', $pageSize);
}
if (isset($pageToken)) {
if (!empty($pageToken)) {
$req->setQuery('pageToken', $pageToken);
}
if (isset($orderBy)) {
if (!empty($orderBy)) {
$req->setQuery('orderBy', $orderBy);
}
if (isset($view)) {
if (!empty($view)) {
$req->setQuery('view', $view);
}

Expand Down Expand Up @@ -88,25 +88,25 @@ public function createJoinOrganization(
$req = new Request('admin.flows.createJoinOrganization', 'POST', '/admin/v1/flows:createJoinOrganization');
$body = [];

if (isset($organizationId)) {
if (!empty($organizationId)) {
$body['organizationId'] = $organizationId;
}
if (isset($userId)) {
if (!empty($userId)) {
$body['userId'] = $userId;
}
if (isset($email)) {
if (!empty($email)) {
$body['email'] = $email;
}
if (isset($displayName)) {
if (!empty($displayName)) {
$body['displayName'] = $displayName;
}
if (isset($creatorUserId)) {
if (!empty($creatorUserId)) {
$body['creatorUserId'] = $creatorUserId;
}
if (isset($expireTime)) {
if (!empty($expireTime)) {
$body['expireTime'] = Util::encodeDateTime($expireTime);
}
if (isset($ttl)) {
if (!empty($ttl)) {
$body['ttl'] = $ttl;
}

Expand Down
14 changes: 7 additions & 7 deletions lib/AdminApi/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ public function list(
$req = new Request('admin.invoices.list', 'GET', '/admin/v1/invoices');
$req->setIdempotent(true);

if (isset($organizationId)) {
if (!empty($organizationId)) {
$req->setQuery('organizationId', $organizationId);
}
if (isset($userId)) {
if (!empty($userId)) {
$req->setQuery('userId', $userId);
}
if (isset($pageSize)) {
if (!empty($pageSize)) {
$req->setQuery('pageSize', $pageSize);
}
if (isset($pageToken)) {
if (!empty($pageToken)) {
$req->setQuery('pageToken', $pageToken);
}
if (isset($orderBy)) {
if (!empty($orderBy)) {
$req->setQuery('orderBy', $orderBy);
}

Expand All @@ -73,10 +73,10 @@ public function get(
$req = new Request('admin.invoices.get', 'GET', '/admin/v1/invoices/'.rawurlencode($invoiceId));
$req->setIdempotent(true);

if (isset($organizationId)) {
if (!empty($organizationId)) {
$req->setQuery('organizationId', $organizationId);
}
if (isset($userId)) {
if (!empty($userId)) {
$req->setQuery('userId', $userId);
}

Expand Down
Loading

0 comments on commit a5819c9

Please sign in to comment.