Last active
September 14, 2018 08:22
-
-
Save bellbind/c5c2d3e57818686e8d7e7e7056d3fb9e to your computer and use it in GitHub Desktop.
[solid][browser] retrieve WebID with solid-client JavaScript library (not work well?)
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
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<script | |
src="https://solid.github.io/releases/solid.js/solid-client.min.js" | |
></script> | |
</head> | |
<body> | |
<h1>NOTE: watch on webconsole messages</h2> | |
<input id="webid" size="80" /> | |
<button onclick="login()">login</button> | |
<button onclick="signup()">signup</button> | |
<script src="script.js"></script> | |
</body> | |
</html> |
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
// by https://github.com/solid/solid-client | |
// [retrieve WebID] | |
console.log(`[NOTICE: Only works WebID on databox.me]`); | |
(async () => { | |
// by solid-auth-tls, `currentUser` is alias of `login`? | |
const webId = await SolidClient.currentUser(); | |
if (webId) { | |
console.log(`Current WebID: ${webId}`); | |
if (webId) webIdRetrieved(webId); | |
} else { | |
console.log(`no WebID`); | |
} | |
})(); | |
const login = () => (async () => { | |
try { | |
const inputWebId = document.getElementById("webid").value.trim(); | |
const endpoint = new URL(inputWebId); | |
endpoint.pathname = endpoint.hash = ""; | |
console.log(endpoint.href); | |
const webId = await SolidClient.login(inputWebId); | |
//const webId = await SolidClient.login(inputWebId, { | |
// authEndpoint: endpoint.href}); | |
//const webId = await SolidClient.login(inputWebId, endpoint.href); | |
console.log(`login WebID: ${webId}`); | |
console.log(`same with currentUser()?`, | |
webId === await SolidClient.currentUser()); //why false?? | |
if (webId) webIdRetrieved(webId); | |
} catch (err) { | |
console.error(`login error:`, err); | |
} | |
})(); | |
const signup = () => (async () => { | |
try { | |
const webId = await SolidClient.signup(); | |
console.log(`signup WebID: ${webId}`); | |
console.log(`same with currentUser()?`, | |
webId === await SolidClient.currentUser()); | |
if (webId) webIdRetrieved(webId); | |
} catch (err) { | |
console.error(`signup error`, err); | |
} | |
})(); | |
async function webIdRetrieved(webId) { | |
document.getElementById("webid").value = webId; | |
const profile = await SolidClient.getProfile(webId); | |
const ns = SolidClient.vocab; | |
console.log(`profile name`, profile.name); | |
console.log(`profile inbox`, profile.find(ns.solid(`inbox`))); | |
console.log(`profile sameAs`, profile.findAll(ns.owl(`sameAs`))); | |
const tr = await profile.loadTypeRegistry(); | |
console.log(tr); //=> ?? | |
const regs = await tr.typeRegistryForClass(ns.vcard(`AddressBook`)); | |
console.log(regs); //=> []?? | |
// resource access | |
const url = new URL(webId); | |
url.pathname = url.hash = ""; | |
await scanUri(url.href); | |
return; | |
// paste bin | |
const binsUrl = new URL(url.href); | |
binsUrl.pathname = `/Inbox/`; //Is OK to use /Inbox/ to post wild app data? | |
await createBin(binsUrl.href); | |
} | |
// [resource access] | |
async function scanUri(uri) { | |
const response = await SolidClient.web.get(uri); | |
console.log(`response.isContainer()`, response.isContainer()); | |
if (response.isContainer()) { | |
await scanContainer(response.resource); | |
} else { | |
await scanResource(response.resource); | |
} | |
} | |
async function scanContainer(container) { | |
console.log(container); | |
console.log(`container.uri`, container.uri); | |
console.log(`container.name`, container.name); | |
console.log(`container.types`, container.types); | |
console.log(`container.contentsUris`, container.contentsUris); | |
for (const uri of container.contentsUris) await scanUri(uri); | |
} | |
async function scanResource(resource) { | |
console.log(resource); | |
console.log(`resource.uri`, resource.uri); | |
console.log(resource.response.raw()); | |
const ns = SolidClient.vocab; | |
const graph = resource.parsedGraph; | |
console.log(resource.parsedGraph); | |
} | |
// pastebin example from https://github.com/solid/solid-tutorial-intro | |
async function createBin(postUri) { | |
// bin data | |
const bin = {title: "Hello Bin", body: "Hello World!"}; | |
const $rdf = SolidClient.rdflib; | |
const ns = SolidClient.vocab; | |
const graph = $rdf.graph(); | |
const self = $rdf.sym(``); | |
//[Warning] Relative URIs will fail in future versions | |
graph.add(self, ns.dct(`title`), bin.title); | |
graph.add(self, ns.sioc(`content`), bin.body); | |
const data = new $rdf.Serializer(graph).toN3(graph); | |
console.log(data); | |
// post | |
const meta = await SolidClient.web.post(postUri, data); | |
console.log(meta); | |
console.log(`meta.url`, meta.url); | |
// fetch it | |
const res = await SolidClient.web.get(meta.url); | |
console.log(`res.url`, res.url); | |
//console.log(res.raw()); | |
const g = res.parsedGraph(); | |
console.log(g); | |
const subj = $rdf.sym(res.url); | |
console.log(`title`, g.anyValue(subj, ns.dct(`title`))); | |
console.log(`content`, g.anyValue(subj, ns.sioc(`content`))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Open the page via HTTPS
Refs