all repos

onasty @ 11e5f7b2298bcc00428672b91bd45163732ee016

a one-time notes service

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module Api.Note exposing (create, delete, get, getAll, getMetadata)

import Api
import Data.Note as Note exposing (CreateResponse, Metadata, Note)
import Effect exposing (Effect)
import Http
import Iso8601
import Json.Decode as D
import Json.Encode as E
import Time exposing (Posix)


create :
    { onResponse : Result Api.Error CreateResponse -> msg
    , content : String
    , slug : Maybe String
    , password : Maybe String
    , expiresAt : Posix
    , keepBeforeExpiration : Bool
    }
    -> Effect msg
create options =
    let
        encodeMaybe : String -> (a -> E.Value) -> Maybe a -> ( String, E.Value )
        encodeMaybe field value maybe =
            case maybe of
                Just data ->
                    ( field, value data )

                Nothing ->
                    ( field, E.null )

        body =
            E.object
                [ ( "content", E.string options.content )
                , encodeMaybe "slug" E.string options.slug
                , encodeMaybe "password" E.string options.password
                , ( "keep_before_expiration", E.bool options.keepBeforeExpiration )
                , if options.expiresAt == Time.millisToPosix 0 then
                    ( "expires_at", E.null )

                  else
                    ( "expires_at", options.expiresAt |> Iso8601.fromTime |> E.string )
                ]
    in
    Effect.sendApiRequest
        { endpoint = "/api/v1/note"
        , method = "POST"
        , body = Http.jsonBody body
        , onResponse = options.onResponse
        , decoder = Note.decodeCreateResponse
        }


get :
    { onResponse : Result Api.Error Note -> msg
    , slug : String
    , password : Maybe String
    }
    -> Effect msg
get options =
    case options.password of
        Just passwd ->
            Effect.sendApiRequest
                { endpoint = "/api/v1/note/" ++ options.slug ++ "/view"
                , method = "POST"
                , body = E.object [ ( "password", E.string passwd ) ] |> Http.jsonBody
                , onResponse = options.onResponse
                , decoder = Note.decode
                }

        Nothing ->
            Effect.sendApiRequest
                { endpoint = "/api/v1/note/" ++ options.slug
                , method = "GET"
                , body = Http.emptyBody
                , onResponse = options.onResponse
                , decoder = Note.decode
                }


delete : { onResponse : Result Api.Error () -> msg, slug : String } -> Effect msg
delete options =
    Effect.sendApiRequest
        { endpoint = "/api/v1/note/" ++ options.slug
        , method = "DELETE"
        , body = Http.emptyBody
        , onResponse = options.onResponse
        , decoder = D.succeed ()
        }


getMetadata :
    { onResponse : Result Api.Error Metadata -> msg
    , slug : String
    }
    -> Effect msg
getMetadata options =
    Effect.sendApiRequest
        { endpoint = "/api/v1/note/" ++ options.slug ++ "/meta"
        , method = "GET"
        , body = Http.emptyBody
        , onResponse = options.onResponse
        , decoder = Note.decodeMetadata
        }


getAll : { onResponse : Result Api.Error (List Note) -> msg } -> Effect msg
getAll opts =
    Effect.sendApiRequest
        { endpoint = "/api/v1/note"
        , method = "GET"
        , body = Http.emptyBody
        , onResponse = opts.onResponse
        , decoder = D.list Note.decode
        }