Skip to content

Commit

Permalink
Add missing functions to sharepoint client
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsierant committed Oct 16, 2024
1 parent 4ea2033 commit d4154f5
Showing 1 changed file with 98 additions and 5 deletions.
103 changes: 98 additions & 5 deletions apps/api/lib/buildel/blocks/sharepoint_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ defmodule Buildel.Blocks.SharepointClient do
type: "sharepoint_client",
description: "This module is designed to interact with a SharePoint API.",
groups: ["text", "inputs / outputs"],
inputs: [Block.text_input("list")],
outputs: [Block.text_output("list")],
inputs: [Block.text_input("list"), Block.text_input("get"), Block.file_input("create")],
outputs: [
Block.text_output("list"),
Block.text_output("get"),
Block.file_output("get_file"),
Block.text_output("create")
],
ios: [],
dynamic_ios: nil,
schema: schema()
Expand Down Expand Up @@ -78,8 +83,6 @@ defmodule Buildel.Blocks.SharepointClient do
opts.tenant_id
)

IO.inspect(access_token)

{:ok,
state
|> Map.put(:access_token, access_token)
Expand Down Expand Up @@ -112,12 +115,102 @@ defmodule Buildel.Blocks.SharepointClient do
end

@impl true
def handle_input("list", {_topic, :text, text, _metadata}, state) do
def handle_input("list", {_topic, :text, _text, _metadata}, state) do
{:ok, documents} = list_documents(state)

documents = documents["value"] |> Enum.map(&%{"name" => &1["name"], "id" => &1["id"]})

state |> output("list", {:text, Jason.encode!(documents)})
end

@impl true
def handle_input("get", {_topic, :text, text, _metadata}, state) do
{:ok, document} = get_document(text, state)

document = %{
"name" => document["name"],
"id" => document["id"],
"size" => document["size"],
"mime_type" => document["file"]["mimeType"],
"download_url" => document["@microsoft.graph.downloadUrl"]
}

{:ok, %Req.Response{body: content}} = Req.new(url: document["download_url"]) |> Req.get()

{:ok, path} = Temp.path(%{suffix: Path.extname(document["name"])})
File.write!(path, content)

file_id = UUID.uuid4()

state =
state
|> output("get_file", {:binary, path}, %{
metadata: %{
file_id: file_id,
file_name: document["name"],
file_type: document["mime_type"]
}
})

state |> output("get", {:text, Jason.encode!(document)})
end

@impl true
def handle_input("create", {_name, :binary, path, metadata}, state) do
content = File.read!(path)

{:ok, res} = upload_document(content, metadata.file_name, state)

document = %{
"name" => res["name"],
"id" => res["id"],
"size" => res["size"],
"mime_type" => res["file"]["mimeType"],
"download_url" => res["@microsoft.graph.downloadUrl"]
}

state |> output("create", {:text, Jason.encode!(document)})
end

defp upload_document(file_content, file_path, state) do
url =
"https://graph.microsoft.com/v1.0/sites/#{state.site_id}/drives/#{state.drive_id}/root:/#{file_path}:/content"

headers = [
{"Authorization", "Bearer #{state.access_token}"},
{"Content-Type", "application/octet-stream"}
]

case Req.new(url: url) |> Req.put(body: file_content, headers: headers) do
{:ok, %Req.Response{status: 201, body: body}} ->
{:ok, body}

{:ok, %Req.Response{body: reason}} ->
{:error, reason}

{:error, reason} ->
{:error, reason}
end
end

defp get_document(item_id, state) do
url =
"https://graph.microsoft.com/v1.0/sites/#{state.site_id}/drives/#{state.drive_id}/items/#{item_id}"

headers = [{"Authorization", "Bearer #{state.access_token}"}]

case Req.new(url: url) |> Req.get(headers: headers) do
{:ok, %Req.Response{status: 200, body: body}} ->
{:ok, body}

{:ok, %Req.Response{body: reason}} ->
{:error, reason}

{:error, reason} ->
{:error, reason}
end
end

defp list_documents(state) do
url =
"https://graph.microsoft.com/v1.0/sites/#{state.site_id}/drives/#{state.drive_id}/root/children"
Expand Down

0 comments on commit d4154f5

Please sign in to comment.