all repos

onasty @ 62e4dde75f5cca100c807673176826e5f120c86d

a one-time notes service

onasty/web/src/Api/Note.elm (view raw)

Smirnov Olexandr Smirnov Olexandr
ss2316544@gmail.com
web: show note (#147)..., 11 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
import Url
11
12
13
create :
14
    { onResponse : Result Api.Error CreateResponse -> msg
15
    , content : String
16
    , slug : Maybe String
17
    , password : Maybe String
18
    , expiresAt : Posix
19
    , burnBeforeExpiration : Bool
20
    }
21
    -> Effect msg
22
create options =
23
    let
24
        encodeMaybe : Maybe a -> b -> (a -> E.Value) -> ( b, E.Value )
25
        encodeMaybe maybeData field value =
26
            case maybeData of
27
                Just data ->
28
                    ( field, value data )
29
30
                Nothing ->
31
                    ( field, E.null )
32
33
        body : E.Value
34
        body =
35
            E.object
36
                [ ( "content", E.string options.content )
37
                , encodeMaybe options.slug "slug" E.string
38
                , encodeMaybe options.password "password" E.string
39
                , ( "burn_before_expiration", E.bool options.burnBeforeExpiration )
40
                , if options.expiresAt == Time.millisToPosix 0 then
41
                    ( "expires_at", E.null )
42
43
                  else
44
                    ( "expires_at"
45
                    , options.expiresAt
46
                        |> ISO8601.fromPosix
47
                        |> ISO8601.toString
48
                        |> E.string
49
                    )
50
                ]
51
    in
52
    Effect.sendApiRequest
53
        { endpoint = "/api/v1/note"
54
        , method = "POST"
55
        , body = Http.jsonBody body
56
        , onResponse = options.onResponse
57
        , decoder = Note.decodeCreateResponse
58
        }
59
60
61
get :
62
    { onResponse : Result Api.Error Note -> msg
63
    , slug : String
64
    , password : Maybe String
65
    }
66
    -> Effect msg
67
get options =
68
    Effect.sendApiRequest
69
        { endpoint =
70
            "/api/v1/note/"
71
                ++ options.slug
72
                ++ (case options.password of
73
                        Just p ->
74
                            "?password=" ++ Url.percentEncode p
75
76
                        Nothing ->
77
                            ""
78
                   )
79
        , method = "GET"
80
        , body = Http.emptyBody
81
        , onResponse = options.onResponse
82
        , decoder = Note.decode
83
        }
84
85
86
getMetadata :
87
    { onResponse : Result Api.Error Metadata -> msg
88
    , slug : String
89
    }
90
    -> Effect msg
91
getMetadata options =
92
    Effect.sendApiRequest
93
        { endpoint = "/api/v1/note/" ++ options.slug ++ "/meta"
94
        , method = "GET"
95
        , body = Http.emptyBody
96
        , onResponse = options.onResponse
97
        , decoder = Note.decodeMetadata
98
        }