Skip to content

Commit bc35521

Browse files
committed
more features
1 parent 14491a0 commit bc35521

File tree

8 files changed

+397
-16
lines changed

8 files changed

+397
-16
lines changed

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,71 @@
77
Simple Dropbox v2 client for Elixir.
88
work in progress
99

10+
## Features
11+
12+
* file_requests
13+
* /create
14+
* /get
15+
* /list
16+
* /update
17+
* files
18+
* /copy_batch
19+
* /copy_batch/check
20+
* /copy_reference/get
21+
* /copy_reference/save
22+
* /copy_v2
23+
* /create_folder_v2
24+
* /delete_batch
25+
* /delete_batch/check
26+
* /delete_v2
27+
* /download
28+
* /get_metadata
29+
* /get_preview
30+
* /get_temporary_link
31+
* /get_thumbnail
32+
* /get_thumbnail_batch
33+
* /list_folder
34+
* /list_folder/continue
35+
* /list_folder/get_latest_cursor
36+
* /list_folder/longpoll
37+
* /list_revisions
38+
* /move_batch
39+
* /move_batch/check
40+
* /move_v2
41+
* /permanently_delete
42+
* /restore
43+
* /save_url
44+
* /save_url/check_job_status
45+
* /search
46+
* /upload
47+
* /upload_session/append_v2
48+
* /upload_session/finish
49+
* /upload_session/finish_batch
50+
* /upload_session/finish_batch/check
51+
* /upload_session/start
52+
* paper
53+
* /docs/archive
54+
* /docs/create
55+
* /docs/download
56+
* /docs/folder_users/list
57+
* /docs/folder_users/list/continue
58+
* /docs/get_folder_info
59+
* /docs/list
60+
* /docs/list/continue
61+
* /docs/permanently_delete
62+
* /docs/sharing_policy/get
63+
* /docs/sharing_policy/set
64+
* /docs/update
65+
* /docs/users/add
66+
* /docs/users/list
67+
* /docs/users/list/continue
68+
* /docs/users/remove
69+
* users
70+
* /get_account
71+
* /get_account_batch
72+
* /get_current_account
73+
* /get_space_usage
74+
1075
## Usage
1176

