implement more marvin api

master
Yorick van Pelt 2021-11-11 15:14:48 +01:00
parent 76a9301b0b
commit 82de5ef468
Signed by: yorick
GPG Key ID: D8D3CC6D951384DE
2 changed files with 47 additions and 1 deletions

View File

@ -17,6 +17,7 @@ class Marvin {
password: c.syncPassword,
pathname: "/" + c.syncDatabase
})
this.credentials = credentials
this.remote = new PouchDB(u.href)
this.db = new PouchDB("marvin")
this.synced = new Promise((resolve, reject) => {
@ -29,6 +30,51 @@ class Marvin {
})
this.root = new Taskset(this, 'root', {})
}
async api(url, data=null) {
const response = await fetch("https://serv.amazingmarvin.com/api/" + url, {
method: data ? "POST" : "GET",
body: data ? JSON.stringify(data) : null,
headers: {
"X-API-Token": this.credentials.apiToken,
'Content-Type': 'application/json',
"Accept": "application/json"
// todo: full access token
}
})
if (response.ok) {
if (response.headers.get('content-type').startsWith("application/json")) {
return response.json()
}
return response.text()
}
else throw [response.status, await response.text()]
}
async test() {
// todo: throw?
return (await this.api("test", {})) == "OK"
}
trackedItem() {
// todo: wrap in Task?
return this.api("trackedItem")
}
todayItems() {
return this.api("todayItems")
}
dueItems() {
return this.api("dueItems")
}
categories() {
return this.api("categories")
}
labels() {
return this.api("labels")
}
me() {
return this.api("me")
}
trackInfo(taskIds) {
return this.api("tracks", { taskIds })
}
}
function chunk(xs, n) {

View File

@ -1,7 +1,7 @@
const Beeminder = require('./Beeminder.js')
const Marvin = require('./Marvin.js')
const fs = require('fs')
const BEEMINDER_TOKEN = fs.readFileSync("./beeminder-token", 'utf8').strip()
const BEEMINDER_TOKEN = fs.readFileSync("./beeminder-token", 'utf8').trim()
const {DateTime, Interval, Duration} = require('luxon')
function sleep(n) {