-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
subscription/list
capability (#1088)
Invoking `subscription/list` will retrieve a list of subscriptions for a given account. The list of subscriptions allows us to discover spaces (consumers) that are owned (paid for) by the account. With the list of owned spaces we can make calls to `usage/report` to retrieve and display usage information.
- Loading branch information
Alan Shaw
authored
Nov 9, 2023
1 parent
a411255
commit 471d7e5
Showing
29 changed files
with
492 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import * as Types from './types.js' | ||
import * as Get from './subscription/get.js' | ||
import * as List from './subscription/list.js' | ||
|
||
/** | ||
* @param {Types.SubscriptionServiceContext} context | ||
*/ | ||
export const createService = (context) => ({ | ||
get: Get.provide(context), | ||
list: List.provide(context), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as API from '../types.js' | ||
import * as Server from '@ucanto/server' | ||
import { Subscription } from '@web3-storage/capabilities' | ||
|
||
/** | ||
* @param {API.SubscriptionServiceContext} context | ||
*/ | ||
export const provide = (context) => | ||
Server.provide(Subscription.list, (input) => list(input, context)) | ||
|
||
/** | ||
* @param {API.Input<Subscription.list>} input | ||
* @param {API.SubscriptionServiceContext} context | ||
* @returns {Promise<API.Result<API.SubscriptionListSuccess, API.SubscriptionListFailure>>} | ||
*/ | ||
const list = async ({ capability }, context) => | ||
context.subscriptionsStorage.list(capability.with) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Result } from '@ucanto/interface' | ||
import { | ||
AccountDID, | ||
SubscriptionListSuccess, | ||
SubscriptionListFailure, | ||
} from '@web3-storage/capabilities/types' | ||
|
||
export interface SubscriptionsStorage { | ||
list: ( | ||
customer: AccountDID | ||
) => Promise<Result<SubscriptionListSuccess, SubscriptionListFailure>> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Subscription } from '@web3-storage/capabilities' | ||
import * as API from '../../src/types.js' | ||
import { createServer, connect } from '../../src/lib.js' | ||
import { alice, registerSpace } from '../util.js' | ||
import { createAuthorization } from '../helpers/utils.js' | ||
|
||
/** @type {API.Tests} */ | ||
export const test = { | ||
'subscription/list retrieves subscriptions for account': async ( | ||
assert, | ||
context | ||
) => { | ||
const spaces = await Promise.all([ | ||
registerSpace(alice, context, 'alic_e'), | ||
registerSpace(alice, context, 'alic_e'), | ||
]) | ||
const connection = connect({ | ||
id: context.id, | ||
channel: createServer(context), | ||
}) | ||
|
||
const subListRes = await Subscription.list | ||
.invoke({ | ||
issuer: alice, | ||
audience: context.id, | ||
with: spaces[0].account.did(), | ||
nb: {}, | ||
proofs: await createAuthorization({ | ||
agent: alice, | ||
account: spaces[0].account, | ||
service: context.service, | ||
}), | ||
}) | ||
.execute(connection) | ||
|
||
assert.ok(subListRes.out.ok) | ||
|
||
const results = subListRes.out.ok?.results | ||
const totalConsumers = results?.reduce( | ||
(total, s) => total + s.consumers.length, | ||
0 | ||
) | ||
assert.equal(totalConsumers, spaces.length) | ||
|
||
for (const space of spaces) { | ||
assert.ok(results?.some((s) => s.consumers[0] === space.spaceDid)) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as Subscription from './subscription.js' | ||
import { test } from '../test.js' | ||
test({ 'subscription/*': Subscription.test }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @typedef {import('../../src/types/subscriptions.js').SubscriptionsStorage} SubscriptionsStore | ||
*/ | ||
|
||
/** | ||
* @implements {SubscriptionsStore} | ||
*/ | ||
export class SubscriptionsStorage { | ||
/** @param {import('./provisions-storage.js').ProvisionsStorage} provisions */ | ||
constructor(provisions) { | ||
this.provisionsStore = provisions | ||
} | ||
|
||
/** @param {import('../types.js').AccountDID} customer */ | ||
async list(customer) { | ||
/** @type {import('../types.js').SubscriptionListItem[]} */ | ||
const results = [] | ||
const entries = Object.entries(this.provisionsStore.provisions) | ||
for (const [subscription, provision] of entries) { | ||
if (provision.customer !== customer) continue | ||
results.push({ | ||
subscription, | ||
provider: provision.provider, | ||
consumers: [provision.consumer], | ||
}) | ||
} | ||
return { ok: { results } } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.