/
Urls.scala
392 lines (347 loc) · 13.7 KB
/
Urls.scala
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package endpoints4s.algebra
import java.util.UUID
import endpoints4s.{Codec, PartialInvariantFunctor, PartialInvariantFunctorSyntax, Tupler}
import scala.collection.compat.Factory
/** Algebra interface for describing URLs made of a path and a query string.
*
* A path is itself made of segments chained together.
*
* A query string is made of named parameters.
*
* {{{
* /**
* * Describes an URL starting with a segment containing “articles”, followed
* * by another `String` segment, and a query string containing
* * a mandatory `Lang` parameter named “lang”, and an
* * optional `Int` parameter named “page”.
* *
* * Examples of matching URLs:
* *
* * - /articles/kitchen?lang=fr
* * - /articles/garden?lang=en&page=2
* */
* val example = path / "articles" / segment[String]() /? (qs[Lang]("lang") & qs[Option[Int]]("page"))
* }}}
*
* @group algebras
* @groupname types Types
* @groupdesc types Types introduced by the algebra
* @groupprio types 1
* @groupname operations Operations
* @groupdesc operations Operations creating and transforming values
* @groupprio operations 2
*/
trait Urls extends PartialInvariantFunctorSyntax {
/** A query string carrying an `A` information
*
* QueryString values can be created with the [[qs]] operation,
* and can be combined with the `&` operation:
*
* {{{
* val queryPageAndLang: QueryString[(Int, Option[String])] =
* qs[Int]("page") & qs[Option[String]]("lang")
* }}}
*
* - Server interpreters raise an error if they can’t parse the incoming
* request query string parameters as a value of type `A`. By default,
* they produce a Bad Request (400) response with a list of error messages
* in a JSON array. Refer to the documentation of your server interpreter
* to customize this behavior.
*
* @note This type has implicit methods provided by the [[QueryStringSyntax]],
* [[InvariantFunctorSyntax]], and the [[PartialInvariantFunctorSyntax]] classes.
* @group types
*/
type QueryString[A]
/** Provides `xmap` and `xmapPartial` operations.
* @see [[PartialInvariantFunctorSyntax]] and [[InvariantFunctorSyntax]]
*/
implicit def queryStringPartialInvariantFunctor: PartialInvariantFunctor[QueryString]
/** Extension methods on [[QueryString]].
* @group operations
*/
implicit class QueryStringSyntax[A](first: QueryString[A]) {
/** Convenient method to concatenate two [[QueryString]]s.
*
* {{{
* qs[Int]("foo") & qs[String]("baz")
* }}}
*
* @param second `QueryString` to concatenate with this one
* @tparam B Information carried by the second `QueryString`
* @return A `QueryString` that carries both `A` and `B` information
*/
final def &[B](
second: QueryString[B]
)(implicit tupler: Tupler[A, B]): QueryString[tupler.Out] =
combineQueryStrings(first, second)
}
/** Concatenates two `QueryString`s */
def combineQueryStrings[A, B](first: QueryString[A], second: QueryString[B])(implicit
tupler: Tupler[A, B]
): QueryString[tupler.Out]
/** Builds a `QueryString` with one parameter.
*
* Examples:
*
* {{{
* qs[Int]("page") // mandatory `page` parameter
* qs[Option[String]]("lang") // optional `lang` parameter
* qs[List[Long]]("id") // repeated `id` parameter
* }}}
*
* @param name Parameter’s name
* @tparam A Type of the value carried by the parameter
* @group operations
*/
def qs[A](name: String, docs: Documentation = None)(implicit
value: QueryStringParam[A]
): QueryString[A]
/** This type is necessary to express different perspectives of servers and clients
* on optional query string parameters with default value:
* - Client interpreters should define it as `Option[A]` and omit query string parameters with default value
* that are empty
* - Server interpreters should define it as `A` and accept incoming requests whose query string parameters
* with default value are missing, while providing the defined default value
* - Documentation interpreters should mark the parameter as optional and document the provided default value
*
* @group types
*/
type WithDefault[A]
/** Builds a `QueryString` with one optional parameter, which has a default value.
*
* Examples:
*
* {{{
* optQsWithDefault[Int]("page", 1) // optional `page` parameter, with default value 1
* }}}
*
* @param name Parameter’s name
* @tparam A Type of the value carried by the parameter
* @group operations
*/
def optQsWithDefault[A](name: String, default: A, docs: Documentation = None)(implicit
value: QueryStringParam[A]
): QueryString[WithDefault[A]] = unsupportedInterpreter("1.6.0")
/** Make a query string parameter optional:
*
* {{{
* path / "articles" /? qs[Option[Int]]("page")
* }}}
*
* - Client interpreters must omit optional query string parameters that are empty.
* - Server interpreters must accept incoming requests whose optional query string
* parameters are missing, and they must report a failure for incoming requests
* whose optional query string parameters are present, but malformed,
* - Documentation interpreters should mark the parameter as optional.
*
* @group operations
*/
implicit def optionalQueryStringParam[A: QueryStringParam]: QueryStringParam[Option[A]]
/** Support query string parameters with multiple values:
*
* {{{
* path / "articles" /? qs[List[Long]]("id")
* }}}
*
* - Server interpreters must accept incoming requests where such parameters are
* missing (in such a case, its value is an empty collection), and report a
* failure if at least one value is malformed.
*
* @group operations
*/
implicit def repeatedQueryStringParam[A: QueryStringParam, CC[X] <: Iterable[
X
]](implicit factory: Factory[A, CC[A]]): QueryStringParam[CC[A]]
/** A query string parameter codec for type `A`.
*
* The trait `Urls` provides implicit instances of type `QueryStringParam[A]`
* for basic types (e.g., `Int`, `String`, etc.). You can create additional
* instances by transforming or refining the existing instances with `xmap`
* and `xmapPartial`.
*
* @note This type has implicit methods provided by the [[PartialInvariantFunctorSyntax]]
* and the [[InvariantFunctorSyntax]] classes.
* @group types
*/
type QueryStringParam[A]
/** Provides `xmap` and `xmapPartial` operations.
* @see [[PartialInvariantFunctorSyntax]] and [[InvariantFunctorSyntax]]
*/
implicit def queryStringParamPartialInvariantFunctor: PartialInvariantFunctor[QueryStringParam]
/** Ability to define `String` query string parameters
* @group operations
*/
implicit def stringQueryString: QueryStringParam[String]
/** Ability to define `UUID` query string parameters
* @group operations
*/
implicit def uuidQueryString: QueryStringParam[UUID] =
stringQueryString.xmapWithCodec(Codec.uuidCodec)
/** Ability to define `Int` query string parameters
* @group operations
*/
implicit def intQueryString: QueryStringParam[Int] =
stringQueryString.xmapWithCodec(Codec.intCodec)
/** Query string parameter containing a `Long` value
* @group operations
*/
implicit def longQueryString: QueryStringParam[Long] =
stringQueryString.xmapWithCodec(Codec.longCodec)
/** Query string parameter containing a `Boolean` value
* @group operations
*/
implicit def booleanQueryString: QueryStringParam[Boolean] =
stringQueryString.xmapWithCodec(Codec.booleanCodec)
/** Codec for query string parameters of type `Double`
* @group operations
*/
implicit def doubleQueryString: QueryStringParam[Double] =
stringQueryString.xmapWithCodec(Codec.doubleCodec)
/** An URL path segment codec for type `A`.
*
* The trait `Urls` provides implicit instances of `Segment[A]` for basic types
* (e.g., `Int`, `String`, etc.). You can create additional instances by transforming
* or refining the existing instances with `xmap` and `xmapPartial`.
*
* @note This type has implicit methods provided by the [[PartialInvariantFunctorSyntax]]
* and the [[InvariantFunctorSyntax]] classes.
* @group types
*/
type Segment[A]
/** Provides `xmap` and `xmapPartial` operations.
* @see [[PartialInvariantFunctorSyntax]] and [[InvariantFunctorSyntax]]
*/
implicit def segmentPartialInvariantFunctor: PartialInvariantFunctor[Segment]
/** Path segment codec for type `String`
*
* - Server interpreters should return an URL-decoded string value,
* - Client interpreters should take an URL-decoded string value.
* @group operations
*/
implicit def stringSegment: Segment[String]
/** Path segment codec for type `UUID`
* @group operations
*/
implicit def uuidSegment: Segment[UUID] =
stringSegment.xmapWithCodec(Codec.uuidCodec)
/** Path segment codec for type `Int`
* @group operations
*/
implicit def intSegment: Segment[Int] =
stringSegment.xmapWithCodec(Codec.intCodec)
/** Path segment codec for type `Long`
* @group operations
*/
implicit def longSegment: Segment[Long] =
stringSegment.xmapWithCodec(Codec.longCodec)
/** Path segment codec for type `Double`
* @group operations
*/
implicit def doubleSegment: Segment[Double] =
stringSegment.xmapWithCodec(Codec.doubleCodec)
/** An URL path carrying an `A` information
*
* Values of type `Path[A]` can be constructed by the operations [[path]],
* [[segment]], and [[remainingSegments]].
*
* {{{
* path / "user" / segment[UUID]("id")
* }}}
*
* - Server interpreters raise an error if they can’t parse the incoming
* request path as a value of type `A`. By default,
* they produce a Bad Request (400) response with a list of error messages
* in a JSON array. Refer to the documentation of your server interpreter
* to customize this behavior.
*
* @note This type has implicit methods provided by the [[PathOps]],
* [[InvariantFunctorSyntax]], and the [[PartialInvariantFunctorSyntax]] classes.
* @group types
*/
type Path[A] <: Url[A]
/** Provides `xmap` and `xmapPartial` operations.
* @see [[PartialInvariantFunctorSyntax]] and [[InvariantFunctorSyntax]]
*/
implicit def pathPartialInvariantFunctor: PartialInvariantFunctor[Path]
/** Implicit conversion to get rid of intellij errors when defining paths. Effectively should not be called.
* @see [[https://youtrack.jetbrains.com/issue/SCL-16284]]
*/
private[endpoints4s] implicit def dummyPathToUrl[A](p: Path[A]): Url[A] = p
/** Convenient methods for [[Path]]s.
* @group operations
*/
implicit class PathOps[A](first: Path[A]) {
/** Chains this path with the `second` constant path segment */
final def /(second: String): Path[A] =
chainPaths(first, staticPathSegment(second))
/** Chains this path with the `second` path segment */
final def /[B](second: Path[B])(implicit
tupler: Tupler[A, B]
): Path[tupler.Out] = chainPaths(first, second)
/** Chains this path with the given [[QueryString]] */
final def /?[B](qs: QueryString[B])(implicit
tupler: Tupler[A, B]
): Url[tupler.Out] = urlWithQueryString(first, qs)
}
/** A path segment whose value is the given `segment`
* @group operations
*/
def staticPathSegment(segment: String): Path[Unit]
/** A path segment carrying an `A` information
* @group operations
*/
def segment[A](name: String = "", docs: Documentation = None)(implicit
s: Segment[A]
): Path[A]
/** The remaining segments of the path. The `String` value carried by this `Path` is still URL-encoded.
* @group operations
*/
def remainingSegments(
name: String = "",
docs: Documentation = None
): Path[String] // TODO Make it impossible to chain it with another path (ie, `path / remainingSegments() / "foo"` should not compile)
/** Chains the two paths */
def chainPaths[A, B](first: Path[A], second: Path[B])(implicit
tupler: Tupler[A, B]
): Path[tupler.Out]
/** An empty path.
*
* Useful to begin a path definition:
*
* {{{
* path / "foo" / segment[Int] /? qs[String]("bar")
* }}}
*
* @group operations
*/
val path: Path[Unit] = staticPathSegment("")
/** An URL carrying an `A` information
*
* Values of type `URL[A]` are typically constructed by first using the [[path]]
* constructor and then chaining it with segments and query parameters.
*
* {{{
* path / "users" / segment[UUID]("id") /? qs[String]("apiKey")
* }}}
*
* - Server interpreters raise an error if they can’t parse the incoming
* request URL as a value of type `A`. By default,
* they produce a Bad Request (400) response with a list of error messages
* in a JSON array. Refer to the documentation of your server interpreter
* to customize this behavior.
*
* @note This type has implicit methods provided by the [[PartialInvariantFunctorSyntax]]
* and [[InvariantFunctorSyntax]] classes.
* @group types
*/
type Url[A]
/** Provides `xmap` and `xmapPartial` operations
* @see [[PartialInvariantFunctorSyntax]] and [[InvariantFunctorSyntax]]
*/
implicit def urlPartialInvariantFunctor: PartialInvariantFunctor[Url]
/** Builds an URL from the given path and query string */
def urlWithQueryString[A, B](path: Path[A], qs: QueryString[B])(implicit
tupler: Tupler[A, B]
): Url[tupler.Out]
}