-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a new endpoint to return the events of the given calendar iden…
…tifier.
- Loading branch information
Burgy Benjamin
committed
Dec 16, 2023
1 parent
e090027
commit f9f7c7e
Showing
13 changed files
with
251 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
defmodule KsuiteMiddleware.KsuiteClient do | ||
require Logger | ||
use Tesla | ||
|
||
alias KsuiteMiddleware.State | ||
|
||
require Logger | ||
|
||
plug Tesla.Middleware.BaseUrl, "https://api.infomaniak.com" | ||
plug Tesla.Middleware.Logger, debug: false | ||
plug Tesla.Middleware.Headers, [{"User-Agent", "ksuite-middleware"}] | ||
plug Tesla.Middleware.Headers, [{"Authorization", "Bearer #{Application.get_env(:ksuite_middleware, :ksuite_api_token)}"}] | ||
plug Tesla.Middleware.Headers, [{"Authorization", "Bearer #{State.get_ksuite_api_token()}"}] | ||
plug Tesla.Middleware.Headers, [{"Content-Type", "application/json"}] | ||
plug Tesla.Middleware.FollowRedirects, max_redirects: 1 | ||
|
||
@spec download(integer()) :: {:error, any()} | {:ok, Tesla.Env.t()} | ||
def download(file_id) when is_integer(file_id) do | ||
kdrive_id = Application.get_env(:ksuite_middleware, :kdrive_id) | ||
get("/2/drive/#{kdrive_id}/files/#{file_id}/download") | ||
end | ||
def download(file_id) when is_integer(file_id), | ||
do: get("/2/drive/#{State.get_kdrive_id()}/files/#{file_id}/download") | ||
|
||
@spec download_as(integer(), bitstring()) :: {:error, any()} | {:ok, Tesla.Env.t()} | ||
def download_as(file_id, as \\ "pdf") when is_integer(file_id) and is_bitstring(as) do | ||
kdrive_id = Application.get_env(:ksuite_middleware, :kdrive_id) | ||
get("/2/drive/#{kdrive_id}/files/#{file_id}/download", query: [as: as]) | ||
end | ||
def download_as(file_id, as \\ "pdf") when is_integer(file_id) and is_bitstring(as), | ||
do: get("/2/drive/#{State.get_kdrive_id()}/files/#{file_id}/download", query: [as: as]) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
defmodule KsuiteMiddleware.State do | ||
alias Timex.TimezoneInfo | ||
use GenServer | ||
|
||
# Client | ||
|
||
def start_link(default), do: GenServer.start_link(__MODULE__, default, name: State) | ||
|
||
@spec get_ksuite_api_token() :: String.t() | ||
def get_ksuite_api_token(), do: GenServer.call(State, :get_ksuite_api_token) | ||
|
||
@spec get_kdrive_id() :: String.t() | ||
def get_kdrive_id(), do: GenServer.call(State, :get_kdrive_id) | ||
|
||
@spec get_caldav_client() :: CalDAVClient.Client.t() | ||
def get_caldav_client(), do: GenServer.call(State, :get_caldav_client) | ||
|
||
@spec get_timezone() :: Timex.TimezoneInfo.t() | ||
def get_timezone(), do: GenServer.call(State, :get_timezone) | ||
|
||
# Callbacks | ||
|
||
@impl true | ||
def init(_) do | ||
{:ok, %{}} | ||
end | ||
|
||
@impl true | ||
def handle_call(:get_ksuite_api_token, _from, %{ksuite_api_token: x} = state), | ||
do: {:reply, x, state} | ||
|
||
@impl true | ||
def handle_call(:get_ksuite_api_token, _from, state) do | ||
api_token = Application.get_env(:ksuite_middleware, :ksuite_api_token) | ||
{:reply, api_token, Map.put_new(state, :ksuite_api_token, api_token)} | ||
end | ||
|
||
@impl true | ||
def handle_call(:get_kdrive_id, _from, %{kdrive_id: x} = state), | ||
do: {:reply, x, state} | ||
|
||
@impl true | ||
def handle_call(:get_kdrive_id, _from, state) do | ||
kdrive_id = Application.get_env(:ksuite_middleware, :kdrive_id) | ||
{:reply, kdrive_id, Map.put_new(state, :kdrive_id, kdrive_id)} | ||
end | ||
|
||
@impl true | ||
def handle_call(:get_caldav_client, _from, %{caldav_client: x} = state), | ||
do: {:reply, x, state} | ||
|
||
@impl true | ||
def handle_call(:get_caldav_client, _from, state) do | ||
username = Application.get_env(:ksuite_middleware, :caldav_username) | ||
password = Application.get_env(:ksuite_middleware, :caldav_password) | ||
server = Application.get_env(:ksuite_middleware, :caldav_server) | ||
|
||
client = %CalDAVClient.Client{ | ||
server_url: server, | ||
auth: %CalDAVClient.Auth.Basic{username: username, password: password} | ||
} | ||
|
||
{:reply, client, Map.put_new(state, :caldav_client, client)} | ||
end | ||
|
||
@impl true | ||
def handle_call(:get_timezone, _from, %{timezone: x} = state), do: {:reply, x, state} | ||
|
||
@impl true | ||
def handle_call(:get_timezone, _from, state) do | ||
with %TimezoneInfo{} = timezone <- Application.get_env(:ksuite_middleware, :timezone) |> Timex.Timezone.get() do | ||
{:reply, timezone, Map.put_new(state, :timezone, timezone)} | ||
else | ||
{:error, :time_zone_not_found} -> raise("The environment variable TIMEZONE was missing.") | ||
end | ||
end | ||
end |
101 changes: 101 additions & 0 deletions
101
lib/ksuite_middleware_web/controllers/calendar_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
defmodule KsuiteMiddlewareWeb.CalendarController do | ||
use KsuiteMiddlewareWeb, :controller | ||
|
||
alias KsuiteMiddlewareWeb.Models.KsuiteCalendarEvent | ||
alias KsuiteMiddleware.State | ||
alias Timex.TimezoneInfo | ||
|
||
require Logger | ||
|
||
def get_events(conn, %{"from" => from, "to" => to, "calendar_id" => calendar_id}), | ||
do: | ||
parse_params(from, to, calendar_id) | ||
|> then(&read_events/1) | ||
|> then(&translate_to_events/1) | ||
|> then(&send_response(conn, &1)) | ||
|
||
# Private | ||
|
||
defp read_events({:error, error, reason}), do: {:error, error, reason} | ||
|
||
defp read_events({:ok, from, to, calendar_id}) do | ||
client = State.get_caldav_client() | ||
%CalDAVClient.Client{auth: %CalDAVClient.Auth.Basic{username: username}} = client | ||
calendar_url = CalDAVClient.URL.Builder.build_calendar_url(username, calendar_id) | ||
|
||
Logger.info("Reading the events from the calendar #{calendar_id} ...") | ||
|
||
client |> CalDAVClient.Event.get_events(calendar_url, from, to) | ||
end | ||
|
||
defp send_response(conn, {:ok, events}), do: json(conn, events) | ||
|
||
defp send_response(conn, {:error, error, reason}), | ||
do: | ||
conn | ||
|> put_status(error) | ||
|> json(%{reason: reason}) | ||
|
||
defp send_response(conn, {:error, reason}), | ||
do: | ||
conn | ||
|> put_status(500) | ||
|> json(%{reason: reason}) | ||
|
||
defp parse_params(from, to, calendar_id) do | ||
Logger.info("Parsing the arguments ...") | ||
|
||
with {:ok, from} <- parse_datetime(:invalid_from, from), | ||
{:ok, to} <- parse_datetime(:invalid_to, to), | ||
{:ok, calendar_id} <- parse_calendar_id(calendar_id) do | ||
{:ok, from, to, calendar_id} | ||
else | ||
:invalid_to -> {:error, :bad_request, "The argument 'to' was invalid."} | ||
:invalid_from -> {:error, :bad_request, "The argument 'from' was invalid."} | ||
{:invalid_calendar_id, reason} -> {:error, :bad_request, reason} | ||
end | ||
end | ||
|
||
defp parse_calendar_id(calendar_id) when byte_size(calendar_id) <= 100, do: {:ok, calendar_id} | ||
defp parse_calendar_id(_), do: {:invalid_calendar_id, "The calendar_id was too long."} | ||
|
||
defp parse_datetime(error_atom, datetime) when is_atom(error_atom) and is_bitstring(datetime) do | ||
case Timex.parse!(datetime, "{ISO:Extended:Z}") do | ||
%DateTime{} = x -> {:ok, x} | ||
_ -> error_atom | ||
end | ||
end | ||
|
||
defp translate_to_events({:error, :unauthorized}), do: {:error, :unauthorized, "Unauthorized access to the CalDAV server, double check your credentials."} | ||
defp translate_to_events({:error, :not_found}), do: {:error, :not_found, "The given calendar_id was not found in the given server CalDAV."} | ||
defp translate_to_events({:error, reason}), do: {:error, reason} | ||
defp translate_to_events({:error, error, reason}), do: {:error, error, reason} | ||
defp translate_to_events({:ok, []}), do: {:ok, []} | ||
defp translate_to_events({:ok, icalendar_events}), do: translate_to_events(icalendar_events, []) | ||
defp translate_to_events([], acc) when is_list(acc), do: {:ok, acc} | ||
|
||
defp translate_to_events([head | tail], acc) when is_list(acc) do | ||
%CalDAVClient.Event{icalendar: icalendar_event} = head | ||
[%ICalendar.Event{} = event] = ICalendar.from_ics(icalendar_event) | ||
%ICalendar.Event{summary: summary, dtstart: from, dtend: to, description: description} = event | ||
|
||
# Because the start and end date don't contain the timezone when it is a daily event, we have to recompute the datetime to the configured timezone. | ||
from = translate_date_to_utc(from) | ||
to = translate_date_to_utc(to) | ||
|
||
new_event = %KsuiteCalendarEvent{subject: summary, from: Timex.format!(from, "{ISO:Extended:Z}"), to: Timex.format!(to, "{ISO:Extended:Z}"), description: description} | ||
|
||
translate_to_events(tail, [new_event | acc]) | ||
end | ||
|
||
defp translate_date_to_utc(%DateTime{time_zone: "Etc/UTC", hour: 0, minute: 0, second: 0} = datetime) do | ||
%DateTime{year: y, month: m, day: d} = datetime | ||
%TimezoneInfo{full_name: tz} = State.get_timezone() | ||
|
||
Timex.to_date({y, m, d}) | ||
|> Timex.to_datetime(tz) | ||
|> Timex.Timezone.convert(:utc) | ||
end | ||
|
||
defp translate_date_to_utc(datetime), do: datetime | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
defmodule KsuiteMiddlewareWeb.Models.KsuiteCalendarEvent do | ||
@enforce_keys [:subject, :from, :to, :description] | ||
@derive Jason.Encoder | ||
defstruct [:subject, :from, :to, :description] | ||
@type t :: %__MODULE__{subject: String.t(), from: String.t(), to: String.t(), description: String.t()} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters