Skip to content

Instantly share code, notes, and snippets.

@ohanhi
Created March 26, 2016 15:57
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Example abstraction layer on top of elm-http-extra
module JsonApi where
import Task exposing (Task)
import Json.Decode exposing (Decoder)
import Json.Encode exposing (Value)
import Http.Extra as HttpExtra exposing (..)
import Task exposing (Task)
import Json.Decode exposing (Decoder)
import Json.Encode exposing (Value)
import Http.Extra as HttpExtra exposing (..)
apiCall : (String -> RequestBuilder) -> Decoder success -> Decoder error -> String -> Value -> Task (Error error) (Response success)
apiCall useMethod successDecoder errorDecoder url body =
url
|> useMethod
|> withHeader "content-type" "application/json"
|> withJsonBody body
|> send (jsonReader successDecoder) (jsonReader errorDecoder)
get : Decoder success -> Decoder error -> String -> Value -> Task (Error error) (Response success)
get =
apiCall HttpExtra.get
post : Decoder success -> Decoder error -> String -> Value -> Task (Error error) (Response success)
post =
apiCall HttpExtra.post
put : Decoder success -> Decoder error -> String -> Value -> Task (Error error) (Response success)
put =
apiCall HttpExtra.put
patch : Decoder success -> Decoder error -> String -> Value -> Task (Error error) (Response success)
patch =
apiCall HttpExtra.patch
delete : Decoder success -> Decoder error -> String -> Value -> Task (Error error) (Response success)
delete =
apiCall HttpExtra.delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment