Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds additional query syntax for the CDA #49

Merged
merged 20 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactors into uri construction
as proposed by @asaaki
  • Loading branch information
floriank committed Jul 9, 2020
commit f93b39fc7019fdb579109f1c88c92bfd7f7aeb05
24 changes: 19 additions & 5 deletions lib/contentful/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,17 @@ defmodule Contentful.Query do
env,
api_key
) do
url = [
space |> Delivery.url(env),
queryable.endpoint(),
parameters |> Request.collection_query_params()
]
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()
Expand Down Expand Up @@ -439,4 +445,12 @@ defmodule Contentful.Query do
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
21 changes: 7 additions & 14 deletions lib/contentful/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,21 @@ defmodule Contentful.Request do
accept: "application/json"
]

def collection_query_params([]) do
""
end
def collection_query_params([]), do: []

@doc """
parses the options for retrieving a collection, usually triggered by a `Contentful.Query.fetch_all/4`.
It will drop any option that is not in an allowed set of parameter options.

## Examples

"?limit=50&skip=25&order=foobar"
[limit: 50, skip: 25, order: "foobar"]
= collection_query_params(limit: 50, baz: "foo", skip: 25, order: "foobar", bar: 42)

Also provides support for mapping out some of the API specific syntax in field handling.

## Examples

"?sys.id[ne]=foobar" = collection_query_params(select_params: [id: [ne: "foobar"]])
[{:"sys.id[ne]", "foobar"}] = collection_query_params(select_params: [id: [ne: "foobar"]])

"""
@spec collection_query_params(
Expand All @@ -45,21 +42,17 @@ defmodule Contentful.Request do
content_type: String.t(),
query: String.t(),
select_params: map()
) :: String.t()
) :: list()
def collection_query_params(options) do
filters =
options
|> Keyword.get(:select_params)
|> fallback([])
|> deconstruct_filters()

params =
options
|> Keyword.take([:limit, :skip, :include, :content_type, :query])
|> Keyword.merge(filters)
|> URI.encode_query()

"?#{params}"
options
|> Keyword.take([:limit, :skip, :include, :content_type, :query])
|> Keyword.merge(filters)
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion lib/contentful_delivery/delivery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ defmodule Contentful.Delivery do
@doc """
Sends a request against the CDA. It's really just a wrapper around `HTTPoison.get/2`
"""
@spec send_request(tuple()) :: {:ok, Response.t()}
@spec send_request({binary(), any()}) :: {:error, HTTPoison.Error.t()} | {:ok, Response.t()}
def send_request({url, headers}) do
get(url, headers)
end
Expand Down
8 changes: 4 additions & 4 deletions test/contentful_delivery/request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule Contentful.RequestTest do

describe "collection_query_params/1" do
test "omits arbitrary keywords" do
"?limit=1&skip=2" = Request.collection_query_params(limit: 1, skip: 2)
[limit: 1, skip: 2] = Request.collection_query_params(limit: 1, skip: 2)
end

test "raises an error for unknown modifiers" do
Expand All @@ -28,16 +28,16 @@ defmodule Contentful.RequestTest do
end

test "translates id into sys.id" do
"?sys.id=foo" = Request.collection_query_params(select_params: [id: "foo"])
[{:"sys.id", "foo"}] = Request.collection_query_params(select_params: [id: "foo"])
end

test "supports modifiers" do
"?fields.name%5Bne%5D=bar" =
[{:"fields.name[ne]", "bar"}] =
Request.collection_query_params(select_params: [name: [ne: "bar"]])
end

test "supports translation of array properties" do
"?fields.tags%5Bnin%5D=foo%2Cbar%2Cbarfoo" =
[{:"fields.tags[nin]", "foo,bar,barfoo"}] =
Request.collection_query_params(select_params: [tags: [nin: ["foo", "bar", "barfoo"]]])
end
end
Expand Down