Skip to content

Commit

Permalink
Retry many times to connect to the API when an error occurred and use…
Browse files Browse the repository at this point in the history
… the request with the middleware PathParams.
  • Loading branch information
Burgy Benjamin committed Dec 16, 2023
1 parent c7ca713 commit 240b21e
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/ksuite_middleware/ksuite_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@ defmodule KsuiteMiddleware.KsuiteClient do

plug Tesla.Middleware.BaseUrl, "https://api.infomaniak.com"
plug Tesla.Middleware.Logger, debug: false
plug Tesla.Middleware.PathParams
plug Tesla.Middleware.Headers, [{"User-Agent", "ksuite-middleware"}]
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
plug Tesla.Middleware.FollowRedirects, max_redirects: 2
plug Tesla.Middleware.Retry, max_retries: 5, delay: 10_000

@spec download(integer()) :: {:error, any()} | {:ok, Tesla.Env.t()}
def download(file_id) when is_integer(file_id),
do: get("/2/drive/#{State.get_kdrive_id()}/files/#{file_id}/download")
do:
get("/2/drive/:kdrive_id/files/:file_id/download",
opts: [path_params: [kdrive_id: State.get_kdrive_id(), file_id: file_id]]
)

@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: get("/2/drive/#{State.get_kdrive_id()}/files/#{file_id}/download", query: [as: as])
do:
get("/2/drive/:kdrive_id/files/:file_id/download",
query: [as: as],
opts: [path_params: [kdrive_id: State.get_kdrive_id(), file_id: file_id]]
)
end

0 comments on commit 240b21e

Please sign in to comment.