Skip to content

Commit

Permalink
logpoint_api: refactored functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelFangel committed Apr 26, 2024
1 parent c83fe5a commit 7e95bdd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 45 deletions.
23 changes: 12 additions & 11 deletions lib/incident_api.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule LogpointApi.IncidentApi do
alias LogpointApi.Credential, as: Credential

defmodule TimePeriod do
defmodule TimeRange do
@derive {Jason.Encoder, only: [:version, :ts_from, :ts_to]}
defstruct [:ts_from, :ts_to, version: "0.1"]
end
Expand All @@ -11,14 +11,14 @@ defmodule LogpointApi.IncidentApi do
defstruct [:incident_obj_id, :incident_id]
end

def get_incidents(ip, %Credential{} = credential, %TimePeriod{} = request_data),
do: get_incident_information(ip, "/incidents", credential, request_data)
def get_incidents(ip, credential, %TimeRange{} = time_range),
do: get_incident_information(ip, "/incidents", credential, time_range)

def get_data_from_incident(ip, %Credential{} = credential, %Incident{} = incident),
def get_data_from_incident(ip, credential, %Incident{} = incident),
do: get_incident_information(ip, "/get_data_from_incident", credential, incident)

def get_incident_states(ip, %Credential{} = credential, %TimePeriod{} = request_data),
do: get_incident_information(ip, "/incident_states", credential, request_data)
def get_incident_states(ip, credential, %TimeRange{} = time_range),
do: get_incident_information(ip, "/incident_states", credential, time_range)

def get_users(ip, %Credential{} = credential) do
params = %{
Expand All @@ -40,22 +40,23 @@ defmodule LogpointApi.IncidentApi do
end

defp make_request(ip, path, params) do
url = "https://" <> ip <> path
url = build_url(ip, path)
headers = [{"Content-Type", "application/json"}]
body = Jason.encode!(params)
# On-prem uses self signed certificates and we thus need to disable the verification.
options = [ssl: [{:verify, :verify_none}]]

case HTTPoison.request(:get, url, body, headers, options) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
body
|> Jason.decode!()
{:ok, Jason.decode!(body)}

{:ok, %HTTPoison.Response{status_code: status_code}} ->
IO.puts("Received response with status code #{status_code}")
{:error, "Received response with status code #{status_code}"}

{:error, %HTTPoison.Error{reason: reason}} ->
IO.puts("HTTP request failed with reason: #{reason}")
{:error, "HTTP request failed with reason: #{reason}"}
end
end

defp build_url(ip, path), do: "https://" <> ip <> path
end
68 changes: 34 additions & 34 deletions lib/search_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,65 +13,65 @@ defmodule LogpointApi.SearchApi do
defstruct [:search_id]
end

def get_user_timezone(ip, %Credential{} = credential),
do: get_allowed_data(ip, %Credential{} = credential, "user_preference")
def get_user_timezone(ip, credential),
do: get_allowed_data(ip, credential, "user_preference")

def get_logpoints(ip, %Credential{} = credential),
do: get_allowed_data(ip, %Credential{} = credential, "loginspects")
def get_logpoints(ip, credential),
do: get_allowed_data(ip, credential, "loginspects")

def get_repos(ip, %Credential{} = credential),
do: get_allowed_data(ip, %Credential{} = credential, "Logpoint_repos")
def get_repos(ip, credential),
do: get_allowed_data(ip, credential, "Logpoint_repos")

def get_devices(ip, %Credential{} = credential),
do: get_allowed_data(ip, %Credential{} = credential, "devices")
def get_devices(ip, credential),
do: get_allowed_data(ip, credential, "devices")

def get_livesearches(ip, %Credential{} = credential),
do: get_allowed_data(ip, %Credential{} = credential, "livesearches")
def get_livesearches(ip, credential),
do: get_allowed_data(ip, credential, "livesearches")

def get_search_id(ip, %Credential{} = credential, %Query{} = request_data),
do: get_search_logs(ip, %Credential{} = credential, request_data)
def get_search_id(ip, credential, %Query{} = query),
do: get_search_logs(ip, credential, query)

def get_search_result(ip, %Credential{} = credential, %SearchID{} = request_data),
do: get_search_logs(ip, %Credential{} = credential, request_data)
def get_search_result(ip, credential, %SearchID{} = search_id),
do: get_search_logs(ip, credential, search_id)

defp make_request(ip, path, payload) do
url = "https://" <> ip <> path
url = build_url(ip, path)
headers = [{"Content-Type", "application/x-www-form-urlencoded"}]
# On-prem uses self signed certificates and we thus need to disable the verification.
options = [ssl: [{:verify, :verify_none}]]

case HTTPoison.post(url, payload, headers, options) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
body
|> Jason.decode!()
{:ok, Jason.decode!(body)}

{:ok, %HTTPoison.Response{status_code: status_code}} ->
IO.puts("Received response with status code #{status_code}")
{:error, "Received response with status code #{status_code}"}

{:error, %HTTPoison.Error{reason: reason}} ->
IO.puts("HTTP request failed with reason: #{reason}")
{:error, "HTTP request failed with reason: #{reason}"}
end
end

defp get_allowed_data(ip, %Credential{} = credential, type) when type in @allowed_types do
payload =
URI.encode_query(%{
"username" => credential.username,
"secret_key" => credential.secret_key,
"type" => type
})
defp build_url(ip, path), do: "https://" <> ip <> path

defp get_allowed_data(ip, credential, type) when type in @allowed_types do
payload = build_payload(credential, %{"type" => type})
make_request(ip, "/getalloweddata", payload)
end

defp get_search_logs(ip, %Credential{} = credential, request_data) do
payload =
URI.encode_query(%{
"username" => credential.username,
"secret_key" => credential.secret_key,
"requestData" => request_data |> Jason.encode!()
})

defp get_search_logs(ip, credential, request_data) do
payload = build_payload(credential, %{"requestData" => Jason.encode!(request_data)})
make_request(ip, "/getsearchlogs", payload)
end

defp build_payload(%Credential{} = credential, data) do
Map.merge(
%{
"username" => credential.username,
"secret_key" => credential.secret_key
},
data
)
|> URI.encode_query()
end
end

0 comments on commit 7e95bdd

Please sign in to comment.