Make BackendTask.Http.get take an Expect, add Http.getJson and Http.post helpers.

This commit is contained in:
Dillon Kearns 2023-01-07 10:38:52 -08:00
parent 2ffea79f0b
commit ef0f091790
3 changed files with 80 additions and 38 deletions

File diff suppressed because one or more lines are too long

View File

@ -10,7 +10,7 @@ import Test exposing (Test)
all : BackendTask error Test
all =
[ BackendTask.Http.get "http://httpstat.us/500" (Decode.succeed ())
[ BackendTask.Http.get "http://httpstat.us/500" (BackendTask.Http.expectWhatever ())
|> test "http 500 error"
(\result ->
case result of
@ -26,7 +26,7 @@ all =
Ok () ->
Expect.fail "Expected HTTP error, got Ok"
)
, BackendTask.Http.get "http://httpstat.us/404" (Decode.succeed ())
, BackendTask.Http.get "http://httpstat.us/404" (BackendTask.Http.expectWhatever ())
|> test "http 404 error"
(\result ->
case result of
@ -42,7 +42,7 @@ all =
Ok () ->
Expect.fail "Expected HTTP error, got Ok"
)
, BackendTask.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" (Decode.field "stargazers_count" Decode.int)
, BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages" (Decode.field "stargazers_count" Decode.int)
|> test "200 JSON"
(\result ->
case result of
@ -52,7 +52,7 @@ all =
Ok count ->
Expect.pass
)
, BackendTask.Http.get "https://api.github.com/repos/dillonkearns/elm-pages" (Decode.field "this-field-doesn't-exist" Decode.int)
, BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages" (Decode.field "this-field-doesn't-exist" Decode.int)
|> test "JSON decoding error"
(\result ->
case result of
@ -63,15 +63,17 @@ all =
_ ->
Expect.fail ("Expected BadStatus, got: " ++ Debug.toString result)
)
, BackendTask.Http.requestWithOptions
, BackendTask.Http.request
{ url = "https://api.github.com/repos/dillonkearns/elm-pages"
, method = "GET"
, headers = []
, body = BackendTask.Http.emptyBody
}
{ cacheStrategy = BackendTask.Http.IgnoreCache
, retries = 0
, timeoutInMs = Nothing
, options =
Just
{ cacheStrategy = BackendTask.Http.IgnoreCache
, retries = 0
, timeoutInMs = Nothing
}
}
(BackendTask.Http.expectJson
(Decode.field "this-field-doesn't-exist" Decode.int)
@ -86,15 +88,17 @@ all =
_ ->
Expect.fail ("Expected BadStatus, got: " ++ Debug.toString result)
)
, BackendTask.Http.requestWithOptions
, BackendTask.Http.request
{ url = "https://api.github.com/repos/dillonkearns/elm-pages"
, method = "GET"
, headers = []
, body = BackendTask.Http.emptyBody
}
{ cacheStrategy = BackendTask.Http.ForceRevalidate
, retries = 0
, timeoutInMs = Nothing
, options =
Just
{ cacheStrategy = BackendTask.Http.ForceRevalidate
, retries = 0
, timeoutInMs = Nothing
}
}
(BackendTask.Http.withMetadata Tuple.pair
(BackendTask.Http.expectJson

View File

@ -1,9 +1,10 @@
module BackendTask.Http exposing
( RequestDetails
, get, request
( get, getJson
, post
, Expect, expectString, expectJson, expectBytes, expectWhatever
, withMetadata, Metadata
, Error(..)
, RequestDetails, request
, withMetadata, Metadata
, Body, emptyBody, stringBody, jsonBody
, CacheStrategy(..), Options
)
@ -35,8 +36,12 @@ in [this article introducing BackendTask.Http requests and some concepts around
- Data that is specific to the logged-in user
- Data that needs to be the very latest and changes often (for example, sports scores)
@docs RequestDetails
@docs get, request
## Making a Request
@docs get, getJson
@docs post
## Decoding Request Body
@ -44,16 +49,21 @@ in [this article introducing BackendTask.Http requests and some concepts around
@docs Expect, expectString, expectJson, expectBytes, expectWhatever
## Error Handling
@docs Error
## General Requests
@docs RequestDetails, request
## With Metadata
@docs withMetadata, Metadata
## Errors
@docs Error
## Building a BackendTask.Http Request Body
The way you build a body is analogous to the `elm/http` package. Currently, only `emptyBody` and
@ -128,26 +138,54 @@ type alias Body =
(Decode.field "stargazers_count" Decode.int)
-}
get :
getJson :
String
-> Json.Decode.Decoder a
-> BackendTask (Catchable Error) a
get url decoder =
getJson url decoder =
request
((\okUrl ->
-- wrap in new variant
{ url = okUrl
, method = "GET"
, headers = []
, body = emptyBody
, options = Nothing
}
)
url
)
{ url = url
, method = "GET"
, headers = []
, body = emptyBody
, options = Nothing
}
(expectJson decoder)
{-| -}
get :
String
-> Expect a
-> BackendTask (Catchable Error) a
get url expect =
request
{ url = url
, method = "GET"
, headers = []
, body = emptyBody
, options = Nothing
}
expect
{-| -}
post :
String
-> Body
-> Expect a
-> BackendTask (Catchable Error) a
post url body expect =
request
{ url = url
, method = "POST"
, headers = []
, body = body
, options = Nothing
}
expect
{-| The full details to perform a BackendTask.Http request.
-}
type alias RequestDetails =