Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions doc/Rest-API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Serval REST API
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the same front matter format as other Markdown doc files, which include a link to Serval Project, and the month and year.


serval-dna presents an HTTP REST API for interacting with Serval MeshMS and Serval Rhizome bundles
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use consistent link format to refer to Serval DNA:

[Serval DNA][]

...

[Serval DNA]: http://developer.servalproject.org/dokuwiki/doku.php?id=content:servaldna:


The following documentation assumes servald is running on localhost on the default port (4110).

CORS (Cross-Origin Resource Sharing) support has been implemented in serva-dna. Cross-origin requests are accepted from any port on 'localhost' or '127.0.0.1'.

## Authentication

The REST API implements basic authentication, configured in your servald config file.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make "basic authentication" into a link to the suitable section of the relevant RFC, or Wikipedia page, whichever is easiest to link and to read.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not refer to "servald config file". One day it may not be persisted in a file. See below comment about Serval DNA configuration. Suggest

... configured in the [Serval DNA configuration][].

...

[Serval DNA configuration]: ./Servald-Configuration.md


To view the syntax for adding users to this config file, dump the servald config schema
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The configuration commands are already well documented in another Markdown file. Please link to it, do not re-quote the config schema command here, it increases the maintenance load if the config commands are ever changed. Instead, please quote the exact config options and give examples of their use. See https://github.com/servalproject/serval-dna/blob/development/doc/Mesh-Packet-Filtering.md#how-to-configure-packet-filtering for an example.


./servald config schema

## MeshMS API Methods

### List Conversations
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the GET /path... as the heading, with backticks, eg

### `GET /restful/meshms/SID/conversationlist.json`

which will produce

GET /restful/meshms/SID/conversationlist.json


GET /restful/meshms/<sid>/conversationlist.json

### List Messages (view Conversation)

GET /restful/meshms/<sid1>/<sid2>/messagelist.json

### List New Messages (since <message token>)

GET /restful/meshms/<sid1>/<sid2>/newsince/<token>/messagelist.json


### Send New Message

Used to send a message from one SID to another. Does not require an existing conversation to send messages.

POST /restful/meshms/<sid1>/<sid2>/sendmessage

The POST attributes that may be used are:

* message: The message being sent.

When sending this message, SID1 must be an identity present in your keyring.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem terminology "Your keyring". The keyring does not belong to the HTTP client, so "your" is not appropriate. Also want consistency, use either <sid1> or SID1 but not both. The clause "When sending this message," is unnecessary, it is implied in the context. Suggest:

`<sid1>` must be an identity that is currently open (unlocked) by the Serval DNA server.


### Mark Messages as "Read"

Serval stores an offset for each conversation indicating up to which message has been read. This means that specific messages cannot be marked as read, instead every message up to a point is marked as read.

To mark all messages to a SID as read

POST /restful/meshms/<sid>/readall

To mark all messages in a conversation as read

POST /restful/meshms/<sid1>/<sid2>/readall

To make all messages in a conversation up to a specific message (by its conversation offset) as read:

POST /restful/meshms/<sid1>/<sid2>/recv/<offset>/read

## Rhizome API Methods

TODO. For now, see the rhizomerestful [tests](../tests/rhizomerestful)


## JSON format

Servald compresses the JSON it outputs to remove redundant headings. Hence, the output JSON looks like this:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use terminology "compress". Simply present the structure of returned JSON table data, explaining that it is not returned as a JSON array of JSON objects to save content-length, and present the JavaScript to "parse" it, not "decompress" it. The wording on the NAF4 wiki page should have been copied almost verbatim.


{
"header":[".token","_id","service","id","version","date",".inserttime",".author",".fromhere","filesize","filehash","sender","recipient","name"],
"rows":[
["-luCgeURRKqvbRKQilANXwcAAAAAAAAA",7,"file","F81645C6693A98EB4D2727A7A3D9F478FF77CC60F68949679BFD2A7C65A29E89",1384921558580,1384921558581,1384921558648,"A8BFD18D5BF6E005E96E62188F553F0149B10AD03B47B7A35181457B2A1ABF69",1,1006,"A49C795AE4687CB14F6F1F01265C1DC6472935B125F52ADE4F166A31770E508AF2E0AF9F48D9F6826D60C7B5B820C63028EFD78AD32B9EBA5E43A2B60D74C9E8",null,null,"file6"],
...
]
}

To decompress this JavaScript, use a function similar to the following

function decompressServalJson(compressed) {
var header = compressed.header;
var rows = compressed.rows;

var records = [];

if (header === undefined || rows === undefined) {
throw "Undefined rows or headers for Serval object\n" + JSON.stringify(compressed);
}

for (var i = 0; i < rows.length; i++) {
var row = rows[i];
if (row.length != header.length) {
console.error("Illegal row/header pair in Serval object" +
"\n" + JSON.stringify(header) +
"\n" + JSON.stringify(row));
continue;
}

var record = {};
for (var j = 0; j < header.length; j++) {
record[header[j]] = row[j];
}
records.push(record);
}
return records;
}

## POST requests

Servald only allows POST requests to be [submitted using multipart/form-data](https://github.com/servalproject/serval-dna/issues/82) as blobs with explicit encoding.

To ensure Servald reponds correctly to your
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentence fragment?


An example request to send a message using CURL is shown below.

curl \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need four spaces of indent to create a Markdown code block

--basic --user harry:potter \
--form "message=Hello World;type=text/plain;charset=utf-8" \
"/restful/meshms/<sid1>/<sid2>/sendmessage"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sid1/sid2 is consumed by github. Suggest explicit code block.



The equivalent JavaScript for sending this request would be

var sid1 = 'foo';
var sid2 = 'bar';

var address = "/restful/meshms/" + sid1 +"/" + sid2 + "/sendmessage";

var username = 'demouser';
var password = 'demopassword';

var params = new FormData();
params.append('message', 'Hello World');

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
console.log(xhr.response);
}
};

xhr.open("POST", address, true);
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password));
xhr.send(params);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the end matter used in all other Markdown technical documents, copyright and license notices.