Skip to content

Commit 82865cd

Browse files
committed
Update instance name to client
1 parent ccbb5d4 commit 82865cd

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

Diff for: .github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ jobs:
5151

5252
- name: Test
5353
run: |
54-
forge test --ffi -vvv
54+
forge test -vvv
5555
id: test

Diff for: README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ Use builder functions to compose your request with headers, body, and query para
2222

2323
```solidity
2424
contract MyScript is Script {
25-
using HTTP for HTTP.Builder;
26-
using HTTP for HTTP.Request;
25+
using HTTP for *;
2726
28-
HTTP.Builder http;
27+
HTTP.Client http;
2928
3029
function run() external {
31-
HTTP.Response memory response = http.build().POST("https://httpbin.org/post")
30+
HTTP.Response memory response = http.initialize().POST("https://httpbin.org/post")
3231
.withHeader("Content-Type", "application/json")
3332
.withHeader("Accept", "application/json")
3433
.withBody('{"foo": "bar"}')

Diff for: foundry.toml

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
src = "src"
33
out = "out"
44
libs = ["lib"]
5+
ffi = true
56

67
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options

Diff for: src/HTTP.sol

+14-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ library HTTP {
99

1010
Vm constant vm = Vm(address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))));
1111

12-
error HTTPBuilderInvalidArrayLengths(uint256 a, uint256 b);
12+
error HTTPArrayLengthsMismatch(uint256 a, uint256 b);
1313

1414
enum Method {
1515
GET,
@@ -32,17 +32,21 @@ library HTTP {
3232
string data;
3333
}
3434

35-
struct Builder {
35+
struct Client {
3636
Request[] requests;
3737
}
3838

39-
function build(HTTP.Builder storage builder) internal returns (HTTP.Request storage) {
40-
builder.requests.push();
41-
return builder.requests[builder.requests.length - 1];
39+
function initialize(HTTP.Client storage client) internal returns (HTTP.Request storage) {
40+
client.requests.push();
41+
return client.requests[client.requests.length - 1];
4242
}
4343

44-
function instance(HTTP.Builder storage builder) internal view returns (HTTP.Request storage) {
45-
return builder.requests[builder.requests.length - 1];
44+
function initialize(HTTP.Client storage client, string memory url) internal returns (HTTP.Request storage) {
45+
return withUrl(initialize(client), url);
46+
}
47+
48+
function instance(HTTP.Client storage client) internal view returns (HTTP.Request storage) {
49+
return client.requests[client.requests.length - 1];
4650
}
4751

4852
function withUrl(HTTP.Request storage req, string memory url) internal returns (HTTP.Request storage) {
@@ -113,7 +117,7 @@ library HTTP {
113117
returns (HTTP.Request storage)
114118
{
115119
if (keys.length != values.length) {
116-
revert HTTPBuilderInvalidArrayLengths(keys.length, values.length);
120+
revert HTTPArrayLengthsMismatch(keys.length, values.length);
117121
}
118122
for (uint256 i = 0; i < keys.length; i++) {
119123
req.headers.set(keys[i], values[i]);
@@ -134,7 +138,7 @@ library HTTP {
134138
returns (HTTP.Request storage)
135139
{
136140
if (keys.length != values.length) {
137-
revert HTTPBuilderInvalidArrayLengths(keys.length, values.length);
141+
revert HTTPArrayLengthsMismatch(keys.length, values.length);
138142
}
139143
for (uint256 i = 0; i < keys.length; i++) {
140144
req.query.set(keys[i], values[i]);
@@ -183,6 +187,7 @@ library HTTP {
183187
} else if (method == Method.PATCH) {
184188
return "PATCH";
185189
} else {
190+
// unreachable code
186191
revert();
187192
}
188193
}

Diff for: test/HTTP.t.sol

+17-13
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@ import {HTTP} from "../src/HTTP.sol";
77
import {strings} from "solidity-stringutils/strings.sol";
88

99
contract HTTPTest is Test {
10-
using HTTP for HTTP.Builder;
11-
using HTTP for HTTP.Request;
10+
using HTTP for *;
1211
using strings for *;
1312

14-
HTTP.Builder http;
13+
HTTP.Client http;
1514

1615
function test_HTTP_GET() public {
17-
HTTP.Response memory res = http.build().GET("https://jsonplaceholder.typicode.com/todos/1").request();
16+
HTTP.Response memory res = http.initialize().GET("https://jsonplaceholder.typicode.com/todos/1").request();
1817

1918
assertEq(res.status, 200);
2019
assertEq(res.data, '{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}');
2120
}
2221

2322
function test_HTTP_GET_options() public {
24-
HTTP.Response memory res = http.build().GET("https://httpbin.org/headers").withHeader(
23+
HTTP.Response memory res = http.initialize("https://httpbin.org/headers").GET().withHeader(
2524
"accept", "application/json"
2625
).withHeader("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==").request();
2726

@@ -33,7 +32,7 @@ contract HTTPTest is Test {
3332

3433
function test_HTTP_POST_form_data() public {
3534
HTTP.Response memory res =
36-
http.build().POST("https://httpbin.org/post").withBody("[email protected]").request();
35+
http.initialize().POST("https://httpbin.org/post").withBody("[email protected]").request();
3736

3837
assertEq(res.status, 200);
3938

@@ -42,35 +41,40 @@ contract HTTPTest is Test {
4241
}
4342

4443
function test_HTTP_POST_json() public {
45-
HTTP.Response memory res = http.build().POST("https://httpbin.org/post").withBody('{"foo": "bar"}').request();
44+
HTTP.Response memory res =
45+
http.initialize("https://httpbin.org/post").POST().withBody('{"foo": "bar"}').request();
4646

4747
assertEq(res.status, 200);
4848
assertTrue(res.data.toSlice().contains(("foo").toSlice()));
4949
assertTrue(res.data.toSlice().contains(("bar").toSlice()));
5050
}
5151

5252
function test_HTTP_PUT() public {
53-
HTTP.Response memory res = http.build().PUT("https://httpbin.org/put").request();
53+
HTTP.Response memory res = http.initialize().PUT("https://httpbin.org/put").request();
5454
assertEq(res.status, 200);
5555
}
5656

5757
function test_HTTP_PUT_json() public {
58-
HTTP.Response memory res = http.build().PUT("https://httpbin.org/put").withBody('{"foo": "bar"}').withHeader(
59-
"Content-Type", "application/json"
60-
).request();
58+
HTTP.Response memory res = http.initialize("https://httpbin.org/put").PUT().withBody('{"foo": "bar"}')
59+
.withHeader("Content-Type", "application/json").request();
6160

6261
assertEq(res.status, 200);
6362
assertTrue(res.data.toSlice().contains(('"foo"').toSlice()));
6463
assertTrue(res.data.toSlice().contains(('"bar"').toSlice()));
6564
}
6665

6766
function test_HTTP_DELETE() public {
68-
HTTP.Response memory res = http.build().DELETE("https://httpbin.org/delete").request();
67+
HTTP.Response memory res = http.initialize().DELETE("https://httpbin.org/delete").request();
6968
assertEq(res.status, 200);
7069
}
7170

7271
function test_HTTP_PATCH() public {
73-
HTTP.Response memory res = http.build().PATCH("https://httpbin.org/patch").request();
72+
HTTP.Response memory res = http.initialize().PATCH("https://httpbin.org/patch").request();
7473
assertEq(res.status, 200);
7574
}
75+
76+
function test_HTTP_instance() public {
77+
HTTP.Request storage req = http.initialize("https://jsonplaceholder.typicode.com/todos/1");
78+
assertEq(req.url, "https://jsonplaceholder.typicode.com/todos/1");
79+
}
7680
}

0 commit comments

Comments
 (0)