-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: update docs to reflect usage of results (#207)
* docs: update docs to reflect usage of results * chore: update installation instructions * docs: add example request * docs: fix request example * docs: update summary * docs: update result docs * docs: update results example * docs: update requests description * chore: remove example request test * docs: update fe docs * Update docs/src/modules/requests.md Co-authored-by: V <[email protected]> --------- Co-authored-by: V <[email protected]>
- Loading branch information
Showing
12 changed files
with
108 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Requests | ||
|
||
The request module is inspired by Rust's `reqwest` crate. It provides a flexible API to interact | ||
with external web services allowing to work with request headers, request authorization, response | ||
headers, parsing a response body as JSON and others. | ||
|
||
```solidity | ||
import { Test, request, RequestClient, ResponseResult, Response, JsonObject } from "vulcan/test.sol"; | ||
contract HttpRequests is Test { | ||
function getFoo() external returns (string memory) { | ||
// Create a request client | ||
RequestClient memory client = request.create().defaultHeader("Content-Type", "application/json"); | ||
// Send a GET request | ||
ResponseResult responseResult = client.get("https://httpbin.org/get?foo=bar").send(); | ||
// Check if it's an error | ||
if (responseResult.isError()) { | ||
revert("Request to httpbin.org failed"); | ||
} | ||
// Get the response from the response result | ||
Response memory response = responseResult.toValue(); | ||
// Check status code | ||
if (response.status == 401) { | ||
revert("Request to httpbin.org is Unauthorized"); | ||
} | ||
// Get the response body as a JsonObject | ||
JsonObject memory jsonBody = response.json().unwrap(); | ||
// Get the value of `foo` from the json body response from httpbin | ||
string memory foo = jsonBody.getString(".args.foo"); | ||
return foo; | ||
} | ||
} | ||
``` |
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,40 @@ | ||
# Results | ||
|
||
The concept of "Results" is inspired by Rust. It centers around using specific types for returning values while also offering mechanisms to handle any potential errors. | ||
|
||
Similar to Rust's Results API, Vulcan implements the following functions for all result types: | ||
- `unwrap()`: if the Result is an Error, reverts with the default error message. If if is `Ok`, it returns the underlying value | ||
- `expect(message)`: same as `unwrap()`, but reverts with the provided error message | ||
- `isError()`: returns `true` if the `Result` is an error, `false` otherwise | ||
- `isOk()`: the oposite of `isError()` | ||
- `toError()`: transforms the Result into an `Error` | ||
- `toValue()`: gets the Result's underlying value (if the Result is Ok) | ||
|
||
```solidity | ||
import { Test, StringResult, Ok, console, CommandResult, Error } from "vulcan/test.sol"; | ||
contract TestMyContract is Test { | ||
function testMyContract() external { | ||
CommandResult result = commands.run(["echo", "Hello, world!"]); | ||
// We can handle different types of errors | ||
if (result.isError()) { | ||
Error err = result.toError(); | ||
if (err.matches(CommandError.NotExecuted)) { | ||
revert("Something weird happened!"); | ||
} else { | ||
revert("Unknown error"); | ||
} | ||
// Or we could immediately get the actual output, reverting if the command failed to run | ||
bytes memory out = result.expect("wtf echo failed!").stdout; | ||
// Another way is to check if the result is ok | ||
if (result.isOk()) { | ||
// We know the result is ok so we can access the underlying value | ||
out = result.toValue().stdout; | ||
} | ||
} | ||
} | ||
``` |
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