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
adds support for assets using by
  • Loading branch information
floriank committed Jul 8, 2020
commit 73f5a6c2a7805a004c6db2a1059d90efbff61b39
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"request": {
"body": "",
"headers": {
"authorization": "***",
"User-Agent": "Contentful Elixir SDK",
"accept": "application/json"
},
"method": "get",
"options": [],
"request_body": "",
"url": "https://cdn.contentful.com/spaces/bmehzfuz4raf/environments/master/assets?fields.title%5Bne%5D=bafoo"
},
"response": {
"binary": false,
"body": "{\n \"sys\": {\n \"type\": \"Array\"\n },\n \"total\": 1,\n \"skip\": 0,\n \"limit\": 100,\n \"items\": [\n {\n \"sys\": {\n \"space\": {\n \"sys\": {\n \"type\": \"Link\",\n \"linkType\": \"Space\",\n \"id\": \"bmehzfuz4raf\"\n }\n },\n \"id\": \"5ECf6ltDUOnX441PtBR8Wk\",\n \"type\": \"Asset\",\n \"createdAt\": \"2020-03-16T10:06:11.604Z\",\n \"updatedAt\": \"2020-06-10T12:59:00.879Z\",\n \"environment\": {\n \"sys\": {\n \"id\": \"master\",\n \"type\": \"Link\",\n \"linkType\": \"Environment\"\n }\n },\n \"revision\": 3,\n \"locale\": \"en-US\"\n },\n \"fields\": {\n \"file\": {\n \"url\": \"//images.ctfassets.net/bmehzfuz4raf/5ECf6ltDUOnX441PtBR8Wk/fa3dc5cde2b4d4c5736566c956f20163/Screenshot_from_2020-03-11_22-56-44.png\",\n \"details\": {\n \"size\": 247990,\n \"image\": {\n \"width\": 1920,\n \"height\": 1053\n }\n },\n \"fileName\": \"Screenshot from 2020-03-11 22-56-44.png\",\n \"contentType\": \"image/png\"\n }\n }\n }\n ]\n}\n",
"headers": {
"Connection": "keep-alive",
"Content-Length": "1131",
"Access-Control-Allow-Headers": "Accept,Accept-Language,Authorization,Cache-Control,Content-Length,Content-Range,Content-Type,DNT,Destination,Expires,If-Match,If-Modified-Since,If-None-Match,Keep-Alive,Last-Modified,Origin,Pragma,Range,User-Agent,X-Http-Method-Override,X-Mx-ReqToken,X-Requested-With,X-Contentful-Version,X-Contentful-Content-Type,X-Contentful-Organization,X-Contentful-Skip-Transformation,X-Contentful-User-Agent,X-Contentful-Enable-Alpha-Feature",
"Access-Control-Allow-Methods": "GET,HEAD,OPTIONS",
"Access-Control-Allow-Origin": "*",
"Access-Control-Expose-Headers": "Etag",
"Access-Control-Max-Age": "86400",
"CF-Environment-Id": "master",
"CF-Environment-Uuid": "69b0fe47-9d71-4e0c-a7e0-4e12cb573930",
"CF-Organization-Id": "0bEbsBel9WL3OIaiqx0MnO",
"CF-Space-Id": "bmehzfuz4raf",
"Content-Type": "application/vnd.contentful.delivery.v1+json",
"Contentful-Api": "cda_cached",
"ETag": "W/\"6071154412135978374\"",
"Server": "Contentful",
"X-Content-Type-Options": "nosniff",
"X-Contentful-Region": "us-east-1",
"Accept-Ranges": "bytes",
"Date": "Sun, 05 Jul 2020 14:18:56 GMT",
"Via": "1.1 varnish",
"Age": "0",
"X-Served-By": "cache-hhn4034-HHN",
"X-Cache": "MISS",
"X-Cache-Hits": "0",
"Vary": "Accept-Encoding",
"x-contentful-request-id": "591a465a-dc43-4014-a5bb-dca6d905fcdb"
},
"status_code": 200,
"type": "ok"
}
}
]
50 changes: 50 additions & 0 deletions fixture/vcr_cassettes/multiple_assets,_filtered_by_name.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"request": {
"body": "",
"headers": {
"authorization": "***",
"User-Agent": "Contentful Elixir SDK",
"accept": "application/json"
},
"method": "get",
"options": [],
"request_body": "",
"url": "https://cdn.contentful.com/spaces/bmehzfuz4raf/environments/master/assets?fields.title=bafoo"
},
"response": {
"binary": false,
"body": "{\n \"sys\": {\n \"type\": \"Array\"\n },\n \"total\": 1,\n \"skip\": 0,\n \"limit\": 100,\n \"items\": [\n {\n \"sys\": {\n \"space\": {\n \"sys\": {\n \"type\": \"Link\",\n \"linkType\": \"Space\",\n \"id\": \"bmehzfuz4raf\"\n }\n },\n \"id\": \"577fpmbIfYD71VCjCpYA84\",\n \"type\": \"Asset\",\n \"createdAt\": \"2020-03-16T10:30:45.706Z\",\n \"updatedAt\": \"2020-03-16T10:30:45.706Z\",\n \"environment\": {\n \"sys\": {\n \"id\": \"master\",\n \"type\": \"Link\",\n \"linkType\": \"Environment\"\n }\n },\n \"revision\": 1,\n \"locale\": \"en-US\"\n },\n \"fields\": {\n \"title\": \"bafoo\",\n \"description\": \"a pdf\",\n \"file\": {\n \"url\": \"//assets.ctfassets.net/bmehzfuz4raf/577fpmbIfYD71VCjCpYA84/6456d4157900af3b35537f5f15dd97c2/Travis_Picking_-_The_LEGENDARY_picking_pattern.pdf\",\n \"details\": {\n \"size\": 90783\n },\n \"fileName\": \"Travis Picking - The LEGENDARY picking pattern.pdf\",\n \"contentType\": \"application/pdf\"\n }\n }\n }\n ]\n}\n",
"headers": {
"Connection": "keep-alive",
"Content-Length": "1120",
"Access-Control-Allow-Headers": "Accept,Accept-Language,Authorization,Cache-Control,Content-Length,Content-Range,Content-Type,DNT,Destination,Expires,If-Match,If-Modified-Since,If-None-Match,Keep-Alive,Last-Modified,Origin,Pragma,Range,User-Agent,X-Http-Method-Override,X-Mx-ReqToken,X-Requested-With,X-Contentful-Version,X-Contentful-Content-Type,X-Contentful-Organization,X-Contentful-Skip-Transformation,X-Contentful-User-Agent,X-Contentful-Enable-Alpha-Feature",
"Access-Control-Allow-Methods": "GET,HEAD,OPTIONS",
"Access-Control-Allow-Origin": "*",
"Access-Control-Expose-Headers": "Etag",
"Access-Control-Max-Age": "86400",
"CF-Environment-Id": "master",
"CF-Environment-Uuid": "69b0fe47-9d71-4e0c-a7e0-4e12cb573930",
"CF-Organization-Id": "0bEbsBel9WL3OIaiqx0MnO",
"CF-Space-Id": "bmehzfuz4raf",
"Content-Type": "application/vnd.contentful.delivery.v1+json",
"Contentful-Api": "cda_cached",
"ETag": "W/\"14856425138171294666\"",
"Server": "Contentful",
"X-Content-Type-Options": "nosniff",
"X-Contentful-Region": "us-east-1",
"Accept-Ranges": "bytes",
"Date": "Sun, 05 Jul 2020 14:17:51 GMT",
"Via": "1.1 varnish",
"Age": "0",
"X-Served-By": "cache-fra19140-FRA",
"X-Cache": "MISS",
"X-Cache-Hits": "0",
"Vary": "Accept-Encoding",
"x-contentful-request-id": "f2ae57c3-a489-4c9a-a31a-6dbaa55c3d06"
},
"status_code": 200,
"type": "ok"
}
}
]
3 changes: 3 additions & 0 deletions lib/contentful/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Contentful.Query do
"""
alias Contentful.ContentType
alias Contentful.Delivery
alias Contentful.Delivery.Assets
alias Contentful.Delivery.Entries
alias Contentful.Delivery.Spaces
alias Contentful.Space
Expand Down Expand Up @@ -352,10 +353,12 @@ defmodule Contentful.Query do
parameters |> Keyword.put(:select_params, select_params |> Keyword.merge(new_select_params))}
end

@spec by(module(), list()) :: tuple()
def by(Entries, select_params) do
by({Entries, []}, select_params)
end

@spec by(module(), list()) :: tuple()
def by(Assets, select_params) do
by({Assets, []}, select_params)
end
Expand Down
26 changes: 23 additions & 3 deletions test/contentful_delivery/assets_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ defmodule Contentful.Delivery.AssetsTest do
import Contentful.Query

@space_id "bmehzfuz4raf"
@env "master"
@asset_id "577fpmbIfYD71VCjCpYA84"
@access_token nil

setup_all do
HTTPoison.start()
Expand Down Expand Up @@ -49,26 +51,44 @@ defmodule Contentful.Delivery.AssetsTest do
end
end

test "will fetch all published entries for a space, respecting the limit parameter" do
test "will fetch all published assets for a space, respecting the limit parameter" do
use_cassette "multiple assets, limit filter" do
{:ok, [%Asset{fields: %{title: "bafoo"}}], total: 2} =
Assets |> limit(1) |> fetch_all(@space_id)
end
end

test "will fetch all published entries for a space, respecting the skip param" do
test "will fetch all published assets for a space, respecting the skip param" do
use_cassette "multiple assets, skip filter" do
{:ok, [%Asset{fields: %{title: "Foobar"}}], total: 2} =
Assets |> skip(1) |> fetch_all(@space_id)
end
end

test "will fetch fetch all published entries for a space, respecting both the skip and the limit param" do
test "will fetch fetch all published assets for a space, respecting both the skip and the limit param" do
use_cassette "multiple assets, all filters" do
{:ok, [%Asset{fields: %{title: "Foobar"}}], total: 2} =
Assets |> limit(1) |> skip(1) |> fetch_all(@space_id)
end
end

test "will fetch all published assets, filtered by a name" do
use_cassette "multiple assets, filtered by name" do
{:ok, [%Asset{fields: %{title: "bafoo"}}], total: 1} =
Assets
|> by(title: "bafoo")
|> fetch_all(@space_id, @env, @access_token)
end
end

test "will fetch all published assets, filtered by a name, negated" do
use_cassette "multiple assets, filtered by name, negated" do
{:ok, [%Asset{fields: %{title: nil}}], total: 1} =
Assets
|> by(title: [ne: "bafoo"])
|> fetch_all(@space_id, @env, @access_token)
end
end
end

describe ".stream" do
Expand Down