adds type variable for Api.Response value field
This commit is contained in:
89
src/Api.elm
89
src/Api.elm
@@ -8,6 +8,7 @@ module Api exposing
|
||||
, Msg(..)
|
||||
, Player
|
||||
, RequestData(..)
|
||||
, ToChest(..)
|
||||
, Update(..)
|
||||
, Wealth
|
||||
, blankPlayer
|
||||
@@ -31,13 +32,8 @@ type alias HttpResult a =
|
||||
-- Format of the server's response
|
||||
|
||||
|
||||
type Value
|
||||
= Text String
|
||||
| Items Loot
|
||||
|
||||
|
||||
type alias Response =
|
||||
{ value : Maybe Value
|
||||
type alias Response a =
|
||||
{ value : Maybe a
|
||||
, notification : Maybe String
|
||||
, updates : Maybe (List Update)
|
||||
, errors : Maybe String
|
||||
@@ -53,7 +49,7 @@ type Update
|
||||
|
||||
|
||||
type Msg
|
||||
= GotActionResult (HttpResult Response)
|
||||
= GotActionResult (HttpResult (Response ()))
|
||||
|
||||
|
||||
|
||||
@@ -186,25 +182,47 @@ lootDecoder =
|
||||
D.list itemDecoder
|
||||
|
||||
|
||||
fetchLoot : String -> (Result Http.Error Loot -> msg) -> Cmd msg
|
||||
fetchLoot url toMsg =
|
||||
type ToChest
|
||||
= OfPlayer Int
|
||||
| OfGroup
|
||||
| OfShop
|
||||
|
||||
|
||||
fetchLoot : (ToChest -> Result Http.Error Loot -> msg) -> ToChest -> Cmd msg
|
||||
fetchLoot toMsg dest =
|
||||
let
|
||||
url =
|
||||
case dest of
|
||||
OfPlayer id ->
|
||||
"http://localhost:8088/api/players/" ++ String.fromInt id ++ "/loot"
|
||||
|
||||
OfShop ->
|
||||
"http://localhost:8088/api/items"
|
||||
|
||||
OfGroup ->
|
||||
"http://localhost:8088/api/players/0/loot"
|
||||
in
|
||||
Http.get
|
||||
{ url = url
|
||||
, expect = Http.expectJson toMsg (valueDecoder lootDecoder)
|
||||
, expect = Http.expectJson (toMsg dest) (valueDecoder lootDecoder)
|
||||
}
|
||||
|
||||
|
||||
|
||||
-- Retrieves items from a list of names
|
||||
|
||||
|
||||
checkList : (Loot -> Maybe String -> msg) -> List String -> Cmd msg
|
||||
checkList toMsg itemList =
|
||||
let
|
||||
parseResponse : Result Http.Error Response -> msg
|
||||
parseResponse : Result Http.Error (Response Loot) -> msg
|
||||
parseResponse response =
|
||||
case response of
|
||||
Ok r ->
|
||||
let
|
||||
items =
|
||||
case r.value of
|
||||
Just (Items loot) ->
|
||||
Just loot ->
|
||||
loot
|
||||
|
||||
_ ->
|
||||
@@ -237,9 +255,13 @@ checkList toMsg itemList =
|
||||
|
||||
|
||||
|
||||
-- CLAIMS
|
||||
-- API Response
|
||||
-- API RESPONSE
|
||||
--
|
||||
-- Loot-a-lot API use a flat response format with four fields :
|
||||
-- * value
|
||||
-- * notification
|
||||
-- * updates
|
||||
-- * errors
|
||||
|
||||
|
||||
valueDecoder : Decoder a -> Decoder a
|
||||
@@ -266,10 +288,13 @@ updatesDecoder =
|
||||
|
||||
|
||||
|
||||
-- TODO: add a valueDecoder to get a proper value out of Json
|
||||
-- The 'value' field is actually a union-type
|
||||
-- with heterogeneous data. We need to provide a
|
||||
-- decoder to extract the value we expect, or ignore
|
||||
-- it with ().
|
||||
|
||||
|
||||
apiResponseDecoder : Decoder Value -> Decoder Response
|
||||
apiResponseDecoder : Decoder a -> Decoder (Response a)
|
||||
apiResponseDecoder toValue =
|
||||
D.map4 Response
|
||||
(D.maybe (field "value" toValue))
|
||||
@@ -278,18 +303,6 @@ apiResponseDecoder toValue =
|
||||
(D.maybe (field "errors" string))
|
||||
|
||||
|
||||
undoLastAction id =
|
||||
Http.request
|
||||
{ url = "http://localhost:8088/api/players/" ++ String.fromInt id ++ "/events/last"
|
||||
, method = "DELETE"
|
||||
, headers = []
|
||||
, body = Http.emptyBody
|
||||
, expect = Http.expectJson GotActionResult (apiResponseDecoder string)
|
||||
, timeout = Nothing
|
||||
, tracker = Nothing
|
||||
}
|
||||
|
||||
|
||||
|
||||
{- ACTIONS
|
||||
|
||||
@@ -370,12 +383,28 @@ confirmAction id data =
|
||||
, headers = []
|
||||
, url = endpoint
|
||||
, body = Http.jsonBody <| buildPayload data
|
||||
, expect = Http.expectJson GotActionResult (apiResponseDecoder string)
|
||||
, expect = Http.expectJson GotActionResult (apiResponseDecoder <| D.succeed ())
|
||||
, timeout = Nothing
|
||||
, tracker = Nothing
|
||||
}
|
||||
|
||||
|
||||
undoLastAction id =
|
||||
Http.request
|
||||
{ url = "http://localhost:8088/api/players/" ++ String.fromInt id ++ "/events/last"
|
||||
, method = "DELETE"
|
||||
, headers = []
|
||||
, body = Http.emptyBody
|
||||
, expect = Http.expectJson GotActionResult (apiResponseDecoder <| D.succeed ())
|
||||
, timeout = Nothing
|
||||
, tracker = Nothing
|
||||
}
|
||||
|
||||
|
||||
|
||||
-- UTILS
|
||||
|
||||
|
||||
printError : Http.Error -> String
|
||||
printError error =
|
||||
case error of
|
||||
|
||||
Reference in New Issue
Block a user