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

Add read_bytes_body for downloading images, etc #7

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ffi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ function make_headers(headersList) {
return headers;
}

export async function read_bytes_body(response) {
let body;
try {
body = await response.body.text();
} catch (error) {
return new Error(new UnableToReadBody());
}
return new Ok(response.withFields({ body }));
}

export async function read_text_body(response) {
let body;
try {
Expand Down
5 changes: 5 additions & 0 deletions src/gleam/fetch.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ pub fn to_fetch_request(a: Request(String)) -> FetchRequest
@external(javascript, "../ffi.mjs", "from_fetch_response")
pub fn from_fetch_response(a: FetchResponse) -> Response(FetchBody)

@external(javascript, "../ffi.mjs", "read_bytes_body")
pub fn read_bytes_body(
a: Response(FetchBody),
) -> Promise(Result(Response(BitArray), FetchError))

@external(javascript, "../ffi.mjs", "read_text_body")
pub fn read_text_body(
a: Response(FetchBody),
Expand Down
20 changes: 20 additions & 0 deletions test/gleam_fetch_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ pub fn request_test() {
|> request.set_path("/hello/world")
|> request.prepend_header("accept", "application/vnd.hmrc.1.0+json")

fetch.send(req)
|> promise.try_await(fetch.read_bytes_body)
|> promise.await(fn(resp: Result(Response(BitArray), FetchError)) {
let assert Ok(resp) = resp
let assert 200 = resp.status
let assert Ok("application/json") =
response.get_header(resp, "content-type")
// TODO let assert <<123, 34, 109, 101, 115, 115, 97, 103, 101, 34, 58, 34, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 34, 125>> = resp.body
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still WIP?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah-rgh...
extra commit coming up...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wish I could've used <<"...":utf8>> but String as part of bit syntax not allowed in JS ?

promise.resolve(Ok(Nil))
})
}

pub fn text_request_test() {
let req =
request.new()
|> request.set_method(Get)
|> request.set_host("test-api.service.hmrc.gov.uk")
|> request.set_path("/hello/world")
|> request.prepend_header("accept", "application/vnd.hmrc.1.0+json")

fetch.send(req)
|> promise.try_await(fetch.read_text_body)
|> promise.await(fn(resp: Result(Response(String), FetchError)) {
Expand Down