generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPInterface.ts
66 lines (57 loc) · 1.42 KB
/
HTTPInterface.ts
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
export default class HTTPInterface {
localID: string;
async init() {
console.log('getting local ID');
await fetch(`http://localhost:5001/api/v0/id`, {
method: 'POST'
}).then(async function(response) {
if (response.ok) {
localID = await response.json();
console.log(localID);
} else {
console.error('Error in response');
console.error(response);
}
}).catch((err) => {
console.error('No local api found');
console.error(err);
});
}
async pin(hash, title) {
var ret = {
success: 0,
message: 'Something went wrong.'
}
console.log('pinning');
let add = await fetch(`http://localhost:5001/api/v0/pin/add?arg=${hash}`, {
method: 'POST'
})
.catch((err) => {
ret.message = err;
});
if (add.ok) {
console.log('added locally');
let copy = fetch(`http://localhost:5001/api/v0/files/cp?arg=/ipfs/${hash}&arg=/${encodeURIComponent(title)}`, {
method: 'POST'
});
copy.then(async (r2) => {
console.log(r2);
if (r2.ok) {
ret.message = 'Copied to your local drive.';
ret.success = 1;
} else {
ret.messeage = 'Copy did not complete. File may already exist in filesystem.';
}
});
// call complete
copy.catch((err) => { ret.message = 'file already exists'; });
return ret;
}
}
getFolder (path) {
path = path || '/';
return fetch(`http://localhost:5001/api/v0/files/ls?arg=${path}&long=true`, {
method: 'POST'
})
}
}