From 8eb0599a7f4ee8e38cd34e2bff0a5905e2c8581c Mon Sep 17 00:00:00 2001 From: Marc Neudert Date: Fri, 2 Dec 2016 11:58:48 +0100 Subject: [PATCH] adds support for CSV response encoding --- README.md | 12 ++++++++-- lib/instream/connection/query_runner.ex | 2 +- lib/instream/query/headers.ex | 29 +++++++++++++++++++++++-- test/instream/response_test.exs | 13 +++++++++-- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a0ca6a9d..24d47a7c 100644 --- a/README.md +++ b/README.md @@ -275,8 +275,16 @@ Every query can be executed asynchronously by passing `[async: true]` to `MyApp.MyConnection.execute()`. The result will then always be an immediate `:ok` without waiting for the query to be actually executed. -To access the raw (undecoded) response of a query you can pass -`[result_as: :raw]` to `MyApp.MyConnection.execute()`. +By default the response of a query will be a map decoded from your server's +JSON response. + +Alternatively you can pass `[result_as: format]` to +`MyApp.MyConnection.execute/2` to change the result format to one of the +following: + +- `:csv` - CSV encoded response +- `:json` - JSON encoded response (implicit default) +- `:raw` - Raw server format (JSON string) #### Administrative Queries diff --git a/lib/instream/connection/query_runner.ex b/lib/instream/connection/query_runner.ex index 66342c1a..a61ff107 100644 --- a/lib/instream/connection/query_runner.ex +++ b/lib/instream/connection/query_runner.ex @@ -53,7 +53,7 @@ defmodule Instream.Connection.QueryRunner do @spec read(Query.t, Keyword.t, map) :: any def read(%Query{} = query, opts, %{ module: conn }) do config = conn.config() - headers = Headers.assemble(config) + headers = Headers.assemble(config, opts) url = config |> URL.query() diff --git a/lib/instream/query/headers.ex b/lib/instream/query/headers.ex index 3067f2df..8ea5b4e6 100644 --- a/lib/instream/query/headers.ex +++ b/lib/instream/query/headers.ex @@ -6,9 +6,10 @@ defmodule Instream.Query.Headers do @doc """ Assembles the headers for a query. """ - @spec assemble(Keyword.t) :: list - def assemble(config) do + @spec assemble(Keyword.t, Keyword.t) :: list + def assemble(config, options \\ []) do assemble_auth(config[:auth]) + ++ assemble_encoding(options[:result_as]) end @doc """ @@ -39,6 +40,30 @@ defmodule Instream.Query.Headers do end end + @doc """ + Assembles headers for response encoding. + + ## Usage + + iex> assemble_encoding(nil) + [] + + # not handled here... + iex> assemble_encoding(:raw) + [] + + iex> assemble_encoding(:csv) + [{'Accept', 'application/csv'}] + + iex> assemble_encoding(:json) + [{'Accept', 'application/json'}] + """ + @spec assemble_encoding(nil | atom) :: list + def assemble_encoding(nil), do: [] + def assemble_encoding(:csv), do: [{ 'Accept', 'application/csv' }] + def assemble_encoding(:json), do: [{ 'Accept', 'application/json' }] + def assemble_encoding(:raw), do: [] + defp basic_auth_header(nil, _), do: [] defp basic_auth_header(_, nil), do: [] diff --git a/test/instream/response_test.exs b/test/instream/response_test.exs index a64d263a..fca2551d 100644 --- a/test/instream/response_test.exs +++ b/test/instream/response_test.exs @@ -6,16 +6,25 @@ defmodule Instream.ResponseTest do alias Instream.TestHelpers.Connection - test "parsed response" do + test "response format: default (parsed)" do response = Database.show() |> Connection.execute() assert is_map(response) end - test "raw response" do + @tag influxdb_version: "1.1.0" + test "response format: csv" do + response = Database.show() |> Connection.execute([ result_as: :csv ]) + + assert is_binary(response) + assert "name," <> _ = response + end + + test "response format: raw" do response = Database.show() |> Connection.execute([ result_as: :raw ]) assert is_binary(response) + assert "{" <> _ = response end