onasty/web/src/Api/Note.elm (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com refactor!: rename "burn before expiration" to "keep before expiration" (#199)..., 9 months ago
olexsmir@gmail.com refactor!: rename "burn before expiration" to "keep before expiration" (#199)..., 9 months ago
| 1 | module Api.Note exposing (create, get, getMetadata) |
| 2 | |
| 3 | import Api |
| 4 | import Data.Note as Note exposing (CreateResponse, Metadata, Note) |
| 5 | import Effect exposing (Effect) |
| 6 | import Http |
| 7 | import Iso8601 |
| 8 | import Json.Encode as E |
| 9 | import Time exposing (Posix) |
| 10 | |
| 11 | |
| 12 | create : |
| 13 | { onResponse : Result Api.Error CreateResponse -> msg |
| 14 | , content : String |
| 15 | , slug : Maybe String |
| 16 | , password : Maybe String |
| 17 | , expiresAt : Posix |
| 18 | , keepBeforeExpiration : Bool |
| 19 | } |
| 20 | -> Effect msg |
| 21 | create options = |
| 22 | let |
| 23 | encodeMaybe : String -> (a -> E.Value) -> Maybe a -> ( String, E.Value ) |
| 24 | encodeMaybe field value maybe = |
| 25 | case maybe of |
| 26 | Just data -> |
| 27 | ( field, value data ) |
| 28 | |
| 29 | Nothing -> |
| 30 | ( field, E.null ) |
| 31 | |
| 32 | body = |
| 33 | E.object |
| 34 | [ ( "content", E.string options.content ) |
| 35 | , encodeMaybe "slug" E.string options.slug |
| 36 | , encodeMaybe "password" E.string options.password |
| 37 | , ( "keep_before_expiration", E.bool options.keepBeforeExpiration ) |
| 38 | , if options.expiresAt == Time.millisToPosix 0 then |
| 39 | ( "expires_at", E.null ) |
| 40 | |
| 41 | else |
| 42 | ( "expires_at", options.expiresAt |> Iso8601.fromTime |> E.string ) |
| 43 | ] |
| 44 | in |
| 45 | Effect.sendApiRequest |
| 46 | { endpoint = "/api/v1/note" |
| 47 | , method = "POST" |
| 48 | , body = Http.jsonBody body |
| 49 | , onResponse = options.onResponse |
| 50 | , decoder = Note.decodeCreateResponse |
| 51 | } |
| 52 | |
| 53 | |
| 54 | get : |
| 55 | { onResponse : Result Api.Error Note -> msg |
| 56 | , slug : String |
| 57 | , password : Maybe String |
| 58 | } |
| 59 | -> Effect msg |
| 60 | get options = |
| 61 | case options.password of |
| 62 | Just passwd -> |
| 63 | Effect.sendApiRequest |
| 64 | { endpoint = "/api/v1/note/" ++ options.slug ++ "/view" |
| 65 | , method = "POST" |
| 66 | , body = E.object [ ( "password", E.string passwd ) ] |> Http.jsonBody |
| 67 | , onResponse = options.onResponse |
| 68 | , decoder = Note.decode |
| 69 | } |
| 70 | |
| 71 | Nothing -> |
| 72 | Effect.sendApiRequest |
| 73 | { endpoint = "/api/v1/note/" ++ options.slug |
| 74 | , method = "GET" |
| 75 | , body = Http.emptyBody |
| 76 | , onResponse = options.onResponse |
| 77 | , decoder = Note.decode |
| 78 | } |
| 79 | |
| 80 | |
| 81 | getMetadata : |
| 82 | { onResponse : Result Api.Error Metadata -> msg |
| 83 | , slug : String |
| 84 | } |
| 85 | -> Effect msg |
| 86 | getMetadata options = |
| 87 | Effect.sendApiRequest |
| 88 | { endpoint = "/api/v1/note/" ++ options.slug ++ "/meta" |
| 89 | , method = "GET" |
| 90 | , body = Http.emptyBody |
| 91 | , onResponse = options.onResponse |
| 92 | , decoder = Note.decodeMetadata |
| 93 | } |