| 1 |
|
defmodule DaProductApp.SaasKit.Client do |
| 2 |
|
@moduledoc """ |
| 3 |
|
SaaS Kit API client for handling subscriptions, billing, and user management. |
| 4 |
|
""" |
| 5 |
|
|
| 6 |
|
# Do not alias HTTPoison.Response to avoid compile-time struct lookup issues |
| 7 |
|
|
| 8 |
|
# @base_url and @timeout removed: saas_kit dependency no longer present |
| 9 |
|
|
| 10 |
:-( |
defp api_key do |
| 11 |
|
# Application.get_env(:saas_kit, :api_key) removed: saas_kit dependency no longer present |
| 12 |
|
end |
| 13 |
|
|
| 14 |
:-( |
defp headers do |
| 15 |
|
[ |
| 16 |
:-( |
{"Authorization", "Bearer #{api_key()}"}, |
| 17 |
|
{"Content-Type", "application/json"}, |
| 18 |
|
{"Accept", "application/json"} |
| 19 |
|
] |
| 20 |
|
end |
| 21 |
|
|
| 22 |
|
@doc """ |
| 23 |
|
Make a GET request to the SaaS Kit API |
| 24 |
|
""" |
| 25 |
:-( |
def get(path, params \\ %{}) do |
| 26 |
:-( |
url = build_url(path, params) |
| 27 |
|
|
| 28 |
:-( |
case HTTPoison.get(url, headers()) do |
| 29 |
|
{:ok, resp} when is_map(resp) -> |
| 30 |
:-( |
status = Map.get(resp, :status_code) || Map.get(resp, "status_code") |
| 31 |
:-( |
body = Map.get(resp, :body) || Map.get(resp, "body") |
| 32 |
|
|
| 33 |
:-( |
case status do |
| 34 |
:-( |
200 -> {:ok, Jason.decode!(body)} |
| 35 |
:-( |
_ -> {:error, %{status_code: status, body: body}} |
| 36 |
|
end |
| 37 |
|
|
| 38 |
:-( |
{:error, error} -> |
| 39 |
|
{:error, error} |
| 40 |
|
end |
| 41 |
|
end |
| 42 |
|
|
| 43 |
|
@doc """ |
| 44 |
|
Make a POST request to the SaaS Kit API |
| 45 |
|
""" |
| 46 |
:-( |
def post(path, body \\ %{}) do |
| 47 |
:-( |
url = build_url(path) |
| 48 |
:-( |
json_body = Jason.encode!(body) |
| 49 |
|
|
| 50 |
:-( |
case HTTPoison.post(url, json_body, headers()) do |
| 51 |
|
{:ok, resp} when is_map(resp) -> |
| 52 |
:-( |
status = Map.get(resp, :status_code) || Map.get(resp, "status_code") |
| 53 |
:-( |
body = Map.get(resp, :body) || Map.get(resp, "body") |
| 54 |
|
|
| 55 |
:-( |
if status in 200..299 do |
| 56 |
|
{:ok, Jason.decode!(body)} |
| 57 |
|
else |
| 58 |
|
{:error, %{status_code: status, body: body}} |
| 59 |
|
end |
| 60 |
|
|
| 61 |
:-( |
{:error, error} -> |
| 62 |
|
{:error, error} |
| 63 |
|
end |
| 64 |
|
end |
| 65 |
|
|
| 66 |
|
@doc """ |
| 67 |
|
Make a PUT request to the SaaS Kit API |
| 68 |
|
""" |
| 69 |
:-( |
def put(path, body \\ %{}) do |
| 70 |
:-( |
url = build_url(path) |
| 71 |
:-( |
json_body = Jason.encode!(body) |
| 72 |
|
|
| 73 |
:-( |
case HTTPoison.put(url, json_body, headers()) do |
| 74 |
|
{:ok, resp} when is_map(resp) -> |
| 75 |
:-( |
status = Map.get(resp, :status_code) || Map.get(resp, "status_code") |
| 76 |
:-( |
body = Map.get(resp, :body) || Map.get(resp, "body") |
| 77 |
|
|
| 78 |
:-( |
if status in 200..299 do |
| 79 |
|
{:ok, Jason.decode!(body)} |
| 80 |
|
else |
| 81 |
|
{:error, %{status_code: status, body: body}} |
| 82 |
|
end |
| 83 |
|
|
| 84 |
:-( |
{:error, error} -> |
| 85 |
|
{:error, error} |
| 86 |
|
end |
| 87 |
|
end |
| 88 |
|
|
| 89 |
|
@doc """ |
| 90 |
|
Make a DELETE request to the SaaS Kit API |
| 91 |
|
""" |
| 92 |
|
def delete(path) do |
| 93 |
:-( |
url = build_url(path) |
| 94 |
|
|
| 95 |
:-( |
case HTTPoison.delete(url, headers()) do |
| 96 |
|
{:ok, resp} when is_map(resp) -> |
| 97 |
:-( |
status = Map.get(resp, :status_code) || Map.get(resp, "status_code") |
| 98 |
:-( |
body = Map.get(resp, :body) || Map.get(resp, "body") |
| 99 |
|
|
| 100 |
:-( |
if status in 200..299 do |
| 101 |
|
{:ok, :deleted} |
| 102 |
|
else |
| 103 |
|
{:error, %{status_code: status, body: body}} |
| 104 |
|
end |
| 105 |
|
|
| 106 |
:-( |
{:error, error} -> |
| 107 |
|
{:error, error} |
| 108 |
|
end |
| 109 |
|
end |
| 110 |
|
|
| 111 |
:-( |
defp build_url(path, params \\ %{}) do |
| 112 |
:-( |
url = "#{@base_url}#{path}" |
| 113 |
|
|
| 114 |
:-( |
if Enum.empty?(params) do |
| 115 |
:-( |
url |
| 116 |
|
else |
| 117 |
:-( |
query_string = URI.encode_query(params) |
| 118 |
:-( |
"#{url}?#{query_string}" |
| 119 |
|
end |
| 120 |
|
end |
| 121 |
|
end |