diff options
author | Yorhel <git@yorhel.nl> | 2019-12-30 09:49:13 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2019-12-30 09:49:13 +0100 |
commit | d79c982ee876502db2e1a12752667c6a198c2ddc (patch) | |
tree | ccd4a4e72e5b0d3dd412bed6aee58044e93893f0 /elm3/Lib/Api.elm | |
parent | e146ed7bda12d369532e485ca0c2e3d823811854 (diff) |
Actually, let's get rid of v3 now that it doesn't work anymore anyway
Diffstat (limited to 'elm3/Lib/Api.elm')
-rw-r--r-- | elm3/Lib/Api.elm | 110 |
1 files changed, 0 insertions, 110 deletions
diff --git a/elm3/Lib/Api.elm b/elm3/Lib/Api.elm deleted file mode 100644 index c1e0ddcb..00000000 --- a/elm3/Lib/Api.elm +++ /dev/null @@ -1,110 +0,0 @@ -module Lib.Api exposing (..) - -import Json.Encode as JE -import Json.Decode as JD -import File exposing (File) -import Http -import Html exposing (Attribute) -import Html.Events exposing (on) - -import Lib.Gen exposing (ApiResponse(..), decodeApiResponse) - - --- Handy state enum for forms -type State - = Normal - | Loading - | Error Response - - - -type alias Response = ApiResponse - - --- User-friendly error message if the response isn't what the code expected. --- (Technically a good chunk of this function could also be automatically --- generated by ElmGen.pm, but that wouldn't really have all that much value). -showResponse : Response -> String -showResponse res = - let unexp = "Unexpected response, please report a bug." - in case res of - HTTPError (Http.Timeout) -> "Network timeout, please try again later." - HTTPError (Http.NetworkError) -> "Network error, please try again later." - HTTPError (Http.BadStatus r) -> "Server error " ++ String.fromInt r ++ ", please try again later, or report an issue if this persists." - HTTPError (Http.BadBody r) -> "Invalid response from the server, please report a bug (debug info: " ++ r ++")." - HTTPError (Http.BadUrl _) -> unexp - Success -> unexp - CSRF -> "Invalid CSRF token, please refresh the page and try again." - Throttled -> "Action throttled." - Invalid _ -> "Invalid form data, please report a bug." -- This error is already logged server-side, no debug info necessary - Unauth -> "You do not have the permission to perform this action." - BadEmail -> "Unknown email address." - BadLogin -> "Invalid username or password." - BadPass -> "Your chosen password is in a database of leaked passwords, please choose another one." - Bot -> "Invalid answer to the anti-bot question." - Taken -> "Username already taken, please choose a different name." - DoubleEmail -> "Email address already used for another account." - DoubleIP -> "You can only register one account from the same IP within 24 hours." - Unchanged -> "No changes" - Changed _ _ -> unexp - VNResult _ -> unexp - StaffResult _ -> unexp - ProducerResult _ -> unexp - CharResult _ -> unexp - TraitResult _ -> unexp - ReleaseResult _ -> unexp - ImgFormat -> "Unrecognized image format, please upload a JPG or PNG file." - Image _ _ _ -> unexp - Content _ -> unexp - - -expectResponse : (Response -> msg) -> Http.Expect msg -expectResponse msg = - let - res r = msg <| case r of - Err e -> HTTPError e - Ok v -> v - in Http.expectJson res decodeApiResponse - - --- Send a POST request with a JSON body to the VNDB API and get a Response back. -post : String -> JE.Value -> (Response -> msg) -> Cmd msg -post url body msg = - Http.post - { url = url - , body = Http.jsonBody body - , expect = expectResponse msg - } - - - --- Simple image upload API - -type ImageType - = Cv - | Sf - | Ch - - -onFileChange : (List File -> m) -> Attribute m -onFileChange msg = on "change" <| JD.map msg <| JD.at ["target","files"] <| JD.list File.decoder - - --- Upload an image to /js/imageupload.json -postImage : ImageType -> File -> (Response -> msg) -> Cmd msg -postImage ty file msg = - let - tys = case ty of - Cv -> "cv" - Sf -> "sf" - Ch -> "ch" - - body = Http.multipartBody - [ Http.stringPart "type" tys - , Http.filePart "img" file - ] - in Http.post - { url = "/js/imageupload.json" - , body = body - , expect = expectResponse msg - } |