all repos

onasty @ cbf53ca

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
module Api.Note exposing (create, get, getMetadata)

import Api
import Data.Note as Note exposing (CreateResponse, Metadata, Note)
import Effect exposing (Effect)
import Http
import Iso8601
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
                }


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
        }