-
Notifications
You must be signed in to change notification settings - Fork 100
feat(agent): allow passing identity into all HttpAgent canister requests
#1286
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
Open
hpeebles
wants to merge
14
commits into
dfinity:main
Choose a base branch
from
hpeebles:polling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b25b40e
feat(agent): allow specifying identity within `pollForResponse`
hpeebles b4fe59a
Fix import
hpeebles 266f10d
Update changelog
hpeebles 021b9f2
Update comment
hpeebles d164991
Merge branch 'main' into polling
nikosxenakis e123805
Merge branch 'main' into polling
hpeebles c42375b
Apply same changes to `update`, `createReadStateRequest` and `readSub…
hpeebles 16adc09
Update changelog
hpeebles 4a3334a
Add test
hpeebles e1debe1
Add missing doc comments
hpeebles 7dce850
Use the existing `CallOptions` rather than redefining options
hpeebles efda1e4
Merge branch 'main' into polling
hpeebles e45e0d3
Merge branch 'main' into polling
hpeebles bf39273
Add doc comment
hpeebles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -475,23 +475,12 @@ export class HttpAgent implements Agent { | |
| * Makes a call to a canister method. | ||
| * @param canisterId - The ID of the canister to call. Can be a Principal or a string. | ||
| * @param options - Options for the call. | ||
| * @param options.methodName - The name of the method to call. | ||
| * @param options.arg - The argument to pass to the method, as a Uint8Array. | ||
| * @param options.effectiveCanisterId - (Optional) The effective canister ID, if different from the target canister ID. | ||
| * @param options.callSync - (Optional) Whether to use synchronous call mode. Defaults to true. | ||
| * @param options.nonce - (Optional) A unique nonce for the request. If provided, it will override any nonce set by transforms. | ||
| * @param identity - (Optional) The identity to use for the call. If not provided, the agent's current identity will be used. | ||
| * @returns A promise that resolves to the response of the call, including the request ID and response details. | ||
| */ | ||
| public async call( | ||
| canisterId: Principal | string, | ||
| options: { | ||
| methodName: string; | ||
| arg: Uint8Array; | ||
| effectiveCanisterId?: Principal | string; | ||
| callSync?: boolean; | ||
| nonce?: Uint8Array | Nonce; | ||
| }, | ||
| options: CallOptions, | ||
| identity?: Identity | Promise<Identity>, | ||
| ): Promise<SubmitResponse> { | ||
| const callSync = options.callSync ?? true; | ||
|
|
@@ -652,15 +641,17 @@ export class HttpAgent implements Agent { | |
| * @param canisterId The canister to call. | ||
| * @param fields The call options (method name, arg, effective canister ID, optional nonce). | ||
| * @param pollingOptions Optional polling configuration. | ||
| * @param identity - (Optional) The identity to use. If not provided, the agent's current identity will be used. | ||
| * @returns The certified result including the certificate, reply bytes, and raw certificate bytes. | ||
| */ | ||
| public async update( | ||
| canisterId: Principal | string, | ||
| fields: CallOptions, | ||
| pollingOptions: PollingOptions = {}, | ||
| identity?: Identity | Promise<Identity>, | ||
| ): Promise<UpdateResult> { | ||
| const effectiveCanisterId = Principal.from(fields.effectiveCanisterId); | ||
| const { requestId, response, requestDetails } = await this.call(canisterId, fields); | ||
| const effectiveCanisterId = Principal.from(fields.effectiveCanisterId ?? canisterId); | ||
| const { requestId, response, requestDetails } = await this.call(canisterId, fields, identity); | ||
| const { body, ...httpDetails } = response; | ||
|
|
||
| if (isV4ResponseBody(body)) { | ||
|
|
@@ -686,6 +677,7 @@ export class HttpAgent implements Agent { | |
| effectiveCanisterId, | ||
| requestId, | ||
| pollingOptions, | ||
| identity, | ||
| ); | ||
| return { ...pollResult, requestDetails, callResponse: response }; | ||
| } | ||
|
|
@@ -1023,9 +1015,7 @@ export class HttpAgent implements Agent { | |
| identity?: Identity | Promise<Identity>, | ||
| ): Promise<ApiQueryResponse> { | ||
| const backoff = this.#backoffStrategy(); | ||
| const ecid = fields.effectiveCanisterId | ||
| ? Principal.from(fields.effectiveCanisterId) | ||
| : Principal.from(canisterId); | ||
| const ecid = Principal.from(fields.effectiveCanisterId ?? canisterId); | ||
| await this.#asyncGuard(ecid); | ||
|
|
||
| this.log.print(`ecid ${ecid.toString()}`); | ||
|
|
@@ -1242,7 +1232,7 @@ export class HttpAgent implements Agent { | |
| public async readState( | ||
| canisterId: Principal | string, | ||
| fields: ReadStateOptions, | ||
| _identity?: Identity | Promise<Identity>, | ||
| identity?: Identity | Promise<Identity>, | ||
| // eslint-disable-next-line | ||
| request?: any, | ||
| ): Promise<ReadStateResponse> { | ||
|
|
@@ -1271,10 +1261,6 @@ export class HttpAgent implements Agent { | |
| requestId = getRequestId(fields); | ||
|
|
||
| // Always create a fresh request with the current identity | ||
| const identity = await this.#identity; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to calculate the identity here since that already happens within |
||
| if (!identity) { | ||
| throw ExternalError.fromCode(new IdentityInvalidErrorCode()); | ||
| } | ||
| transformedRequest = await this.createReadStateRequest(fields, identity); | ||
| } | ||
|
|
||
|
|
@@ -1292,14 +1278,15 @@ export class HttpAgent implements Agent { | |
| public async readSubnetState( | ||
| subnetId: Principal | string, | ||
| options: ReadStateOptions, | ||
| identity?: Identity | Promise<Identity>, | ||
| ): Promise<ReadStateResponse> { | ||
| await this.#rootKeyGuard(); | ||
| const subnet = Principal.from(subnetId); | ||
|
|
||
| const url = new URL(`/api/v3/subnet/${subnet.toString()}/read_state`, this.host); | ||
| const transformedRequest: ReadStateRequest = await this.createReadStateRequest( | ||
| options, | ||
| this.#identity ?? undefined, | ||
| identity, | ||
| ); | ||
|
|
||
| return await this.#readStateInner(url, { subnetId: subnet }, transformedRequest); | ||
|
|
||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made this optional to be consistent with
QueryFields