solid-auth-client icon indicating copy to clipboard operation
solid-auth-client copied to clipboard

Add write example to README (for example PUT)

Open melvincarvalho opened this issue 7 years ago • 6 comments

There is a section on the README called reading and writing data. However it only shows how to read, and not write. I think it would be helpful to add an example to write data with an HTTP PUT.

Reading and writing data

The fetch method mimics the browser's fetch API. You can use it to access any kind of HTTP(S) document, regardless of whether that document is on a Solid pod:

solid.auth.fetch('https://timbl.com/timbl/Public/friends.ttl')
  .then(console.log);
const { fetch } = solid.auth;
fetch('https://timbl.com/timbl/Public/friends.ttl')
  .then(console.log);

If the document is on a Solid pod, and the user is logged in, they will be able to access private documents that require read or write permissions.

https://github.com/solid/solid-auth-client#reading-and-writing-data

melvincarvalho avatar Oct 13 '18 08:10 melvincarvalho

Yea, as a user I've been testing and trying out different folder/file operations: My current write looks like

        solid.auth.fetch('https://roger.localhost:8443/public/'+file, {
           method: 'PUT', // or 'PUT'
           body: data // data can be `string` or {object}!
           }).then(res => {return res;})
        .then((response) => {callback(null);})
        .catch(error => callback('Error: '+JSON.stringify(error)));

where I'm serving a node-solid-server at https://localhost:8443/ that I setup with developer credentials and can get the current session and my webId earlier. and file is the file name data is a string of a text file.

headers can be added to set Content-Type but this one is text/plain

Managed to DELETE as well with

solid.auth.fetch('https://roger.localhost:8443/public/'+path, {
  method: 'DELETE'
}).then(res => {return res;})
.then((response) => {callback(null);})
.catch(error => callback('Error: '+JSON.stringify(error)));

where path is the relative path to the file to delete.

Trying to create a pod folder but not working yet

rimmartin avatar Oct 13 '18 18:10 rimmartin

@rimmartin very nice!

Here's the code I also found in solid-yo

    const query = `
      INSERT DATA {
        [] <${AS}actor>   <${this_escape(from)}>;
           <${AS}to>      <${this_escape(to)}>;
           <${AS}summary> "Yo"@en.
      }`
    // Send a PATCH request to update the source
    const response = await this._fetch(this._source, {
      method: 'PATCH',
      headers: { 'Content-Type': 'application/sparql-update' },
      body: query,
      credentials: 'include',
    });

melvincarvalho avatar Oct 13 '18 19:10 melvincarvalho

To put in more folders my latest attempt is by applying rdflib.js to the Contaier

        const LDP = this.rdf.Namespace('http://www.w3.org/ns/ldp#>');
        let folder = this.rdf.sym('https://roger.localhost:8443/public/');  // NOTE: Ends in a slash
        let childFolder = this.rdf.sym('https://roger.localhost:8443/public/'+path+'/');  // NOTE: Ends in a slash
        let ins = this.store.add(folder, LDP('contains'), childFolder, folder.doc());
        let del = [];
        this.updater.update(del, [ins], (uri, ok, message) => {
          if (ok) console.log('contains '+ path);
          else alert(message);
        });

It gets into the store [IndexedFormula] alright and I can list it on the container but the updater throws

Exception in update: Error: Update: Loaded <https://roger.localhost:8443/public/>but stil can't figure out what editing protcol it supports.

Close to working; I may have a cors issue yet I'm testing all on the same server

rimmartin avatar Oct 14 '18 14:10 rimmartin

I agree we should add a write example to the README, but we should be careful this does not become a Solid read/write tutorial. A write example would be good to show how solid-auth-client works, but other examples are more about how Solid works, and they belong in the Solid docs.

@melvincarvalho Do we currently have such examples on the website? If not, we might want to make a separate issue for that.

RubenVerborgh avatar Oct 15 '18 08:10 RubenVerborgh

Do we currently have such examples on the website? If not, we might want to make a separate issue for that

@RubenVerborgh there may be, but im not aware of something with solid.auth.fetch.

but we should be careful this does not become a Solid read/write tutorial

Agreed. So the question is whether to use PUT, POST or PATCH. I feel PUT would be most suitable for the target audience.

melvincarvalho avatar Oct 15 '18 08:10 melvincarvalho

Ok, learned to use rdflib less and this api more:-)

        var n=path.lastIndexOf('/');
        solid.auth.fetch('https://roger.localhost:8443/public/'+path.substr(0,n+1), {
           method: 'POST', // or 'PUT'
           headers:{
            'Content-Type': 'text/turtle',
	    'Link': '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"',
            'Slug':  path.substr(n+1)
	   }
           }).then((res) => {return res;})
        .then((response) => {callback(null);})
        .catch(error => callback('Error: '+JSON.stringify(error)));

will create an empty container where path = "scad/scad-poser"

Intermediate containers aren't made so I plan to step thru and create them if they don't already exist

Looks like I'll be able to put solid to use:-)

rimmartin avatar Oct 17 '18 01:10 rimmartin