Skip to content

Commit

Permalink
adds support for CSV response encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mneudert committed Dec 7, 2016
1 parent 90d6417 commit 8eb0599
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
12 changes: 10 additions & 2 deletions README.md
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/instream/connection/query_runner.ex
Expand Up @@ -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()
Expand Down
29 changes: 27 additions & 2 deletions lib/instream/query/headers.ex
Expand Up @@ -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 """
Expand Down Expand Up @@ -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: []
Expand Down
13 changes: 11 additions & 2 deletions test/instream/response_test.exs
Expand Up @@ -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


Expand Down

0 comments on commit 8eb0599

Please sign in to comment.