Skip to content

Instantly share code, notes, and snippets.

@bellbind
Last active September 14, 2018 08:22
Show Gist options
  • Save bellbind/c5c2d3e57818686e8d7e7e7056d3fb9e to your computer and use it in GitHub Desktop.
Save bellbind/c5c2d3e57818686e8d7e7e7056d3fb9e to your computer and use it in GitHub Desktop.
[solid][browser] retrieve WebID with solid-client JavaScript library (not work well?)
<!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>
// 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