Skip to content

Commit

Permalink
chore: store response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
gnkz committed Sep 20, 2023
1 parent 235ef41 commit bc7ec62
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/_modules/experimental/Request.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {Pointer} from "../Pointer.sol";
import {BytesResult, StringResult, Ok, ResultType, LibResultPointer} from "../Result.sol";
import {LibError, Error} from "../Error.sol";

import {println} from "../../_utils/println.sol";

enum Method {
GET,
POST,
Expand Down Expand Up @@ -44,7 +46,7 @@ type RequestResult is bytes32;
struct Response {
string url;
uint256 status;
Header[] headers;
JsonObject headers;
bytes body;
}

Expand Down Expand Up @@ -237,9 +239,12 @@ library LibRequestBuilder {

CommandOutput memory cmdOutput = result.toValue();

(uint256 status, bytes memory _body, bytes memory _headers) = abi.decode(cmdOutput.stdout, (uint256, bytes, bytes));
(uint256 status, bytes memory _body, bytes memory _headers) =
abi.decode(cmdOutput.stdout, (uint256, bytes, bytes));

return Ok(Response({url: req.url, status: status, body: _body, headers: new Header[](0)}));
return Ok(
Response({url: req.url, status: status, body: _body, headers: jsonModule.create(string(_headers)).unwrap()})
);
}

function build(RequestBuilder memory self) internal pure returns (RequestResult) {
Expand Down Expand Up @@ -317,8 +322,9 @@ library LibRequestBuilder {
library LibRequest {
function toCommand(Request memory self) internal pure returns (Command memory) {
// Adapted from https://github.com/memester-xyz/surl/blob/034c912ae9b5e707a5afd21f145b452ad8e800df/src/Surl.sol#L90
string memory script =
string.concat('response=$(curl -s -w "\\n%{header_json}\\n\\n%{http_code}" ', self.url, " -X ", toString(self.method));
string memory script = string.concat(
'response=$(curl -s -w "\\n%{header_json}\\n\\n%{http_code}" ', self.url, " -X ", toString(self.method)
);

for (uint256 i; i < self.headers.length; i++) {
script = string.concat(script, " -H ", '"', self.headers[i].key, ": ", self.headers[i].value, '"');
Expand Down
1 change: 1 addition & 0 deletions test/_modules/Request.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ contract RequestTest is Test {
// { ... "json": { "foo": "bar" } ... }
expect(res.json().unwrap().getString(".json.foo")).toEqual("bar");
expect(res.status).toEqual(200);
expect(res.headers.getStringArray(".content-type")[0]).toEqual("application/json");
}

function testRequestJsonDecode() external {
Expand Down

0 comments on commit bc7ec62

Please sign in to comment.