-
Notifications
You must be signed in to change notification settings - Fork 7
Deleting a resource example
CI edited this page Apr 2, 2019
·
2 revisions
In this example we show how to soft- and hard-delete a resource.
Show code
var DataPeps = require("datapeps-sdk");
global["TextEncoder"] = require("text-encoding").TextEncoder;
global["TextDecoder"] = require("text-encoding").TextDecoder;
global.fetch = require("node-fetch");
global.Headers = global.fetch.Headers;
async function main() {
let aliceLogin = "aliceliddell";
let bobLogin = "bobmorane";
let charlieLogin = "charliebucket";
// Alice logs in
let aliceSession = await DataPeps.Session.login(aliceLogin, "aliceP@ssw0rd");
// Alice creates a resource for herself, Bob and Charlie
let resource = await new DataPeps.ResourceAPI(aliceSession).create(
"text",
null,
[aliceLogin, bobLogin, charlieLogin]
);
// Alice gets the resource
await new DataPeps.ResourceAPI(aliceSession).get(resource.id);
console.log("Alice gets the resource");
// Bob logs in
let bobSession = await DataPeps.Session.login(bobLogin, "bobP@ssw0rd");
// Bob gets the resource
let bobResource = await new DataPeps.ResourceAPI(bobSession).get(resource.id);
console.log("Bob gets the resource");
// Charlie logs in
let charlieSession = await DataPeps.Session.login(
charlieLogin,
"charlieP@ssw0rd"
);
// Charlie gets the resource
let charlieResource = await new DataPeps.ResourceAPI(charlieSession).get(
resource.id
);
// Bob tries to hard-delete the resource
let errorOccurred = false;
try {
await new DataPeps.ResourceAPI(bobSession).delete(resource.id);
} catch (e) {
if (
e instanceof DataPeps.Error &&
e.kind === DataPeps.ServerError.ResourceNotFound
) {
console.log(
"Bob cannot delete a resource as he is not the resource owner"
);
errorOccurred = true;
} else throw e;
}
if (!errorOccurred) {
throw "ERROR: Bob could hard-delete the resource";
}
// Bob soft-deletes (unlinks) a resource
await new DataPeps.ResourceAPI(bobSession).unlink(resource.id);
console.log("Bob soft-deletes the resource");
// Bob cannot get the resource anymore
errorOccurred = false;
try {
bobResource = await new DataPeps.ResourceAPI(bobSession).get(resource.id);
} catch (e) {
if (
e instanceof DataPeps.Error &&
e.kind === DataPeps.ServerError.ResourceNotFound
) {
console.log("Bob cannot get a resource as he has soft-deleted it");
errorOccurred = true;
} else throw e;
}
if (!errorOccurred) {
throw "ERROR: Bob could get a soft-deleted resource";
}
// Charlie can still get the resource
charlieResource = await new DataPeps.ResourceAPI(charlieSession).get(
resource.id
);
// Alice, as the resource owner, hard-deletes the resource
await new DataPeps.ResourceAPI(aliceSession).delete(resource.id);
// Charlie cannot get the resource anymore
errorOccurred = false;
try {
charlieResource = await new DataPeps.ResourceAPI(charlieSession).get(
resource.id
);
} catch (e) {
if (
e instanceof DataPeps.Error &&
e.kind === DataPeps.ServerError.ResourceNotFound
) {
console.log("Charlie cannot get a hard-deleted resource");
errorOccurred = true;
} else throw e;
}
if (!errorOccurred) {
throw "ERROR: Charlie could get a hard-deleted resource";
}
// Alice cannot get the resource anymore
errorOccurred = false;
try {
aliceResource = await new DataPeps.ResourceAPI(aliceSession).get(
resource.id
);
} catch (e) {
if (
e instanceof DataPeps.Error &&
e.kind === DataPeps.ServerError.ResourceNotFound
) {
console.log("Alice cannot get a hard-deleted resource");
errorOccurred = true;
} else throw e;
}
if (!errorOccurred) {
throw "ERROR: Alice could get a hard-deleted resource";
}
}
main().catch(e => console.log("An error occurred: ", e));
Before running the example make sure you followed the steps described here.
To fetch the code run the following command in the examples directory:
git clone https://gist.github.com/e1e4cd0f040762c38698c480e3bb6e8f.git deleting-a-resource
To run this example execute the following command in the examples directory:
node deleting-a-resource/deleting-a-resource.js
- Session
- Identity
- Resource
- Delegated access