-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathquery.ex
468 lines (358 loc) · 13 KB
/
query.ex
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
defmodule Contentful.Query do
require Logger
@moduledoc """
This module provides the chainable query syntax for building queries against the
APIs of Contentful.
The chains will then be serialized to a URL and send to the API. A basic query looks like this:
```
Entity
|> skip(3)
|> limit(2)
|> fetch_all
```
wherein `Entity` is one of the modules that exhibit `Contentful.Queryable` behaviour, such as
`Contentful.Delivery.Entries`, `Contentful.Delivery.Assets` and `Contentful.Delivery.ContentTypes`.
As an example, querying all entries of a given `Contentful.Space` (represented by its `space_id`) can
be done as follows:
```
Contentful.Delivery.Entries
|> Contentful.Query.fetch_all(space_id)
```
"""
alias Contentful.Configuration
alias Contentful.ContentType
alias Contentful.Delivery
alias Contentful.Delivery.Assets
alias Contentful.Delivery.Entries
alias Contentful.Delivery.Spaces
alias Contentful.Request
alias Contentful.Space
alias Contentful.SysData
@allowed_filter_modifiers [:all, :in, :nin, :ne, :lte, :gte, :lt, :gt, :match, :exists]
@doc """
adds the `include` parameter to a query.
This allows for fetching associated items up to a collection of `Contentful.Entry`.
The `include` call will _only_ work with `Contentful.Delivery.Entries`, as it is meaningless to
other entities.
## Example:
alias Contentful.Delivery.Entries
Entries |> include(2) |> fetch_all
# translates in the background to
"<api_url>/entries?include=2"
"""
@spec include({Entries, list()}, integer()) :: {Entries, list()}
def include(queryable, number \\ 1)
def include({Entries, parameters}, number) do
if number > 10 do
raise(ArgumentError, "Include depth cannot be higher than 10!")
end
{Entries, parameters |> Keyword.put(:include, number)}
end
def include(Entries, number) do
include({Entries, []}, number)
end
def include(queryable, _number) do
queryable
end
@doc """
adds the `limit` parameter to a call, limiting the amount of entities returned.
The caller will still retreive the total amount of entities, if successful.
The limit defaults to `1000`, the maximum `limit` allowed for API calls.
## Examples
alias Contentful.Delivery.Assets
Assets |> limit(2) |> fetch_all
# translates in the background to
"<api_url>/assets?limit=2"
"""
@spec limit({module(), list()}, integer()) :: {module(), list()}
def limit(queryable, number \\ 1000)
def limit({queryable, parameters}, number) do
{queryable, parameters |> Keyword.put(:limit, number)}
end
def limit(queryable, number) do
limit({queryable, []}, number)
end
@doc """
adds the `skip` parameter to API calls, allowing to skip over a number of entities, effectively
allowing the implementation of pagination if desired.
## Examples
alias Contentful.Delivery.Assets
Assets |> skip(10) |> fetch_all
# translates in the background to a call to the API
"<api_url>/assets?skip=10"
"""
@spec skip({module(), list()}, non_neg_integer()) :: {module(), list()}
def skip({queryable, parameters}, number) do
{queryable, parameters |> Keyword.put(:skip, number)}
end
def skip(queryable, number) do
skip({queryable, []}, number)
end
@doc """
adds a `content_type` parameter for filtering sets of `Contentful.Entry`
by a `Contentful.ContentType`, effectively allowing for scoping by content type.
`content_type` will only work with `Contentful.Delivery.Entries` at the moment.
## Examples
alias Contentful.Delivery.Entries
Entries |> content_type("foobar") |> fetch_all
# translates in the background to
"<api_url>/entries?content_type=foobar"
# also works with passing `Contentful.ContentType`:
my_content_type = %Contentful.ContentType{sys: %Contentful.SysData{id: "foobar"}}
Entries |> content_type(my_content_type) |> fetch_all
"""
@spec content_type({Entries, list()}, String.t() | ContentType.t()) :: {Entries, list()}
def content_type({Entries, parameters}, c_type) when is_binary(c_type) do
{Entries, parameters |> Keyword.put(:content_type, c_type)}
end
def content_type({Entries, parameters}, %ContentType{sys: %SysData{id: value}} = _c_type) do
content_type({Entries, parameters}, value)
end
def content_type(Entries, c_type) do
content_type({Entries, []}, c_type)
end
def content_type(queryable, _c_type) do
queryable
end
@doc """
will __resolve__ a query chain by eagerly calling the API and resolving the response immediately
## Examples
alias Contentful.Delivery.Entries
{:ok, entries, total: _total_count_of_entries} =
Entries |> content_type("foobar") |> limit(1) |> fetch_all
"""
@spec fetch_all({module(), list()}, String.t(), String.t(), String.t()) ::
{:ok, list(struct()), total: non_neg_integer()}
| {:error, :rate_limit_exceeded, wait_for: integer()}
| {:error, atom(), original_message: String.t()}
def fetch_all(
queryable,
space \\ Configuration.get(:space_id),
env \\ Configuration.get(:environment),
api_key \\ Configuration.get(:access_token)
)
def fetch_all({Spaces, _}, _, _, _) do
{:error, [message: "Fetching a spaces collection is not supported, use fetch_one/1 instead"],
total: 0}
end
def fetch_all(queryable, %Space{sys: %SysData{id: space}}, env, api_key) do
fetch_all(queryable, space, env, api_key)
end
def fetch_all(
{queryable, parameters},
space,
env,
api_key
) do
params = parameters |> Request.collection_query_params()
url =
[
space |> Delivery.url(env),
queryable.endpoint()
]
|> Enum.join()
|> URI.parse()
|> add_query_params(params)
|> to_string()
{url, api_key |> Request.headers()}
|> Delivery.send_request()
|> Delivery.parse_response(&queryable.resolve_collection_response/1)
end
def fetch_all(queryable, space, env, api_key) do
fetch_all({queryable, []}, space, env, api_key)
end
@doc """
will __resolve__ a query chain by eagerly calling the API asking for _one_ entity
## Examples
import Contentful.Query
alias Contentful.Delivery.{Spaces, Entries}
# Note: Spaces is the only Queryable that can be fetched without an id
Spaces |> fetch_one
# all others would throw an error, so an id has to be passed:
Entries |> fetch_one("my_entry_id")
"""
@spec fetch_one(module(), String.t() | nil, String.t(), String.t(), String.t()) ::
{:ok, struct()}
| {:error, :rate_limit_exceeded, wait_for: integer()}
| {:error, atom(), original_message: String.t()}
@deprecated """
Use Contentful.Query.by/2 and Contentful.Query.fetch_all/4 with an id instead
"""
def fetch_one(
queryable,
id \\ nil,
space \\ Configuration.get(:space_id),
env \\ Configuration.get(:environment),
api_key \\ Configuration.get(:access_token)
)
def fetch_one(queryable, id, %Space{sys: %SysData{id: space_id}}, env, api_key) do
fetch_one(queryable, id, space_id, env, api_key)
end
def fetch_one(
queryable,
id,
space,
env,
api_key
) do
url =
case {queryable, id} do
{Spaces, nil} ->
[space |> Delivery.url()]
{Spaces, id} ->
[id |> Delivery.url()]
{_queryable, nil} ->
raise ArgumentError, "id is missing!"
{{module, _parameters}, id} ->
# drops the parameters, as single query responses don't allow parameters
[space |> Delivery.url(env), module.endpoint(), "/#{id}"]
_ ->
[space |> Delivery.url(env), queryable.endpoint(), "/#{id}"]
end
# since you can pass compose into fetch one, we strip extra params here
queryable =
case queryable do
{module, parameters} ->
Logger.warn("Stripping parameters: #{inspect(parameters)}")
module
_ ->
queryable
end
{url, api_key |> Request.headers()}
|> Delivery.send_request()
|> Delivery.parse_response(&queryable.resolve_entity_response/1)
end
@doc """
Adds a filter condition to the query.
This will work for Entries *requiring* a call to `content_type` before:
## Example
import Contentful.Query
alias Contentful.Delivery.Entries
{:ok, entries, total: 1}
= Entries
|> content_type("dogs")
|> by(name: "Hasso", breed: "dalmatian")
|> fetch_all
This will also allow for more complex queries using modifiers:
## Example
import Contentful.Query
alias Contentful.Delivery.Entries
{:ok, entries, total: 100}
= Entries
|> content_type("dogs")
|> by(name: [ne: "Hasso"], breed: "dalmatian")
|> fetch_all
Allowed modifiers are `[:in, :nin, :ne, :lte, :gte, :lt, :gt, :match, :exist]`. See the
[official docs](https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/equality-operator)
for adding search parameters this way.
Working with `Contentful.Delivery.Assets` requires no `content_type` call:
## Example
import Contentful.Query
alias Contentful.Delivery.Assets
{:ok, assets, total: 1} = Assets |> by(id: "foobar") |> fetch_all
Calling `by/2` allows for adding multiple conditions to the query:
## Example
import Contentful.Query
alias Contentful.Delivery.Assets
{:ok, assets, total: 200}
= Assets
|> by(tags: [nin: "maps"])
|> fetch_all
"""
@spec by(tuple(), list()) :: tuple()
@doc since: "0.4.0"
def by({Entries, parameters}, new_select_params) do
select_params = parameters |> Keyword.take([:select_params])
content_type_present? = parameters |> Keyword.take([:content_type]) |> length() > 0
unless content_type_present? do
raise %ArgumentError{
message: """
Filtering for entries requires a content_type, example:
Entries |> content_type("cats") |> by(name: "Gretchen")
"""
}
end
{Entries,
parameters |> Keyword.put(:select_params, select_params |> Keyword.merge(new_select_params))}
end
def by({Assets, parameters}, new_select_params) do
select_params = parameters |> Keyword.take([:select_params])
{Assets,
parameters |> Keyword.put(:select_params, select_params |> Keyword.merge(new_select_params))}
end
def by(Entries, select_params) do
by({Entries, []}, select_params)
end
def by(Assets, select_params) do
by({Assets, []}, select_params)
end
def by(queryable, _select_params) do
queryable
end
@doc """
allows for full text search over all entries fields. The original nomenclature fromthe API docs is `query`.
This has been renamed for clarity here.
## Example
import Contentful.Query
{Entries, [query: "Nyancat"]} = Entries |> search_full_text("Nyancat")
# or, with full `fetch_all`
{:ok, nyan_cats, total: 616} =
Entries
|> search_full_text("Nyancat")
|> fetch_all
"""
@spec search_full_text(tuple(), term()) :: tuple()
@doc since: "0.4.0"
def search_full_text({Entries, parameters}, term) do
{Entries, parameters |> Keyword.put(:query, term)}
end
def search_full_text(Entries, term) do
search_full_text({Entries, []}, term)
end
def search_full_text(queryable, _term) do
queryable
end
@doc """
will __resolve__ a query chain by constructing a `Stream.resource` around a possible API response
allowing for lazy evaluation of queries. Cann be helpful with translating collection calls of
unknown size.
Be careful when using this, as one can run into API rate limits quickly for very large sets.
## Examples
import Contentful.Query
alias Contentful.Delivery.{Assets, Spaces}
# translates into two api calls in the background
Assets |> stream |> Enum.take(2000)
# you can use limit() to set the page size, in the example, stream would call the API
# 10 times total.
Assets |> limit(100) |> Enum.take(1000)
# will not work with Spaces, though, as they is no collection endpoint
"""
@spec stream(tuple(), String.t(), String.t(), String.t()) ::
Enumerable.t()
def stream(
queryable,
space \\ Configuration.get(:space_id),
env \\ Configuration.get(:environment),
api_key \\ Configuration.get(:access_token)
)
def stream(Spaces, _space, _env, _api_key) do
{:error, [message: "Streaming a spaces collection is not supported"], total: 0}
end
def stream(args, space, env, api_key) do
Contentful.Stream.stream(args, space, env, api_key)
end
@doc """
Returns the filter modifiers supported byt the Query syntax
"""
@spec allowed_filter_modifiers() :: list()
@doc since: "0.4.0"
def allowed_filter_modifiers do
@allowed_filter_modifiers
end
defp add_query_params(uri, []) do
uri
end
defp add_query_params(%URI{} = uri, params) do
uri |> Map.put(:query, URI.encode_query(params))
end
end