about summary refs log tree commit diff
path: root/assessments/tt/client/src/Utils.elm
blob: 60343cd870188600ce649e506e08c6cb006a27b1 (plain) (blame)
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
module Utils exposing (..)

import DateFormat
import Http
import Time
import Shared


explainHttpError : Http.Error -> String
explainHttpError e =
    case e of
        Http.BadUrl _ ->
            "Bad URL: you may have supplied an improperly formatted URL"

        Http.Timeout ->
            "Timeout: the resource you requested did not arrive within the interval of time that you claimed it should"

        Http.BadStatus s ->
            "Bad Status: the server returned a bad status code: " ++ String.fromInt s

        Http.BadBody b ->
            "Bad Body: our application had trouble decoding the body of the response from the server: " ++ b

        Http.NetworkError ->
            "Network Error: something went awry in the network stack. I recommend checking the server logs if you can."


getWithCredentials :
    { url : String
    , expect : Http.Expect msg
    }
    -> Cmd msg
getWithCredentials { url, expect } =
    Http.riskyRequest
        { url = url
        , headers = [ Http.header "Origin" Shared.clientOrigin ]
        , method = "GET"
        , timeout = Nothing
        , tracker = Nothing
        , body = Http.emptyBody
        , expect = expect
        }


postWithCredentials :
    { url : String
    , body : Http.Body
    , expect : Http.Expect msg
    }
    -> Cmd msg
postWithCredentials { url, body, expect } =
    Http.riskyRequest
        { url = url
        , headers = [ Http.header "Origin" Shared.clientOrigin ]
        , method = "POST"
        , timeout = Nothing
        , tracker = Nothing
        , body = body
        , expect = expect
        }


deleteWithCredentials :
    { url : String
    , body : Http.Body
    , expect : Http.Expect msg
    }
    -> Cmd msg
deleteWithCredentials { url, body, expect } =
    Http.riskyRequest
        { url = url
        , headers = [ Http.header "Origin" Shared.clientOrigin ]
        , method = "DELETE"
        , timeout = Nothing
        , tracker = Nothing
        , body = body
        , expect = expect
        }

putWithCredentials :
    { url : String
    , body : Http.Body
    , expect : Http.Expect msg
    }
    -> Cmd msg
putWithCredentials { url, body, expect } =
    Http.riskyRequest
        { url = url
        , headers = [ Http.header "Origin" Shared.clientOrigin ]
        , method = "PUT"
        , timeout = Nothing
        , tracker = Nothing
        , body = body
        , expect = expect
        }



formatTime : Time.Posix -> String
formatTime ts =
    DateFormat.format
        [ DateFormat.monthNameFull
        , DateFormat.text " "
        , DateFormat.dayOfMonthSuffix
        , DateFormat.text ", "
        , DateFormat.yearNumber
        ]
        Time.utc
        ts