Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add run_search #13

Merged
merged 6 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Elixir library implementing the [Logpoint API reference](https://docs.logpoint.c
```elixir
def deps do
[
{:logpoint_api, github: "MikaelFangel/logpoint_api", tag: "v0.1.1"}
{:logpoint_api, github: "MikaelFangel/logpoint_api", tag: "v0.2.1"}
]
end
```
Expand Down
45 changes: 45 additions & 0 deletions lib/logpoint_api.ex
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
7 changes: 3 additions & 4 deletions lib/search_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule LogpointApi.SearchApi do
"""

alias LogpointApi.Credential
alias LogpointApi.SearchApi.{Query, SearchID}

@allowed_types ["user_preference", "loginspects", "logpoint_repos", "devices", "livesearches"]

Expand Down Expand Up @@ -77,10 +76,10 @@ defmodule LogpointApi.SearchApi do
@doc """
Retrieve the search result of a specific search id.
"""
@spec get_search_result(String.t(), Credential.t(), SearchID.t()) ::
@spec get_search_result(String.t(), Credential.t(), String.t()) ::
{:ok, map()} | {:error, String.t()}
def get_search_result(ip, credential, %SearchID{} = search_id),
do: get_search_logs(ip, credential, search_id)
def get_search_result(ip, credential, search_id),
do: get_search_logs(ip, credential, %SearchID{search_id: search_id})

@doc false
@spec make_request(String.t(), String.t(), String.t()) :: {:ok, map()} | {:error, String.t()}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule LogpointApi.MixProject do
def project do
[
app: :logpoint_api,
version: "0.1.1",
version: "0.2.1",
elixir: "~> 1.15",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
Loading