Published
Edited
Sep 15, 2020
1 star
Insert cell
Insert cell
async function login(username, password) {
const response = await fetch('https://d3.ru/api/auth/login/', {
body: JSON.stringify({
username,
password,
}),
headers: {
'Content-Type': 'application/json'
},
method: 'POST'
});
return await response.json();
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const button = html`<button>YARRR!!!</button>`
button.onclick = async function(event) {
const session = await login(username, password);
localStorage.setItem('sid', session.sid);
localStorage.setItem('uid', session.uid);
}
return button;
}
Insert cell
Insert cell
localStorage.getItem('sid')
Insert cell
localStorage.getItem('uid')
Insert cell
async function subscriptions(uid, sid) {
const response = await fetch('https://d3.ru/api/posts/subscriptions/', {
headers: {
'Content-Type': 'application/json',
'X-Futuware-SID': sid,
'X-Futuware-UID': uid,
},
method: 'GET'
});
return await response.json();
}
Insert cell
await subscriptions(localStorage.getItem('uid'), localStorage.getItem('sid'))
Insert cell
async function renderSubscriptions() {
const result = await subscriptions(localStorage.getItem('uid'), localStorage.getItem('sid'));
const items = result.posts
.map(post => `<li><a href="https://d3.ru/${post.id}">${post.title}</a></li>`).join('');
return html`<ul>${items}</ul>`;
}
Insert cell
await renderSubscriptions()
Insert cell
async function notes(uid, sid, username) {
const notes = [];
let page = 1;
let pageCount = 1;

const params = {
method: 'GET',
headers: { 'X-Futuware-UID': uid, 'X-Futuware-SID': sid, }
};

while (page <= pageCount) {
const response = await fetch(
`https://d3.ru/api/user_notes/?user_login=${username}&page=${page++}`, params);
const result = await response.json();

pageCount = result.page_count;
notes.push(...result.user_notes);
}

return notes;
}
Insert cell
await notes(localStorage.getItem('uid'), localStorage.getItem('sid'), 'romaklimenko')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more