-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.php
More file actions
58 lines (50 loc) · 1.61 KB
/
quickstart.php
File metadata and controls
58 lines (50 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
declare(strict_types=1);
/**
* Stromcom Customer SDK — quickstart.
*
* The smallest possible round-trip: print the project bound to your
* token and the active users in it. Read this first, then move on to
* the focused examples in this directory.
*
* Run with:
* STROMCOM_TOKEN=… php examples/quickstart.php
*
* Optional:
* STROMCOM_BASE_URL=http://localhost:7000/api/customer/v1/ php examples/quickstart.php
*/
require __DIR__ . '/../vendor/autoload.php';
use Stromcom\Sdk\Client;
use Stromcom\Sdk\Configuration;
use Stromcom\Sdk\Exception\ApiException;
use Stromcom\Sdk\Exception\TransportException;
$token = getenv('STROMCOM_TOKEN');
if (!is_string($token) || $token === '') {
fwrite(STDERR, "Set STROMCOM_TOKEN to run this example.\n");
exit(1);
}
$baseUrl = getenv('STROMCOM_BASE_URL');
$config = (is_string($baseUrl) && $baseUrl !== '')
? new Configuration(token: $token, baseUrl: $baseUrl)
: new Configuration(token: $token);
$stromcom = new Client($config);
try {
$project = $stromcom->project()->get();
printf("Project: %s (state=%s)\n", $project->name, $project->state ?? '-');
$users = $stromcom->users()->list(state: 'active');
printf("Active users: %d\n", count($users));
foreach ($users as $user) {
printf(" - %s | %s <%s>\n", $user->code, $user->name, $user->emailAddress ?? '');
}
} catch (ApiException $e) {
fwrite(STDERR, sprintf(
"API error %d (%s): %s\n",
$e->getStatusCode(),
$e->getApiCode() ?? 'unknown',
$e->getMessage(),
));
exit(2);
} catch (TransportException $e) {
fwrite(STDERR, 'Network error: ' . $e->getMessage() . "\n");
exit(3);
}