-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* search_api: simplify using get search result * logpoint_api: add initial run_search function * logpoint_api: add missing s in success * logpoint_api: make run_search fail fast * logpoint_api: add doc and fix spec * version: bump minor
- Loading branch information
1 parent
c18e4bb
commit a03ad82
Showing
4 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,56 @@ | ||
defmodule LogpointApi do | ||
@moduledoc false | ||
|
||
alias LogpointApi.SearchApi.Query | ||
alias LogpointApi.SearchApi | ||
|
||
defmodule Credential do | ||
@typedoc """ | ||
Struct representing credentials used for authorization. | ||
""" | ||
@type t :: %__MODULE__{username: String.t(), secret_key: String.t()} | ||
defstruct [:username, :secret_key] | ||
end | ||
|
||
@doc """ | ||
Run a search query. | ||
""" | ||
@spec run_search(String.t(), Credential.t(), Query.t()) :: map() | ||
def run_search(ip, credential, %Query{} = query) do | ||
{:ok, %{"success" => true} = search_info} = SearchApi.get_search_id(ip, credential, query) | ||
search_id = Map.get(search_info, "search_id") | ||
|
||
SearchApi.get_search_result(ip, credential, search_id) | ||
|> handle_search_result(ip, credential, search_id, query) | ||
end | ||
|
||
defp handle_search_result({:ok, %{"final" => true} = result}, _, _, _, _), do: result | ||
|
||
defp handle_search_result( | ||
{:ok, %{"final" => false, "success" => true}}, | ||
ip, | ||
credential, | ||
search_id, | ||
query | ||
) do | ||
result = SearchApi.get_search_result(ip, credential, search_id) | ||
|
||
# Wait before retrying. | ||
:timer.sleep(1000) | ||
|
||
handle_search_result(result, ip, credential, search_id, query) | ||
end | ||
|
||
defp handle_search_result( | ||
{:ok, %{"final" => false, "success" => false}}, | ||
ip, | ||
credential, | ||
_, | ||
query | ||
) do | ||
# Wait before recreating the search. | ||
:timer.sleep(1000) | ||
|
||
run_search(ip, credential, query) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters