-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathElm_ipfs_api.elm
438 lines (373 loc) · 8.5 KB
/
Elm_ipfs_api.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
module Elm_ipfs_api exposing(..)
import Http exposing (..)
import Json.Decode exposing(..)
-- import UrlParser as Url exposing ((</>), (<?>), s, int, stringParam, top)
--http://localhost:5001/api/v0/cat?arg=<ipfs-path>
type alias Stub = String
type alias Multihash = String
type alias Path = String
type alias Id = String
type alias Maddr = String
type alias Key = String
type alias Value = String
type alias File = String -- urls to get the data currently
type alias Answer = String
-- to_url : String -> Ipfs -> String
-- to_url
-- type alias Errfun =
-- Result Error a -> msg
daemon_url : String
daemon_url =
"http://localhost:5001"
ipfs_msg_handler : (Result Http.Error String) -> String
ipfs_msg_handler result =
case result of
Ok val -> val
Err errtype ->
case errtype of
BadUrl addr -> addr
Timeout -> "timeout"
NetworkError -> "nerwork err"
_ -> "err"
to_req : String -> Cmd String
to_req query =
send ipfs_msg_handler <| getString (daemon_url ++ query)
args_to_str : String -> (a -> String) -> List a -> List a -> String
args_to_str argstr argfun args used_args =
case (List.head args) of
Just arg ->
let new_argstr =
if (List.member arg used_args) then
(argstr ++ "&" ++ (argfun arg))
else
argstr
in
args_to_str new_argstr argfun (List.drop 1 args) (arg::used_args)
Nothing -> argstr
files_add : File -> List Files_add_args -> Cmd Answer
files_add file args =
to_req <|
args_to_str
("/api/v0/add?")
(\arg ->
case arg of
Recursive_file_add -> "recurive=true"
Quiet_file_add -> "quiet=true"
Quieter -> "quieter=true"
Silent -> "silent=true"
Add_progress -> "progress=true"
Trickle -> "trickle=true"
Only_hash -> "only-hash=true"
Wrap_in_dir -> "wrap-with-directory=true"
Hidden -> "hidden=true"
Chunker algo -> "chunker=" ++ (toString algo)
Raw_leaves -> "raw-leaves=true"
Fscache -> "fscache=true"
)
args
[]
files_cat : Maddr -> Cmd Answer
files_cat maddr = to_req <| "/api/v0/cat?arg=" ++ maddr
files_get : Maddr -> List Files_get_args -> Cmd Answer
files_get maddr args =
to_req <|
args_to_str
("/api/v0/get?arg=" ++ maddr)
(\arg ->
case arg of
Output outpath -> "output=" ++ outpath
Archive -> "archive=true"
Compress level -> "compress=true&compression-level=" ++ (toString level)
)
args
[]
files_ls : List Files_ls_args -> Cmd Answer
files_ls args =
to_req <|
args_to_str
("/api/v0/files/ls?")
(\arg ->
case arg of
File_path f_path -> "arg=" ++ f_path
Long_list -> "l=true"
)
args
[]
-- block_get
-- block_put
-- block_stat
repo_stat : List Repo_stat_args -> Cmd Answer
repo_stat args =
to_req <|
args_to_str
("/api/v0/repo/stat?")
(\arg ->
case arg of
Human_readable -> "human=true"
)
args
[]
repo_gc : List Repo_gc_args -> Cmd Answer
repo_gc args =
to_req <|
args_to_str
("/api/v0/repo/gc?")
(\arg ->
case arg of
Quiet1 -> "quiet=true"
Stream_errors -> "stream_errors=true"
)
args
[]
-- dag_put
dag_get : Maddr -> Cmd Answer
dag_get maddr = to_req <| "/api/v0/dag/get?arg=" ++ maddr
pin_add : Maddr -> List Pin_add_args -> Cmd Answer
pin_add maddr args =
to_req <|
args_to_str
("/api/v0/pin/add?=" ++ maddr)
(\arg ->
case arg of
Recursive1 -> "recursive=true"
Progress -> "progress=true"
)
args
[]
pin_rm : Path -> List Pin_rm_args -> Cmd Answer
pin_rm maddr args =
to_req <|
args_to_str
("/api/v0/pin/rm?arg=" ++ maddr)
(\arg ->
case arg of
Recursive2 -> "recursive=true"
)
args
[]
pin_ls : List Pin_ls_args -> Cmd Answer
pin_ls args =
to_req <|
args_to_str
("/api/v0/pin/ls?")
(\arg ->
case arg of
Path path -> "arg=" ++ path
Type pin_type ->
case pin_type of
Direct -> "type=direct"
Indirect -> "type=indirect"
Recursive -> "type=recursive"
All_pins -> "type=all"
Quiet -> "quiet=true"
)
args
[]
refs_local : Cmd Answer
refs_local = to_req <| "/api/v0/refs/local"
bootstrap_list : Cmd Answer
bootstrap_list = to_req <| "/api/v0/bootstrap/list"
bootstrap_add_default : Cmd Answer
bootstrap_add_default = to_req <| "/api/v0/bootstrap/add/default"
bootstrap_rm : Cmd Answer
bootstrap_rm = to_req <| "/api/v0/bootstrap/rm/all"
bitswap_wantlist : List Bitswap_wantlist_args -> Cmd Answer
bitswap_wantlist args =
to_req <|
args_to_str
("/api/v0/bitswap/wantlist?")
(\arg ->
case arg of
Peer_want peer -> "peer=" ++ peer
)
args
[]
bitswap_stat : Cmd Answer
bitswap_stat = to_req <| "/api/v0/stats/bitswap"
bitswap_unwant : List Bitswap_unwant_args -> Cmd Answer
bitswap_unwant args =
to_req <|
args_to_str
("/api/v0/bitswap/unwant?")
(\arg ->
case arg of
Peer_unwant peer -> "peer=" ++ peer
)
args
[]
dht_findprovs : Key -> List Dht_findprovs_args -> Cmd Answer
dht_findprovs key args =
to_req <|
args_to_str
("/api/v0/dht/findprovs?arg=" ++ key)
(\arg ->
case arg of
Verbose1 -> "verbose=true"
)
args
[]
dht_get : Key -> List Dht_get_args -> Cmd Answer
dht_get key args =
to_req <|
args_to_str
("/api/v0/dht/findprovs?arg=" ++key)
(\arg ->
case arg of
Verbose2 -> "verbose=true"
)
args
[]
dht_put : Key -> Value -> List Dht_put_args -> Cmd Answer
dht_put key val args =
to_req <|
args_to_str
("/api/v0/dht/put?arg=" ++ key ++ "&" ++ val)
(\arg ->
case arg of
Ipld_format node_format ->
case node_format of
Cbor -> "format=cbor"
_ -> ""
Input_enc enc -> "input-enc=" ++ "json"
)
args
[]
-- pubsub_subscribe
-- pubsub_unsubscribe
-- pubsub_publish
-- pubsub_ls
-- pubsub_peers
-- swarm_addrs
-- swarm_connect
-- swarm_disconnect
-- swarm_peers
-- name_publish
-- name_resolve
misc_id : List Misc_id_args -> Cmd Answer
misc_id args =
to_req <|
args_to_str
("/api/v0/id?")
(\arg ->
case arg of
Peer_id p_id -> "arg=" ++ p_id
Format format -> "format=" ++ format
)
args
[]
misc_version : List Misc_version_args -> Cmd Answer
misc_version args =
to_req <|
args_to_str
("/api/v0/version?")
(\arg ->
case arg of
Number -> "number=true"
Commit -> "commit=true"
Repo -> "repo=true"
All -> "all=true"
)
args
[]
misc_ping : String -> List Misc_ping_args -> Cmd Answer
misc_ping cmd args =
to_req <|
args_to_str
("/api/v0/ping?" ++ cmd)
(\arg ->
case arg of
Count tries -> "count=" ++ (toString tries)
)
args
[]
config_get : Cmd Answer
config_get = to_req <| "/api/v0/config/show"
-- config_set
-- config_replace
log_ls : Cmd Answer
log_ls = to_req <| "/api/v0/log/ls"
log_tail : Cmd Answer
log_tail = to_req <| "/api/v0/log/tail"
log_level : String -> String -> Cmd Answer
log_level log_identifier level =
to_req <| "/api/v0/log/level?" ++ log_identifier ++ level
-- key_get
-- key_list
type Files_add_args
= Recursive_file_add
| Quiet_file_add
| Quieter
| Silent
| Add_progress
| Trickle
| Only_hash
| Wrap_in_dir
| Hidden
| Chunker Int
| Raw_leaves
| Fscache
type Files_get_args
= Output String
| Archive
| Compress Int
type Files_ls_args
= File_path String
| Long_list
type Repo_stat_args
= Human_readable
type Repo_gc_args
= Quiet1
| Stream_errors
type Pin_add_args
= Recursive1
| Progress
type Pin_rm_args
= Recursive2
type Pin_ls_args
= Path String
| Type Pin_type
| Quiet
type Pin_type
= Direct
| Indirect
| Recursive
| All_pins
type Bitswap_wantlist_args
= Peer_want String
type Bitswap_unwant_args
= Peer_unwant String
type Dht_findprovs_args
= Verbose1
type Dht_get_args
= Verbose2
type Node_format
= Cid
| Ipld_node_interface
| Ipld_resolver
| Cbor
| Merkledag_Protobuf
| Raw
| Unixfs_v2
| Git
| Bitcoin
| Zcash
| Ethereum
| Bencode
| Torrent_info
| Torrent_file
| Ipld_selectors
type Enc
= Json
type Dht_put_args
= Ipld_format Node_format
| Input_enc Enc
type Misc_id_args
= Peer_id Id
| Format String
type Misc_version_args
= Number
| Commit
| Repo
| All
type Misc_ping_args
= Count Int