-
Notifications
You must be signed in to change notification settings - Fork 530
/
Copy pathEndpoint.elm
127 lines (83 loc) · 2.58 KB
/
Endpoint.elm
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
117
118
119
120
121
122
123
124
125
126
127
module Api.Endpoint exposing (Endpoint, article, articles, comment, comments, favorite, feed, follow, login, profiles, request, tags, user, users)
import Article.Slug as Slug exposing (Slug)
import CommentId exposing (CommentId)
import Http
import Url.Builder exposing (QueryParameter)
import Username exposing (Username)
{-| Http.request, except it takes an Endpoint instead of a Url.
-}
request :
{ body : Http.Body
, expect : Http.Expect a
, headers : List Http.Header
, method : String
, timeout : Maybe Float
, url : Endpoint
, withCredentials : Bool
}
-> Http.Request a
request config =
Http.request
{ body = config.body
, expect = config.expect
, headers = config.headers
, method = config.method
, timeout = config.timeout
, url = unwrap config.url
, withCredentials = config.withCredentials
}
-- TYPES
{-| Get a URL to the Conduit API.
This is not publicly exposed, because we want to make sure the only way to get one of these URLs is from this module.
-}
type Endpoint
= Endpoint String
unwrap : Endpoint -> String
unwrap (Endpoint str) =
str
url : List String -> List QueryParameter -> Endpoint
url paths queryParams =
-- NOTE: Url.Builder takes care of percent-encoding special URL characters.
-- See https://package.elm-lang.org/packages/elm/url/latest/Url#percentEncode
Url.Builder.crossOrigin "https://conduit.productionready.io"
("api" :: paths)
queryParams
|> Endpoint
-- ENDPOINTS
login : Endpoint
login =
url [ "users", "login" ] []
user : Endpoint
user =
url [ "user" ] []
users : Endpoint
users =
url [ "users" ] []
follow : Username -> Endpoint
follow uname =
url [ "profiles", Username.toString uname, "follow" ] []
-- ARTICLE ENDPOINTS
article : Slug -> Endpoint
article slug =
url [ "articles", Slug.toString slug ] []
comments : Slug -> Endpoint
comments slug =
url [ "articles", Slug.toString slug, "comments" ] []
comment : Slug -> CommentId -> Endpoint
comment slug commentId =
url [ "articles", Slug.toString slug, "comments", CommentId.toString commentId ] []
favorite : Slug -> Endpoint
favorite slug =
url [ "articles", Slug.toString slug, "favorite" ] []
articles : List QueryParameter -> Endpoint
articles params =
url [ "articles" ] params
profiles : Username -> Endpoint
profiles uname =
url [ "profiles", Username.toString uname ] []
feed : List QueryParameter -> Endpoint
feed params =
url [ "articles", "feed" ] params
tags : Endpoint
tags =
url [ "tags" ] []