Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Add config option to set timeout for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
brsntus committed Apr 8, 2017
1 parent b5e915c commit 4ba2697
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
11 changes: 10 additions & 1 deletion README.md
Expand Up @@ -12,7 +12,7 @@ Add `overpex` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:overpex, "~> 0.3.0"}]
[{:overpex, "~> 0.3.1"}]
end
```

Expand All @@ -29,6 +29,15 @@ config :overpex,

The default URL is `http://overpass-api.de/api/interpreter`.

You can also set a timeout in miliseconds:

```elixir
config :overpex,
timeout: 10000 # timeout of 10 seconds
```

The default timeout is `5000` (5 seconds).

## Usage

Query for nodes with name `Gielgen` without the body:
Expand Down
2 changes: 1 addition & 1 deletion lib/overpex/api.ex
Expand Up @@ -13,7 +13,7 @@ defmodule Overpex.API do
@spec query(String.t) :: {:ok, {:xml, String.t}} | {:ok, {:json, String.t}} | {:error, String.t}
def query(query) do
Overpex.Config.url()
|> HTTPoison.post(query)
|> HTTPoison.post(query, %{timeout: Overpex.Config.timeout})
|> process_response()
end

Expand Down
12 changes: 12 additions & 0 deletions lib/overpex/config.ex
Expand Up @@ -14,4 +14,16 @@ defmodule Overpex.Config do
def url do
Application.get_env(:overpex, :url, "http://overpass-api.de/api/interpreter")
end

@doc """
Get timeout in ms to be used when quering the Overpass API
## Return values
Returns the value found in the config files. If nothing is found, returns the default value of 5000 (5 seconds)
"""
@spec timeout :: Integer.t
def timeout do
Application.get_env(:overpex, :timeout, 5000)
end
end
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -3,7 +3,7 @@ defmodule Overpex.Mixfile do

def project do
[app: :overpex,
version: "0.3.0",
version: "0.3.1",
elixir: "~> 1.4",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand Down
14 changes: 12 additions & 2 deletions test/overpex/config_test.exs
@@ -1,13 +1,23 @@
defmodule Overpex.ConfigTest do
use ExUnit.Case

test "url/1 with existing config" do
test "url/0 with existing config" do
Application.put_env(:overpex, :url, "http://example.com")
assert Overpex.Config.url() == "http://example.com"
end

test "url/1 without config" do
test "url/0 without config" do
Application.delete_env(:overpex, :url)
assert Overpex.Config.url() == "http://overpass-api.de/api/interpreter"
end

test "timeout/0 with existing config" do
Application.put_env(:overpex, :timeout, 666)
assert Overpex.Config.timeout() == 666
end

test "timeout/0 without config" do
Application.delete_env(:overpex, :timeout)
assert Overpex.Config.timeout() == 5000
end
end

0 comments on commit 4ba2697

Please sign in to comment.