Skip to content

Commit

Permalink
refactor: change how headers are represented (#22)
Browse files Browse the repository at this point in the history
* refactor: change how headers are represented

* chore: formatting
  • Loading branch information
lessp authored Nov 15, 2019
1 parent 81e37fe commit 743a195
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
5 changes: 3 additions & 2 deletions examples/fetch_native_lwt_get.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ Fetch.(
fetch("https://httpbin.org/get")
|> Lwt.map(
fun
| Ok({Response.body, status, url, _}) => {
| Ok({Response.body, status, url, headers}) => {
Printf.printf(
"Status-Code: %d\nBody: %s\nUrl: %s",
"Headers: \n%sStatus-Code: %d\nBody: %s\nUrl: %s",
Response.Headers.toString(headers),
Response.Status.toCode(status),
Response.Body.toString(body),
url,
Expand Down
20 changes: 17 additions & 3 deletions src/fetch-core/src/Headers.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
type name = string;
type value = string;
type t = (string, string);

type t = (name, value);
let toString = headers => {
let buffer = Buffer.create(128);

List.iter(
((name, value)) => {
Buffer.add_string(buffer, name);
Buffer.add_string(buffer, ": ");
Buffer.add_string(buffer, value);
Buffer.add_string(buffer, "\r\n");
},
headers,
);

Buffer.add_string(buffer, "\r\n");
Buffer.contents(buffer);
};
5 changes: 2 additions & 3 deletions src/fetch-core/src/Headers.rei
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
type name = string;
type value = string;
type t = (string, string);

type t = (name, value);
let toString: list(t) => string;
1 change: 1 addition & 0 deletions src/fetch-core/src/S.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module type Body = {
module type Response = {
module Status: (module type of Status);
module Body: Body;
module Headers: (module type of Headers);

type t = {
body: Body.t,
Expand Down

0 comments on commit 743a195

Please sign in to comment.