all repos

onasty @ d309c75

a one-time notes service

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

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