Skip to content

Commit

Permalink
Merge pull request #201 from roc-lang/http
Browse files Browse the repository at this point in the history
Add Http.get, rename HttpError to HttpErr
  • Loading branch information
Anton-4 authored May 7, 2024
2 parents 85f3847 + 8454bfa commit 8532149
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
6 changes: 5 additions & 1 deletion platform/File.roc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module [
write,
readUtf8,
readBytes,
# read, # TODO: investigate the problem with Decoding here
# read, TODO fix "Ability specialization is unknown - code generation cannot proceed!: DeriveError(UnboundVar)"
delete,
isDir,
isFile,
Expand Down Expand Up @@ -143,6 +143,10 @@ readUtf8 : Str -> Task Str [FileReadErr Path ReadErr, FileReadUtf8Err Path _]
readUtf8 = \path ->
Path.readUtf8 (Path.fromStr path)

# read : Str, fmt -> Task contents [FileReadErr Path ReadErr, FileReadDecodingFailed] where contents implements Decoding, fmt implements DecoderFormatting
# read = \path, fmt ->
# Path.read (Path.fromStr path) fmt

## Returns true if the path exists on disk and is pointing at a directory.
## Any error will return false.
## This uses [rust's std::path::is_dir](https://doc.rust-lang.org/std/path/struct.Path.html#method.is_dir).
Expand Down
13 changes: 11 additions & 2 deletions platform/Http.roc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module [
defaultRequest,
errorToString,
send,
get,
]

import Effect
Expand Down Expand Up @@ -103,7 +104,7 @@ errorToString = \err ->
## |> Result.withDefault "Invalid UTF-8"
## |> Stdout.line
## ```
send : Request -> Task Response [HttpError Err]
send : Request -> Task Response [HttpErr Err]
send = \req ->
# TODO: Fix our C ABI codegen so that we don't this Box.box heap allocation
Effect.sendRequest (Box.box req)
Expand All @@ -123,4 +124,12 @@ send = \req ->
headers: meta.headers,
body,
}
|> Task.mapErr HttpError
|> Task.mapErr HttpErr

get : Str, fmt -> Task body [HttpErr Http.Err, HttpDecodingFailed] where body implements Decoding, fmt implements DecoderFormatting
get = \url, fmt ->
response = send! { defaultRequest & url }

Decode.fromBytes response.body fmt
|> Result.mapErr \_ -> HttpDecodingFailed
|> Task.fromResult
18 changes: 9 additions & 9 deletions platform/Path.roc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module [
write,
readUtf8,
readBytes,
# read, # TODO: investigate the problem with Decoding here
# read, TODO fix "Ability specialization is unknown - code generation cannot proceed!: DeriveError(UnboundVar)"
delete,
# These can all be found in Dir as well
listDir,
Expand Down Expand Up @@ -530,14 +530,14 @@ delete : Path -> Task {} [FileWriteErr Path WriteErr]
delete = \path ->
toWriteTask path \bytes -> Effect.fileDelete bytes

# read :
# Path,
# fmt
# -> Task
# Str
# [FileReadErr Path ReadErr, FileReadDecodeErr Path [Leftover (List U8)]Decode.DecodeError ]
# [Read [File]]
# where val implements Decoding, fmt implements DecoderFormatting
# read : Path, fmt -> Task contents [FileReadErr Path ReadErr, FileReadDecodingFailed] where contents implements Decoding, fmt implements DecoderFormatting
# read = \path, fmt ->
# contents = readBytes! path

# Decode.fromBytes contents fmt
# |> Result.mapErr \_ -> FileReadDecodingFailed
# |> Task.fromResult

# read = \path, fmt ->
# effect = Effect.map (Effect.fileReadBytes (InternalPath.toBytes path)) \result ->
# when result is
Expand Down

0 comments on commit 8532149

Please sign in to comment.