Created
June 4, 2018 23:29
-
-
Save woky/0434b22d30ced28d76d7c5a8effb6d95 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export class Pinboard { | |
constructor(apiToken) { | |
this.authToken = apiToken; | |
} | |
fetchApi(path, params = {}) { | |
let urlParams = new URLSearchParams(); | |
for (let [key, value] of Object.entries(params)) | |
urlParams.append(key, value); | |
urlParams.append('auth_token', this.authToken); | |
urlParams.append('format', 'json'); | |
let url = new URL('https://api.pinboard.in/v1' + path); | |
url.search = urlParams.toString(); | |
return fetch(url.toString()).then(reply => reply.json()); | |
} | |
async lastUpdateTime() { | |
let response = await this.fetchApi('/posts/update'); | |
let dt = new Date(response['update_time']); | |
return dt.getTime(); | |
} | |
async get(params) { | |
return (await this.fetchApi('/posts/get', params)).posts; | |
} | |
add(params) { | |
return this.fetchApi('/posts/add', params); | |
} | |
delete(url) { | |
return this.fetchApi('/posts/delete', { url: url }); | |
} | |
all() { | |
return this.fetchApi('/posts/all'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment