From 82de5ef4685a19b8c37fa45448ea4fe19d8339b4 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Thu, 11 Nov 2021 15:14:48 +0100 Subject: [PATCH] implement more marvin api --- Marvin.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ index.js | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Marvin.js b/Marvin.js index 284d451..f4cf375 100644 --- a/Marvin.js +++ b/Marvin.js @@ -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) { diff --git a/index.js b/index.js index 84001c3..f2d69dd 100644 --- a/index.js +++ b/index.js @@ -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) {