all repos

onasty @ f6a62ba

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
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)
import Url


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

                Nothing ->
                    ( field, E.null )

        body : E.Value
        body =
            E.object
                [ ( "content", E.string options.content )
                , encodeMaybe options.slug "slug" E.string
                , encodeMaybe options.password "password" E.string
                , ( "burn_before_expiration", E.bool options.burnBeforeExpiration )
                , 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 =
    Effect.sendApiRequest
        { endpoint =
            "/api/v1/note/"
                ++ options.slug
                ++ (case options.password of
                        Just p ->
                            "?password=" ++ Url.percentEncode p

                        Nothing ->
                            ""
                   )
        , 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
        }