-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add sWorker api and complete basic decision flow * Fix lint * Fix build
- Loading branch information
Showing
8 changed files
with
265 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import axios, {AxiosInstance} from 'axios'; | ||
import {parseObj} from '../util'; | ||
|
||
export default class SworkerApi { | ||
private readonly sworker: AxiosInstance; | ||
|
||
constructor(sworkerAddr: string, to: number) { | ||
this.sworker = axios.create({ | ||
baseURL: sworkerAddr + '/api/v0', | ||
timeout: to, | ||
headers: {'Content-Type': 'application/json'}, | ||
}); | ||
} | ||
|
||
/// WRITE methods | ||
/** | ||
* Seal cid | ||
* @param cid ipfs cid | ||
* @returns seal success or failed | ||
* @throws sWorker api error | timeout | ||
*/ | ||
async seal(cid: string): Promise<boolean> { | ||
const res = await this.sworker.post( | ||
'/storage/seal', | ||
JSON.stringify({cid: cid}) | ||
); | ||
|
||
return res.status === 200; | ||
} | ||
|
||
/** | ||
* Delete both origin and sealed file by cid | ||
* @param cid ipfs cid | ||
* @returns delete success or failed | ||
* @throws sWorker api error | timeout | ||
*/ | ||
async delete(cid: string): Promise<boolean> { | ||
const res = await this.sworker.post( | ||
'/storage/delete', | ||
JSON.stringify({cid: cid}) | ||
); | ||
|
||
return res.status === 200; | ||
} | ||
|
||
/// READ methods | ||
/** | ||
* Query local free storage size | ||
* @returns free space size(GB) | ||
* @throws sWorker api error | timeout | ||
*/ | ||
async free(): Promise<number> { | ||
const res = await this.sworker.get('/workload'); | ||
|
||
if (res && res.status === 200) { | ||
const body = parseObj(res.data); | ||
return ( | ||
Number(body.srd['srd_complete']) + Number(body.srd['disk_available']) | ||
); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.