Created
May 19, 2016 12:08
-
-
Save ccapndave/941ab9164ecd002c4015be31b28962fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module FileHttp exposing | |
( get | |
, getString | |
) | |
import Http | |
import HttpBuilder exposing (..) | |
import Json.Decode exposing (Decoder) | |
import Task exposing (Task, andThen, onError, mapError, map) | |
{-| Send a GET request to the given URL. You also specify how to decode the | |
response. | |
import Json.Decode (list, string) | |
hats : Task Error (List String) | |
hats = | |
get (list string) "http://example.com/hat-categories.json" | |
Unlike the standard library, an HTTP status code of 0 will *not* result in an error, but will | |
be intercepted and turned into a 200. This allows you to access file:// resources transparently. | |
-} | |
get : Decoder value -> String -> Task Http.Error value | |
get decoder url = | |
let | |
getTask = | |
HttpBuilder.get url | |
|> send (jsonReader decoder) (jsonReader decoder) | |
in | |
makeTask getTask | |
{-| Send a GET request to the given URL. You will get the entire response as a | |
string. | |
hats : Task Error String | |
hats = | |
getString "http://example.com/hat-categories.markdown" | |
Unlike the standard library, an HTTP status code of 0 will *not* result in an error, but will | |
be intercepted and turned into a 200. This allows you to access file:// resources transparently. | |
-} | |
getString : String -> Task Http.Error String | |
getString url = | |
Task.succeed "" | |
{- let | |
getTask = | |
// TODO | |
in | |
makeTask getTask-} | |
{-| Given a task, return a new task that copes with status of 0 | |
-} | |
makeTask : Task (Error r) (Response r) -> Task Http.Error r | |
makeTask task = | |
task | |
`onError` recoverIfStatusZero | |
|> mapError builderErrorToHttpError | |
|> map (\response -> response.data) | |
{-| Turn a Response with status 0 into a Response with status 200 | |
-} | |
recoverIfStatusZero : Error r -> Task (Error r) (Response r) | |
recoverIfStatusZero error = | |
case error of | |
BadResponse response -> | |
if (response.status == 0) then | |
Task.succeed | |
{ data = response.data | |
, status = 200 | |
, statusText = response.statusText | |
, headers = response.headers | |
, url = response.url | |
} | |
else | |
Task.fail error | |
_ -> | |
Task.fail error | |
{-| Convert a HttpBuilder.Error into and Http.Error so that the rest of the application doesn't need to | |
know that anything has changed. | |
-} | |
builderErrorToHttpError : Error a -> Http.Error | |
builderErrorToHttpError error = | |
case error of | |
UnexpectedPayload str -> | |
Http.UnexpectedPayload str | |
NetworkError -> | |
Http.NetworkError | |
Timeout -> | |
Http.Timeout | |
BadResponse response -> | |
Http.BadResponse response.status response.statusText |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment