Question on the low level API

There is a few problems that i met, when i try to play around with the API and see what i can do to create a dynamic website without using PHP or any other server side script.

The reason that i play around with the API is because that i was told that, the data is stored inside the network using the api which is something like redis.

I had started building app, i had try to use google postman to store and retrieve the data. However i found that i am facing one problems here.

I had try creating window’s app using node.js and electron and i am able to authenticate when the moment i launch the app. However, when i try to authenticate in an webapp, which i upload all my js file to the safe demo app and run the website in safe browser 0.4.2, http://localhost:8100 is not usable anymore and it wont authenticate.

What should be the host that i need to use when i need to use the API in the safe browser/demo app or whatever it is called.

Other than this, when i create a structured data, it returns me a handle id, does that handle id act like primary key in mysql database?

If so, when the moment i get the handle id, where do i store that handleid so that whenever next time i need it i am able to retrieve that data from that specific handle id, i try my best to read through the API tutorial but i get no clues on it

For example in mysql, it will be like select name from table where id = that id.
In a short word, how can i know this data belongs to which id.

I saw the API had this fetch data stuff, but all those required a handleId before they are able to return me anything.

Other than this, this might be out of topic, but i hope someone is able to answer me. If i create a window application the file is not inside safe network, but i am storing all the data in safe network for this window application, does this application count as a safe app?

Apologize if my question sounds stupid but i am not really good in javascript and also this is very new to me, so i hope someone can guide me through all this.

Thank You.

Hey @peter910921 good to see you digging in here.

I’m a bit rusty so can’t answer you directly but take a look at App Zero by @whiteoutmashups for the URL. It might be http://api.safenet you need but I’m not sure from memory. Also - you can always examine one of the web apps others have uploaded using the browser console to examine their source code.

I can’t answer the “handle” question, but this will be changing soon anyway as the API is replaced by SAFE node.js which is simpler to use - not yet documented but you can find it in the MaidSafe repo.

I think a Windows app that uses SAFEnetwork is a SAFE App :slight_smile:! There’s no definition but maybe it’s not as “safe” in some respects.

I am also pretty curious about the right way to store a reference to an object in the safeNET on disk. The Launcher API docs provide a way to get pointers to things called data-ids (so you can have a data-id which points to a structured or appendable data for example). The api also provides a way to serialize these data-ids into short buffers, which sure seem like they should correspond to XorNames that can be looked up on the network. Unfortunately, while I can make a structured data, then convert that to a data-id, serialize the data id, and deserialize the returned binary blob, I keep running into trouble when it comes to the final step of converting the data-id back into a structured id.

Structured data has its own serialize/deserialize API, and I’m currently trying that to see if it works, but I’m running into trouble with the deserialize API. Experience tells me that I’m just doing something silly, but I’m not sure.

In any case it would be great to hear from someone in the know about the right way to store references to objects on the safeNET.

1 Like

Hey @happybeing
Thanks for the reply, after digging few topics about the url host i finally figure out how to auth in a web app. However, i think i am still very confuse about the low level API, i guess i need to dig through more different information from this forum.

2 Likes

As the API will change with the new mutable data (and the disappearance of structured and appendable data), maybe is better to start take notice of the new API which, moreover, seems much more clear.

2 Likes

Did you try to use the Get structured data handle endpoint? What issue are you running in?

No, a handle ID is just a temporary reference that points to an object which is stored in an object cache (in memory). If you restart SAFE Launcher, all the previously obtained handles are gone and your application will need to fetch new handles.

To store a permanent reference to a structured data, you could serialize the data ID of that structured data using a data ID handle and store the serialized data ID somewhere.

4 Likes

That’s the endpoint I’m using. It just failes with a NoSuchData error.

Now that I have a little more time, I’ll post some more detail error info. I get the following response when I try to convert a dataID (which is successfully recovered from the XorName) into a StructuredData (I’ve taken some stuff out to make it shorter):

{"request":{"uri":
    {"protocol":"http:",
     ...
    "host":"localhost:8100",
    "port":"8100",
    "hostname":"localhost",
    ...
    "pathname": "/structured-data/handle/8",
    "path":"/structured-data/handle/8",
    "href":"http://localhost:8100/structured-data/handle/8"},
    "method":"GET",
    "headers":{
           "authorization": ...,
           "accept":"application/json"
     }},
     "message":{
            "statusCode":404,
             "body":{
                      "errorCode":-18,
                      "description":"CoreError::GetFailure::GetError::NoSuchData"
            },
            "headers":{ ... },
            "request":{ ... }

I get this after invoking the GET endpoint that you mentioned. This endpoint works for me when I convert from a StructuredDataHandle to a DataIdHandle and back again, but not when I add the extra step of going all the way to an XorName (what I’m calling the thin returned by DataId.serialize).

Could you please upload your code on GitHub so that I can take a look? :slight_smile:

I just tried to go through all the steps using a REST API Client (Insomnia) and everything seems to work fine.

1 Like

Here is the test that is failing: https://github.com/ethanpailes/frames/blob/attempts-at-video-recovery/db/low-level/spec/structured_data_spec.ts#L74

The relevant structured data client file is: https://github.com/ethanpailes/frames/blob/attempts-at-video-recovery/db/low-level/src/ts/structured-data.ts

The client for dataID can be found here: https://github.com/ethanpailes/frames/blob/attempts-at-video-recovery/db/low-level/src/ts/data-id.ts

2 Likes

Actually, could you post some code where you have this working? It might be easier for me to replicate the working code than for you to wade through all of my typescript.

1 Like

Thank you for sharing this source code! I just found what the issue is :slight_smile:

You need to set encoding: null when serializing a data ID: frames/db/low-level/src/ts/data-id.ts at master · ethanpailes/frames · GitHub

I found this while looking at the documentation of request. In your application, you use web-request, which is built on top of request, so it has similar request options.

encoding - Encoding to be used on setEncoding of response data. If null, the body is returned as a Buffer. Anything else (including the default value of undefined) will be passed as the encoding parameter to toString() (meaning this is effectively utf8 by default). (Note: if you expect binary data, you should set encoding: null.)

See also this Stack Overflow question: javascript - Getting binary content in Node.js using request - Stack Overflow

3 Likes

I took another look at it myself this morning armed with the knowledge that you thought it should work, and I found the bug. Sometimes having faith in the underlying tech is all it takes.

Thank you so much for taking a hard look at this! The SAFE developer community is great!

I’m sorry to send you on this wild goose chase.

3 Likes