1277
```sh

lib/elixir_dropbox.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ defmodule ElixirDropbox do
1313
post_request(client, "#{@base_url}#{url}", body, headers)
1414
end
1515

16+
def post_url(client, base_url, url, body \\ "") do
17+
headers = json_headers()
18+
post_request(client, "#{base_url}#{url}", body, headers)
19+
end
20+
1621
@spec upload_response(HTTPoison.Response.t) :: response
1722
def upload_response(%HTTPoison.Response{status_code: 200, body: body}), do: Poison.decode!(body)
1823
def upload_response(%HTTPoison.Response{status_code: status_code, body: body }) do
@@ -44,7 +49,7 @@ defmodule ElixirDropbox do
4449
headers = Map.merge(headers, headers(client))
4550
HTTPoison.post!("#{base_url}#{url}", data, headers) |> download_response
4651
end
47-
52+
4853
def headers(client) do
4954
%{ "Authorization" => "Bearer #{client.access_token}" }
5055
end

lib/file_requests/create.ex

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/file_requests/file_requests.ex

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
defmodule ElixirDropbox.FileRequests do
2+
import ElixirDropbox
3+
4+
@doc """
5+
Creates a file request for this user.
6+
7+
## Example
8+
deadline = %{ "deadline" => "2020-10-12T17:00:00Z" }
9+
ElixirDropbox.FileRequests.create client , "cool", "/Temp", deadline
10+
11+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#file_requests-create
12+
"""
13+
def create(client, title, destination, deadline, open \\ true) do
14+
body = %{"title" => title, "destination" => destination, "deadline" => deadline, "open" => open}
15+
result = to_string(Poison.Encoder.encode(body, []))
16+
post(client, "/file_requests/create", result)
17+
end
18+
19+
@doc """
20+
Returns the specified file request.
21+
22+
## Example
23+
ElixirDropbox.FileRequests.get client , ""
24+
25+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#file_requests-get
26+
"""
27+
def get(client, id) do
28+
body = %{"id" => id,}
29+
result = to_string(Poison.Encoder.encode(body, []))
30+
post(client, "/file_requests/get", result)
31+
end
32+
33+
@doc """
34+
Returns a list of file requests owned by this user.
35+
For apps with the app folder permission, this
36+
will only return file requests with
37+
destinations in the app folder.
38+
39+
## Example
40+
ElixirDropbox.FileRequests.list client
41+
42+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#file_requests-list
43+
"""
44+
def list(client) do
45+
post(client, "/file_requests/list", "null")
46+
end
47+
48+
@doc """
49+
Update a file request.
50+
51+
## Example
52+
deadline = %{ "deadline" => "2020-10-12T17:00:00Z" }
53+
ElixirDropbox.FileRequests.create client, "", "cool", "/Temp", deadline
54+
55+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#file_requests-list
56+
"""
57+
def update(client, id, title, destination, deadline, open \\ true) do
58+
body = %{"id" => id, "title" => title, "destination" => destination, "deadline" => deadline, "open" => open}
59+
result = to_string(Poison.Encoder.encode(body, []))
60+
post(client, "/file_requests/update", result)
61+
end
62+
end

lib/files.ex

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
defmodule ElixirDropbox.Files do
2+
@moduledoc """
3+
This module contains endpoints and data types
4+
for basic file operations.
5+
"""
26
import ElixirDropbox
37
import ElixirDropbox.Utils
48

@@ -38,13 +42,17 @@ defmodule ElixirDropbox.Files do
3842
end
3943

4044
@doc """
41-
Delete folder returns map
45+
Delete the file or folder at a given path.
46+
If the path is a folder, all its contents will be deleted too.
47+
A successful response indicates that the file or folder was deleted.
48+
The returned metadata will be the corresponding FileMetadata
49+
or FolderMetadata for the item at time of deletion, and not a DeletedMetadata object.
4250
43-
## Example
51+
## Example
4452
45-
ElixirDropbox.Files.delete_folder client, "/Path"
53+
ElixirDropbox.Files.delete_folder client, "/Homework/math/Prime_Numbers.txt"
4654
47-
More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-delete
55+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-delete_v2
4856
"""
4957
def delete_folder(client, path) do
5058
body = %{"path" => path}
@@ -102,12 +110,36 @@ defmodule ElixirDropbox.Files do
102110
post(client, "/files/move", result)
103111
end
104112

113+
@doc """
114+
Restore a file to a specific revision.
115+
116+
## Example
117+
118+
ElixirDropbox.Files.restore(client, "/root/word.docx", "a1c10ce0dd78")
119+
120+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-restore
121+
"""
105122
def restore(client, path, rev) do
106123
body = %{"path" => path, "rev" => rev}
107124
result = to_string(Poison.Encoder.encode(body, []))
108125
post(client, "/files/restore", result)
109126
end
110127

128+
@doc """
129+
Searches for files and folders.
130+
131+
## Example
132+
133+
ElixirDropbox.Files.search(client, "/root", "word.docx")
134+
135+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-search
136+
"""
137+
def search(client, path, query, start \\ 0, max_results \\ 100, mode \\ "filename") do
138+
body = %{"path" => path, "query" => query, "start" => start, "max_results" => max_results, "mode" => mode}
139+
result = to_string(Poison.Encoder.encode(body, []))
140+
post(client, "/files/search", result)
141+
end
142+
111143
@doc """
112144
Create a new file with the contents provided in the request.
113145
@@ -128,6 +160,15 @@ defmodule ElixirDropbox.Files do
128160
upload_request(client, Application.get_env(:elixir_dropbox, :upload_url), "files/upload", file, headers)
129161
end
130162

163+
@doc """
164+
Download a file from a user's Dropbox.
165+
166+
## Example
167+
168+
ElixirDropbox.Files.download client, "/mypdf.pdf"
169+
170+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-download
171+
"""
131172
def download(client, path) do
132173
dropbox_headers = %{
133174
:path => path
@@ -136,6 +177,15 @@ defmodule ElixirDropbox.Files do
136177
download_request(client, Application.get_env(:elixir_dropbox, :upload_url), "files/download", [], headers)
137178
end
138179

180+
@doc """
181+
Get a thumbnail for an image.
182+
183+
## Example
184+
185+
ElixirDropbox.Files.get_thumbnail client, "/image.jpg"
186+
187+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail
188+
"""
139189
def get_thumbnail(client, path, format \\ "jpeg", size \\ "w64h64") do
140190
dropbox_headers = %{
141191
:path => path,
@@ -146,6 +196,31 @@ defmodule ElixirDropbox.Files do
146196
download_request(client, Application.get_env(:elixir_dropbox, :upload_url), "files/get_thumbnail", [], headers)
147197
end
148198

199+
@doc """
200+
Get thumbnails for a list of images. We allow up to 25 thumbnails in a single batch.
201+
202+
## Example
203+
batch = %{ "path" => "/image.jpg", "format" => "jpeg", "size" => "w64h64"}
204+
entries = [batch]
205+
ElixirDropbox.Files.get_thumbnail_batch client, entries
206+
207+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail_batch
208+
"""
209+
def get_thumbnail_batch(client, entries) do
210+
body = %{"entries" => entries}
211+
result = to_string(Poison.Encoder.encode(body, []))
212+
post_url(client, Application.get_env(:elixir_dropbox, :upload_url), "/files/get_thumbnail_batch", result)
213+
end
214+
215+
@doc """
216+
Get a preview for a file.
217+
218+
## Example
219+
220+
ElixirDropbox.Files.get_preview client, "/mypdf.pdf"
221+
222+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-get_preview
223+
"""
149224
def get_preview(client, path) do
150225
dropbox_headers = %{
151226
:path => path
@@ -154,8 +229,32 @@ defmodule ElixirDropbox.Files do
154229
download_request(client, Application.get_env(:elixir_dropbox, :upload_url), "files/get_preview", [], headers)
155230
end
156231

157-
def get_metadata(client, path, include_media_info \\ false) do
158-
body = %{"path" => path, "include_media_info" => include_media_info}
232+
@doc """
233+
Get a temporary link to stream content of a file. This link will expire in four hours and afterwards you will get 410 Gone. Content-Type of the link is determined automatically by the file's mime type.
234+
235+
## Example
236+
237+
ElixirDropbox.Files.get_temporary_link client, "/video.mp4"
238+
239+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-get_preview
240+
"""
241+
def get_temporary_link(client, path) do
242+
body = %{"path" => path}
243+
result = to_string(Poison.Encoder.encode(body, []))
244+
post(client, "/files/get_temporary_link", result)
245+
end
246+
247+
@doc """
248+
Returns the metadata for a file or folder.
249+
250+
## Example
251+
252+
ElixirDropbox.Files.get_metadata client, "/mypdf.pdf"
253+
254+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata
255+
"""
256+
def get_metadata(client, path, include_media_info \\ false, include_deleted \\ false, include_has_explicit_shared_members \\ false) do
257+
body = %{"path" => path, "include_media_info" => include_media_info, "include_deleted" => include_deleted, "include_has_explicit_shared_members" => include_has_explicit_shared_members}
159258
result = to_string(Poison.Encoder.encode(body, []))
160259
post(client, "/files/get_metadata", result)
161260
end

lib/files/delete_batch.ex

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
defmodule ElixirDropbox.Files.DeleteBatch do
2+
import ElixirDropbox
3+
4+
@doc """
5+
Delete multiple files/folders at once.
6+
7+
## Example
8+
path_entries = [ %{"path" => "/Temp1"}, %{"path" => "/Temp2" } ]
9+
ElixirDropbox.Files.DeleteBatch.delete_batch(client, path_entries)
10+
11+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-delete_batch
12+
"""
13+
def delete_batch(client, path_entries) do
14+
body = %{"entries" => path_entries}
15+
result = to_string(Poison.Encoder.encode(body, []))
16+
post(client, "/files/delete_batch", result)
17+
end
18+
19+
@doc """
20+
Returns the status of an asynchronous job for delete_batch. If success, it returns list of result for each entry.
21+
22+
## Example
23+
24+
ElixirDropbox.Files.DeleteBatch.check(client, "")
25+
26+
More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-delete_batch-check
27+
"""
28+
def check(client, async_job_id) do
29+
body = %{"async_job_id" => async_job_id}
30+
result = to_string(Poison.Encoder.encode(body, []))
31+
post(client, "/files/delete_batch/check", result)
32+
end
33+
end

0 commit comments

Comments
 (0)