Add API endpoints for site and shared link creation
parent
8ae4d46e5b
commit
3ed964b45b
@ -0,0 +1,50 @@
|
||||
defmodule PlausibleWeb.Api.ExternalSitesController do
|
||||
use PlausibleWeb, :controller
|
||||
use Plausible.Repo
|
||||
use Plug.ErrorHandler
|
||||
alias Plausible.Sites
|
||||
|
||||
def create_site(conn, params) do
|
||||
user_id = conn.assigns[:current_user_id]
|
||||
site_params = Map.get(params, "site", %{})
|
||||
|
||||
case Sites.create(user_id, site_params) do
|
||||
{:ok, %{site: site}} ->
|
||||
json(conn, site)
|
||||
|
||||
{:error, :site, changeset, _} ->
|
||||
conn
|
||||
|> put_status(400)
|
||||
|> json(serialize_errors(changeset))
|
||||
end
|
||||
end
|
||||
|
||||
def find_or_create_shared_link(conn, %{"domain" => domain, "link_name" => link_name}) do
|
||||
site = Sites.get_for_user!(conn.assigns[:current_user_id], domain)
|
||||
shared_link = Repo.get_by(Plausible.Site.SharedLink, site_id: site.id, name: link_name)
|
||||
|
||||
shared_link =
|
||||
case shared_link do
|
||||
nil -> Sites.create_shared_link(site, link_name)
|
||||
link -> {:ok, link}
|
||||
end
|
||||
|
||||
case shared_link do
|
||||
{:ok, link} ->
|
||||
json(conn, %{
|
||||
name: link.name,
|
||||
url: Sites.shared_link_url(site, link)
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
defp serialize_errors(changeset) do
|
||||
{field, {msg, _opts}} = List.first(changeset.errors)
|
||||
error_msg = Atom.to_string(field) <> " " <> msg
|
||||
%{"error" => error_msg}
|
||||
end
|
||||
|
||||
def handle_errors(conn, %{kind: kind, reason: reason}) do
|
||||
json(conn, %{error: Exception.format_banner(kind, reason)})
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,56 @@
|
||||
defmodule PlausibleWeb.AuthorizeSitesApiPlug do
|
||||
import Plug.Conn
|
||||
use Plausible.Repo
|
||||
alias Plausible.Auth.ApiKey
|
||||
|
||||
def init(options) do
|
||||
options
|
||||
end
|
||||
|
||||
def call(conn, _opts) do
|
||||
with {:ok, raw_api_key} <- get_bearer_token(conn),
|
||||
{:ok, api_key} <- verify_access(raw_api_key) do
|
||||
assign(conn, :current_user_id, api_key.user_id)
|
||||
else
|
||||
{:error, :missing_api_key} ->
|
||||
unauthorized(
|
||||
conn,
|
||||
"Missing API key. Please use a valid Plausible API key as a Bearer Token."
|
||||
)
|
||||
|
||||
{:error, :invalid_api_key} ->
|
||||
unauthorized(
|
||||
conn,
|
||||
"Invalid API key. Please make sure you're using a valid API key with access to the site you've requested."
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp verify_access(api_key) do
|
||||
hashed_key = ApiKey.do_hash(api_key)
|
||||
found_key = Repo.get_by(ApiKey, key_hash: hashed_key)
|
||||
|
||||
cond do
|
||||
found_key -> {:ok, found_key}
|
||||
true -> {:error, :invalid_api_key}
|
||||
end
|
||||
end
|
||||
|
||||
defp get_bearer_token(conn) do
|
||||
authorization_header =
|
||||
Plug.Conn.get_req_header(conn, "authorization")
|
||||
|> List.first()
|
||||
|
||||
case authorization_header do
|
||||
"Bearer " <> token -> {:ok, String.trim(token)}
|
||||
_ -> {:error, :missing_api_key}
|
||||
end
|
||||
end
|
||||
|
||||
defp unauthorized(conn, msg) do
|
||||
conn
|
||||
|> put_status(401)
|
||||
|> Phoenix.Controller.json(%{error: msg})
|
||||
|> halt()
|
||||
end
|
||||
end
|
||||
@ -1,4 +1,4 @@
|
||||
defmodule PlausibleWeb.AuthorizeApiStatsPlug do
|
||||
defmodule PlausibleWeb.AuthorizeStatsApiPlug do
|
||||
import Plug.Conn
|
||||
use Plausible.Repo
|
||||
alias Plausible.Auth.ApiKey
|
||||
@ -0,0 +1,67 @@
|
||||
defmodule PlausibleWeb.Api.ExternalSitesControllerTest do
|
||||
use PlausibleWeb.ConnCase
|
||||
import Plausible.TestUtils
|
||||
|
||||
setup [:create_user, :create_api_key, :use_api_key]
|
||||
|
||||
describe "POST /api/v1/sites" do
|
||||
test "can create a site", %{conn: conn} do
|
||||
conn =
|
||||
post(conn, "/api/v1/sites", %{
|
||||
"site" => %{
|
||||
"domain" => "some-site.domain",
|
||||
"timezone" => "Europe/Tallinn"
|
||||
}
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"domain" => "some-site.domain",
|
||||
"timezone" => "Europe/Tallinn"
|
||||
}
|
||||
end
|
||||
|
||||
test "timezone defaults to Etc/Greenwich", %{conn: conn} do
|
||||
conn =
|
||||
post(conn, "/api/v1/sites", %{
|
||||
"site" => %{
|
||||
"domain" => "some-site.domain"
|
||||
}
|
||||
})
|
||||
|
||||
assert json_response(conn, 200) == %{
|
||||
"domain" => "some-site.domain",
|
||||
"timezone" => "Etc/Greenwich"
|
||||
}
|
||||
end
|
||||
|
||||
test "domain is required", %{conn: conn} do
|
||||
conn = post(conn, "/api/v1/sites", %{})
|
||||
|
||||
assert json_response(conn, 400) == %{
|
||||
"error" => "domain can't be blank"
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
describe "PUT /api/v1/sites/shared-links/:link_name" do
|
||||
setup :create_site
|
||||
|
||||
test "can add a shared link to a site", %{conn: conn, site: site} do
|
||||
conn = put(conn, "/api/v1/sites/#{site.domain}/shared-links/Wordpress")
|
||||
|
||||
res = json_response(conn, 200)
|
||||
assert res["name"] == "Wordpress"
|
||||
assert String.starts_with?(res["url"], "http://")
|
||||
end
|
||||
|
||||
test "is idempotent find or create op", %{conn: conn, site: site} do
|
||||
conn = put(conn, "/api/v1/sites/#{site.domain}/shared-links/Wordpress")
|
||||
|
||||
%{"url" => url} = json_response(conn, 200)
|
||||
|
||||
conn = put(conn, "/api/v1/sites/#{site.domain}/shared-links/Wordpress")
|
||||
|
||||
assert %{"url" => ^url} = json_response(conn, 200)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue