Skip to content

Instantly share code, notes, and snippets.

@ohanhi
Created March 26, 2016 15:57
Show Gist options
  • Save ohanhi/c9175609dfd51e89fdda to your computer and use it in GitHub Desktop.
Save ohanhi/c9175609dfd51e89fdda to your computer and use it in GitHub Desktop.
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