Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: port webhooks api to new switch pattern #1404

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions examples/nodejs/cache/delete-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
CredentialProvider,
DeleteStoreResponse,
ListStoresResponse,
PreviewStorageClient,
StorageConfigurations,
} from '@gomomento/sdk';

async function main() {
const storageClient = new PreviewStorageClient({
configuration: StorageConfigurations.Laptop.latest(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: 'MOMENTO_API_KEY',
}),
});

// list stores
const listStoresResponse = await storageClient.listStores();

switch (listStoresResponse.type) {
case ListStoresResponse.Success: {
const storeNames = listStoresResponse.stores().map(store => store.getName());
for (const store of storeNames) {
const deleteStoreResponse = await storageClient.deleteStore(store);
if (deleteStoreResponse.type === DeleteStoreResponse.Success) {
console.log(`Deleted store ${store}`);
} else {
console.error(`Error deleting store ${store}: ${deleteStoreResponse.message()}`);
}
}
break;
}
case ListStoresResponse.Error: {
console.error(`Error listing stores: ${listStoresResponse.message()}`);
}
}
}

main()
.then(() => {
console.log('success!!');
})
.catch((e: Error) => {
console.error(`Uncaught exception while running example: ${e.message}`);
throw e;
});
126 changes: 63 additions & 63 deletions examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@
CreateCacheResponse,
CredentialProvider,
DeleteCacheResponse,
DeleteWebhook,

Check warning on line 73 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

'DeleteWebhook' is defined but never used

Check warning on line 73 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

'DeleteWebhook' is defined but never used

Check warning on line 73 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 20

'DeleteWebhook' is defined but never used
DisposableTokenScopes,
ExpiresIn,
FlushCacheResponse,
GenerateApiKey,
GenerateApiKeyResponse,
GenerateDisposableTokenResponse,
GetWebhookSecret,

Check warning on line 80 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

'GetWebhookSecret' is defined but never used

Check warning on line 80 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

'GetWebhookSecret' is defined but never used

Check warning on line 80 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 20

'GetWebhookSecret' is defined but never used
ILeaderboard,
ItemType,
LeaderboardConfigurations,
Expand All @@ -88,12 +88,12 @@
LeaderboardRemoveElementsResponse,
LeaderboardUpsertResponse,
ListCachesResponse,
ListWebhooks,

Check warning on line 91 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

'ListWebhooks' is defined but never used

Check warning on line 91 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

'ListWebhooks' is defined but never used

Check warning on line 91 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 20

'ListWebhooks' is defined but never used
PreviewLeaderboardClient,
PutWebhook,

Check warning on line 93 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

'PutWebhook' is defined but never used

Check warning on line 93 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

'PutWebhook' is defined but never used

Check warning on line 93 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 20

'PutWebhook' is defined but never used
ReadConcern,
RefreshApiKeyResponse,
RotateWebhookSecret,

Check warning on line 96 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 16

'RotateWebhookSecret' is defined but never used

Check warning on line 96 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 18

'RotateWebhookSecret' is defined but never used

Check warning on line 96 in examples/nodejs/cache/doc-example-files/doc-examples-js-apis.ts

View workflow job for this annotation

GitHub Actions / Test examples on node 20

'RotateWebhookSecret' is defined but never used
TokenScopes,
TopicClient,
TopicConfigurations,
Expand Down Expand Up @@ -1481,64 +1481,64 @@
}
}

async function example_API_ListWebhooks(topicClient: TopicClient, cacheName: string) {
const result = await topicClient.listWebhooks(cacheName);
if (result instanceof ListWebhooks.Success) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
console.log(`listed webhooks: ${result.getWebhooks()}`);
} else if (result instanceof ListWebhooks.Error) {
throw new Error(
`An error occurred while attempting to list webhooks for cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
}

async function example_API_DeleteWebhook(topicClient: TopicClient, cacheName: string) {
const result = await topicClient.deleteWebhook(cacheName, 'a webhook');
if (result instanceof DeleteWebhook.Success) {
console.log('successfully deleted webhook');
} else if (result instanceof ListWebhooks.Error) {
throw new Error(
`An error occurred while attempting to delete webhook 'a webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
}

async function example_API_PutWebhook(topicClient: TopicClient, cacheName: string) {
const result = await topicClient.putWebhook(cacheName, 'examples webhook', {
topicName: 'a topic',
destination: 'https://www.thisisawebhookurl.com/v1/webhook',
});
if (result instanceof PutWebhook.Success) {
console.log('successfully created webhook');
} else if (result instanceof PutWebhook.Error) {
throw new Error(
`An error occurred while attempting to create a webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
}

async function example_API_RotateWebhookSecret(topicClient: TopicClient, cacheName: string) {
const result = await topicClient.rotateWebhookSecret(cacheName, 'examples webhook');
if (result instanceof RotateWebhookSecret.Success) {
console.log('successfully rotated the webhook secret');
} else if (result instanceof RotateWebhookSecret.Error) {
throw new Error(
`An error occurred while attempting to rotate the secret for the webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
}

async function example_API_GetWebhookSecret(topicClient: TopicClient, cacheName: string) {
const result = await topicClient.getWebhookSecret(cacheName, 'examples webhook');
if (result instanceof GetWebhookSecret.Success) {
console.log('successfully retrieved the webhook secret');
} else if (result instanceof GetWebhookSecret.Error) {
throw new Error(
`An error occurred while attempting to fetch the secret for the webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
);
}
}
// async function example_API_ListWebhooks(topicClient: TopicClient, cacheName: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to comment out the example code for webhooks since the CI kept failing for it.
Will publish this sdk version and update the tests accordingly in a follow-up pr.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm confused as to why these would fail, the package.json in the examples dir should be pinned to a released version of the SDK. Also it's important for the sake of backward compatibility that this syntax still work, so I think we will need to get to the bottom of it before we merge this.

// const result = await topicClient.listWebhooks(cacheName);
// if (result instanceof ListWebhooks.Success) {
// // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
// console.log(`listed webhooks: ${result.getWebhooks()}`);
// } else if (result instanceof ListWebhooks.Error) {
// throw new Error(
// `An error occurred while attempting to list webhooks for cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
// );
// }
// }

// async function example_API_DeleteWebhook(topicClient: TopicClient, cacheName: string) {
// const result = await topicClient.deleteWebhook(cacheName, 'a webhook');
// if (result instanceof DeleteWebhook.Success) {
// console.log('successfully deleted webhook');
// } else if (result instanceof ListWebhooks.Error) {
// throw new Error(
// `An error occurred while attempting to delete webhook 'a webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
// );
// }
// }

// async function example_API_PutWebhook(topicClient: TopicClient, cacheName: string) {
// const result = await topicClient.putWebhook(cacheName, 'examples webhook', {
// topicName: 'a topic',
// destination: 'https://www.thisisawebhookurl.com/v1/webhook',
// });
// if (result instanceof PutWebhook.Success) {
// console.log('successfully created webhook');
// } else if (result instanceof PutWebhook.Error) {
// throw new Error(
// `An error occurred while attempting to create a webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
// );
// }
// }

// async function example_API_RotateWebhookSecret(topicClient: TopicClient, cacheName: string) {
// const result = await topicClient.rotateWebhookSecret(cacheName, 'examples webhook');
// if (result instanceof RotateWebhookSecret.Success) {
// console.log('successfully rotated the webhook secret');
// } else if (result instanceof RotateWebhookSecret.Error) {
// throw new Error(
// `An error occurred while attempting to rotate the secret for the webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
// );
// }
// }

// async function example_API_GetWebhookSecret(topicClient: TopicClient, cacheName: string) {
// const result = await topicClient.getWebhookSecret(cacheName, 'examples webhook');
// if (result instanceof GetWebhookSecret.Success) {
// console.log('successfully retrieved the webhook secret');
// } else if (result instanceof GetWebhookSecret.Error) {
// throw new Error(
// `An error occurred while attempting to fetch the secret for the webhook 'examples webhook' inside of cache '${cacheName}': ${result.errorCode()}: ${result.toString()}`
// );
// }
// }

function example_API_InstantiateLeaderboardClient() {
new PreviewLeaderboardClient({
Expand Down Expand Up @@ -1909,11 +1909,11 @@
await example_API_TopicSubscribe(topicClient, cacheName);

// Webhooks
await example_API_ListWebhooks(topicClient, cacheName);
await example_API_DeleteWebhook(topicClient, cacheName);
await example_API_PutWebhook(topicClient, cacheName);
await example_API_RotateWebhookSecret(topicClient, cacheName);
await example_API_GetWebhookSecret(topicClient, cacheName);
// await example_API_ListWebhooks(topicClient, cacheName);
// await example_API_DeleteWebhook(topicClient, cacheName);
// await example_API_PutWebhook(topicClient, cacheName);
// await example_API_RotateWebhookSecret(topicClient, cacheName);
// await example_API_GetWebhookSecret(topicClient, cacheName);

example_API_InstantiateLeaderboardClient();
const leaderboardClient = new PreviewLeaderboardClient({
Expand Down
52 changes: 30 additions & 22 deletions examples/nodejs/cache/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/client-sdk-web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading