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

http_request_update #1532

Closed
Tracked by #1391
dansteren opened this issue Dec 29, 2023 · 0 comments · Fixed by #1533
Closed
Tracked by #1391

http_request_update #1532

dansteren opened this issue Dec 29, 2023 · 0 comments · Fixed by #1533
Assignees

Comments

@dansteren
Copy link
Contributor

dansteren commented Dec 29, 2023

To perform an http request call the canister needs to expose two endpoints: http_request as a query method, and http_request_update as an update method. The http_request method needs to return an upgrade value of Some(true) and all other fields are ignored.

We already have tests for http_request so it is proposed that this canister's http_request method be hardcoded rather than arbitrary.

The generated canister may look like this:

import {
    Canister,
    HttpRequest,
    HttpResponse,
    nat8,
    None,
    Null,
    query,
    Some,
    update
} from 'azle';

const state: number = 0;

export default Canister({
    http_request: query([HttpRequest], HttpResponse(Null), () => {
        return {
            status_code: 204,
            headers: [],
            body: new Uint8Array(),
            streaming_strategy: None,
            upgrade: Some(true)
        };
    }),
    http_request_update: update(
        [HttpRequest],
        HttpResponse(Null),
        (request) => {
            const expectations = {};
            if (request !== expectations) {
                throw new Error('The http request was different than expected');
            }

            return {
                status_code: 201,
                headers: [
                    ['content-type', 'text/plain'],
                    ['content-encoding', 'gzip']
                ],
                body: Uint8Array.from([
                    31, 139, 8, 0, 55, 2, 27, 98, 0, 3, 43, 45, 72, 73, 44, 73,
                    229, 2, 0, 168, 218, 145, 108, 7, 0, 0, 0
                ]),
                streaming_strategy: None,
                upgrade: None
            };
        }
    ),
    get_state: query([], nat8, () => {
        return state;
    })
});

Then we can hit the http_request endpoint with various http requests with different methods, urls, headers, and bodies, and then update can effect state changes...

So we also need a way to get the state changes back out... so we could use http_request and have it return the state... but then we'd have to control how it decides to do a query vs update... I think it's better to just leave http_request alone and instead have a second method for querying state.

So the test will be, when we make an arbitrary http request:

  • Does the body request param inside of http_request_update decode correctly?
  • Does it return data correctly?
  • Does it update the state?

So tests would look like:

  • get the state before
  • call http_request
  • check that the body is good
  • verify the returned response
  • check the state again

Resources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant