Nested query params with Tesla

619 Views Asked by At

This is the URL I'm trying to hit:

/example/?fields=*&filter[platform][eq]=111&order=date:asc&filter[date][gt]=1500619813000&expand=yes

My code:

  get("/release_dates",
      query: [
        fields: "*",
        order: "date:desc",
        expand: "games",
        filter: %{
          date: %{gt: unix_now},
          version_parent: %{not_exists: 1}
        }
      ]
    )

I'm trying to perform a Tesla GET request that has those filter[date][gt]=123123123123 type query param.

Appreciate the help!

1

There are 1 best solutions below

0
aef On

If I understand you correctly, you want to generate a URI with a filter for "greater than" timestamps which are variable.

According to your inital example, this can be done like this:

Tesla.get("/example/",
  fields: "*",
    filter: [
    platform: [
      eq: 111
    ]
  ],
  order: "date:asc",
  filter: [
    date: [
      gt: unix_now
    ]
  ],
  expand: "yes"
)

Note that /example is a relative reference and can only be resolved with a base URI. Better provide a complete URI.

If you want to experiment with the URI generator in an iex console, you can use iex -S mix in your project directory and then use the following functions:

Tesla.build_url("/example/",
  fields: "*",
  filter: [
    platform: [
      eq: 111
    ]
  ],
  order: "date:asc",
  filter: [
    date: [
      gt: 123123123123
    ]
  ],
  expand: "yes"
) |> URI.decode()