defmodule PagerdutyClient do
@base_path "https://api.pagerduty.com"
def main(args) do
with {:ok, args} <- parse_args(args),
{:ok, args} <- prepare_args(args),
{:ok, %HTTPoison.Response{status_code: status_code} = request} <- execute_request(args) do
IO.puts("Request completed with status code #{status_code}")
json_to_map(request)
|> find_user(args)
|> extract_contact_methods()
# |> extract_users_info()
|> IO.inspect(label: "cntact methods")
else
{:error, :no_method_given} -> IO.puts("Error executing request as no HTTP method was given")
{:error, :no_url_given} -> IO.puts("Error executing request as no URL was given")
error -> IO.puts("Something went wrong, error: #{error}")
end
end
defp find_user({:ok, %{"users" => users}}, args) do
Enum.find(users, fn user ->
user["email"] == args.email
end)
end
# defp retrieve_user_info(id) do
# with {:ok, request} <-
# execute_request(
# method: "get",
# url: "/users",
# body: "",
# headers: get_headers(),
# params: build_params(id) |> IO.inspect(label: "params built")
# ) do
# request
# end
# end
defp extract_contact_methods(request_body) do
Enum.map(request_body["contact_methods"], fn method ->
%{
id: method["id"],
address: method["address"],
summary: method["summary"]
}
end)
end
# defp extract_users_info({:ok, %{"users" => users}}) do
# Enum.map(users, fn user ->
# %{
# id: user["id"],
# name: user["name"],
# email: user["email"]
# }
# end)
# end
# def http_get(url) do
# headers = %{"User-Agent" => "Mozilla/5.0"}
# IO.puts("\nSending 'GET' request to URL : #{url}")
# response = HTTPoison.get!(url, headers)
# IO.puts("Response Code : #{response.status_code}")
# response.body
# end
defp parse_args(args) do
with {opts, _, _} <-
OptionParser.parse(args,
strict: [
method: :string,
url: :string,
body: :string,
params: :string,
email: :string
]
) do
{:ok, opts}
end
end
defp prepare_args(args) do
with {:ok, method} <- validate_method(Keyword.get(args, :method)),
{:ok, url} <- build_url(Keyword.get(args, :url)),
body <- format_string(Keyword.get(args, :body)),
params <- build_params(Keyword.get(args, :params)),
headers <- get_headers(),
email <- validate_email(Keyword.get(args, :email)) do
{:ok,
%{method: method, url: url, body: body, headers: headers, params: params, email: email}}
end
end
defp execute_request(%{
method: method,
url: url,
body: body,
headers: headers,
params: params,
email: _
}) do
IO.puts("Sending #{method} request to #{url}")
HTTPoison.request(method, url, body, headers, params)
end
def json_to_map(%HTTPoison.Response{body: body}) do
JSON.decode(body)
end
defp validate_method(nil), do: {:error, :no_method_given}
defp validate_method(method) do
{:ok, String.to_atom(method)}
end
defp validate_email(nil), do: ""
defp validate_email(email), do: email
defp build_url(nil), do: {:error, :no_url_given}
defp build_url(url), do: {:ok, @base_path <> url}
defp format_string(nil), do: ""
defp format_string(string) do
Regex.replace(~r/([a-z0-9]+):/, string, "\"\\1\":")
|> String.replace("'", "\"")
end
defp build_params(nil), do: []
defp build_params(params) when params == %{}, do: []
defp build_params(params) do
params =
params
|> format_string()
|> JSON.decode!()
[{:params, params}]
end
defp get_headers(),
do: [
{"Accept", "application/vnd.pagerduty+json;version=2"},
{"Authorization", "Token token=y_NbAkKc66ryYTWUXYEu"},
{"Content-type", "application/json"}
]
end