-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdoc-examples-php-apis.php
More file actions
178 lines (149 loc) · 5.94 KB
/
doc-examples-php-apis.php
File metadata and controls
178 lines (149 loc) · 5.94 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
require "vendor/autoload.php";
use Momento\Auth\CredentialProvider;
use Momento\Cache\CacheClient;
use Momento\Config\Configurations\Laptop;
function retrieveApiKeyFromYourSecretsManager(): string
{
// this is not a valid API key but conforms to the syntax requirements.
return
"eyJhcGlfa2V5IjogImV5SjBlWEFpT2lKS1YxUWlMQ0poYkdjaU9pSklVekkxTmlKOS5leUpwYzNNaU9pSlBibXhwYm1VZ1NsZFVJRUoxYVd4a1pYSWlMQ0pwWVhRaU9qRTJOemd6TURVNE1USXNJbVY0Y0NJNk5EZzJOVFV4TlRReE1pd2lZWFZrSWpvaUlpd2ljM1ZpSWpvaWFuSnZZMnRsZEVCbGVHRnRjR3hsTG1OdmJTSjkuOEl5OHE4NExzci1EM1lDb19IUDRkLXhqSGRUOFVDSXV2QVljeGhGTXl6OCIsICJlbmRwb2ludCI6ICJ0ZXN0Lm1vbWVudG9ocS5jb20ifQo=";
}
function retrieveApiKeyV2FromYourSecretsManager(): string
{
// this is not a valid API key but conforms to the syntax requirements.
return
"eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJ0IjoiZyIsImp0aSI6InNvbWUtaWQifQ.GMr9nA6HE0ttB6llXct_2Sg5-fOKGFbJCdACZFgNbN1fhT6OPg_hVc8ThGzBrWC_RlsBpLA1nzqK3SOJDXYxAw";
}
function example_API_InstantiateCacheClient()
{
new CacheClient(
Laptop::latest(),
CredentialProvider::fromEnvironmentVariablesV2(),
60
);
}
function example_API_CredentialProviderFromEnvVar()
{
CredentialProvider::fromEnvironmentVariable("V1_API_KEY");
}
function example_API_CredentialProviderFromEnvVarV2()
{
CredentialProvider::fromEnvironmentVariablesV2("MOMENTO_API_KEY", "MOMENTO_ENDPOINT");
}
function example_API_CredentialProviderFromEnvVarV2Default()
{
CredentialProvider::fromEnvironmentVariablesV2();
}
function example_API_CredentialProviderFromApiKeyV2()
{
$api_key = retrieveApiKeyV2FromYourSecretsManager();
$endpoint = "cell-4-us-west-2-1.prod.a.momentohq.com";
CredentialProvider::fromApiKeyV2($api_key, $endpoint);
}
function example_API_CredentialProviderFromDisposableToken()
{
$auth_token = retrieveApiKeyFromYourSecretsManager();
CredentialProvider::fromDisposableToken($auth_token);
}
function example_API_CreateCache(CacheClient $cache_client, string $cache_name)
{
$create_cache_response = $cache_client->createCache($cache_name);
if ($create_cache_response->asSuccess()) {
print("Cache $cache_name created\n");
} elseif ($create_cache_response->asAlreadyExists()) {
print("Cache $cache_name already exists\n");
} elseif ($err = $create_cache_response->asError()) {
print("An error occurred while attempting to create $cache_name: {$err->errorCode()} - {$err->message()}\n");
}
}
function example_API_DeleteCache(CacheClient $cache_client, string $cache_name)
{
$delete_cache_response = $cache_client->deleteCache($cache_name);
if ($err = $delete_cache_response->asError()) {
print("An error occurred while attempting to delete $cache_name: {$err->errorCode()} - {$err->message()}\n");
} else {
print("Cache $cache_name deleted\n");
}
}
function example_API_ListCaches(CacheClient $cache_client)
{
$list_caches_response = $cache_client->listCaches();
if ($success = $list_caches_response->asSuccess()) {
print("Found caches:\n");
foreach ($success->caches() as $cache) {
$cache_name = $cache->name();
print("- $cache_name\n");
}
} elseif ($err = $list_caches_response->asError()) {
print("An error occurred while attempting to list caches: {$err->errorCode()} - {$err->message()}\n");
}
}
function example_API_Set(CacheClient $cache_client, string $cache_name)
{
$set_response = $cache_client->set($cache_name, "test-key", "test-value");
if ($set_response->asSuccess()) {
print("Key $cache_name stored successfully\n");
} elseif ($err = $set_response->asError()) {
print("An error occurred while attempting to store $cache_name: {$err->errorCode()} - {$err->message()}\n");
}
}
function example_API_Get(CacheClient $cache_client, string $cache_name)
{
$get_response = $cache_client->get($cache_name, "test-key");
if ($hit = $get_response->asHit()) {
print("Retrieved value for key 'test-key': {$hit->valueString()}\n");
} elseif ($get_response->asMiss()) {
print("Key 'test-key' was not found in cache $cache_name\n");
} elseif ($err = $get_response->asError()) {
print("An error occurred while attempting to get key 'test-key' from cache $cache_name: {$err->errorCode()} - {$err->message()}\n");
}
}
function example_API_Delete(CacheClient $cache_client, string $cache_name)
{
$delete_response = $cache_client->delete($cache_name, "test-key");
if ($delete_response->asSuccess()) {
print("Key 'test-key' deleted successfully\n");
} elseif ($err = $delete_response->asError()) {
print("An error occurred while attempting to delete key 'test-key' from cache $cache_name: {$err->errorCode()} - {$err->message()}\n");
}
}
function setup()
{
$cache_client = new CacheClient(
Laptop::latest(),
CredentialProvider::fromEnvironmentVariablesV2(),
60
);
$cache_name = uniqid("php-examples-test-cache-");
return [
$cache_client,
$cache_name,
];
}
function teardown($cache_client, $cache_name)
{
$cache_client->deleteCache($cache_name);
}
function main()
{
[$cache_client, $cache_name] = setup();
try {
example_API_CredentialProviderFromEnvVar();
example_API_CredentialProviderFromApiKeyV2();
example_API_CredentialProviderFromDisposableToken();
example_API_CredentialProviderFromEnvVarV2();
example_API_CredentialProviderFromEnvVarV2Default();
example_API_InstantiateCacheClient();
example_API_CreateCache($cache_client, $cache_name);
example_API_ListCaches($cache_client);
example_API_Set($cache_client, $cache_name);
example_API_Get($cache_client, $cache_name);
example_API_Delete($cache_client, $cache_name);
example_API_DeleteCache($cache_client, $cache_name);
} finally {
teardown($cache_client, $cache_name);
}
}
main();