| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- module API exposing (getDraft, getDrafts, getSetData, getSets)
- import Database exposing (Database)
- import DraftMeta exposing (DraftMeta)
- import Http
- import Json.Decode as Decode
- import Url.Builder as UrlB
- apiUrl : String
- apiUrl =
- "http://localhost:8000"
- makeApiUrl : List String -> String
- makeApiUrl paths =
- apiUrl ++ "/" ++ String.join "/" paths
- getSets : (Result String (List String) -> msg) -> Cmd msg
- getSets onSuccess =
- Http.get
- { url = makeApiUrl [ "sets" ]
- , expect =
- Http.expectJson (Result.mapError httpErrorToString >> onSuccess)
- (Decode.field "sets"
- (Decode.list Decode.string)
- )
- }
- getSetData : String -> (Result String ( String, Maybe Database ) -> msg) -> Cmd msg
- getSetData setCode onSuccess =
- Http.request
- { method = "GET"
- , headers = []
- , url = makeApiUrl [ "sets", setCode ]
- , body = Http.emptyBody
- , expect = Http.expectJson (Result.mapError httpErrorToString >> onSuccess) Database.decoder
- , timeout = Nothing
- , tracker = Just setCode
- }
- getDrafts : String -> (Result String (List DraftMeta) -> msg) -> Cmd msg
- getDrafts historyUrl onSuccess =
- Http.get
- { url = apiUrl ++ UrlB.absolute [ "drafts" ] [ UrlB.string "history" historyUrl ]
- , expect =
- Http.expectJson (Result.mapError httpErrorToString >> onSuccess)
- (Decode.field "drafts"
- (Decode.list DraftMeta.decoder)
- )
- }
- getDraft : String -> (Result String DraftMeta -> msg) -> Cmd msg
- getDraft draftID onSuccess =
- Http.get
- { url = "https://www.17lands.com" ++ UrlB.absolute [ "data", "draft" ] [ UrlB.string "draft_id" draftID ]
- , expect =
- Http.expectJson (Result.mapError httpErrorToString >> onSuccess) DraftMeta.decoder
- }
- httpErrorToString : Http.Error -> String
- httpErrorToString error =
- case error of
- Http.BadUrl url ->
- "Bad URL: " ++ url
- Http.Timeout ->
- "Timeout"
- Http.NetworkError ->
- "Network error"
- Http.BadStatus status ->
- "Bad status: " ++ String.fromInt status
- Http.BadBody body ->
- "Bad body: " ++ body
|