From dc1694e0f400fe2a0eb2116f409569fb27e8e7a7 Mon Sep 17 00:00:00 2001 From: Blake DeMarcy Date: Wed, 3 May 2017 21:35:47 -0500 Subject: [PATCH] more doc changes --- documentation/docs/api_overview.md | 225 ++++++++++++-------- documentation/docs/errors.md | 33 +-- documentation/docs/validation.md | 14 +- documentation/site/api_overview/index.html | 216 +++++++++++++++---- documentation/site/errors/index.html | 40 ++-- documentation/site/mkdocs/search_index.json | 155 +++----------- documentation/site/validation/index.html | 15 +- gendocs.sh | 9 + document_endpoints.py => mkendpoints.py | 17 +- server.py | 34 +-- 10 files changed, 443 insertions(+), 315 deletions(-) create mode 100755 gendocs.sh rename document_endpoints.py => mkendpoints.py (93%) diff --git a/documentation/docs/api_overview.md b/documentation/docs/api_overview.md index 845150a..eec562d 100644 --- a/documentation/docs/api_overview.md +++ b/documentation/docs/api_overview.md @@ -86,14 +86,16 @@ to map these responses to native exception types or signals in your language of choice. See [the full error page](errors.md) for details. +

# Authorization - +------ ## check_auth -### Args: -**target_user**: string: either a user_name or a user_id +**Arguments:** -**target_hash**: string: sha256 hash for the password to check + * __target_user__: string: either a user_name or a user_id + + * __target_hash__: string: sha256 hash for the password to check @@ -101,17 +103,17 @@ Takes the arguments `target_user` and `target_hash`, and returns boolean true or false whether the hash is valid. - --------- - +
+

# Threads & Messages - +------ ## delete_post -### Args: -**thread_id**: string: the id of the thread this message was posted in. +**Arguments:** -**post_id**: integer: the id of the target message. + * __thread_id__: string: the id of the thread this message was posted in. + + * __post_id__: integer: the id of the target message. @@ -126,16 +128,19 @@ success. If the post_id is 0, the whole thread is deleted. + +
## edit_post -### Args: -**thread_id**: string: the thread the message was posted in. +**Arguments:** -**post_id**: integer: the target post_id to edit. + * __thread_id__: string: the thread the message was posted in. -**body**: string: the new message body. + * __post_id__: integer: the target post_id to edit. -**OPTIONAL: send_raw**: boolean: set the formatting mode for the target message. + * __body__: string: the new message body. + + * __OPTIONAL: send_raw__: boolean: set the formatting mode for the target message. @@ -156,12 +161,15 @@ endpoint `set_post_raw` instead. Returns the new message object. + +
## edit_query -### Args: -**thread_id**: string: the id of the thread the message was posted in. +**Arguments:** -**post_id**: integer: the id of the target message. + * __thread_id__: string: the id of the thread the message was posted in. + + * __post_id__: integer: the id of the target message. @@ -172,10 +180,13 @@ message. Requires the arguments `thread_id` and `post_id` Returns the original message object without any formatting on success. Returns a descriptive code 4 otherwise. + +
## message_feed -### Args: -**time**: int/float: epoch/unix time of the earliest point of interest +**Arguments:** + + * __time__: int/float: epoch/unix time of the earliest point of interest @@ -206,14 +217,17 @@ You may optionally provide a `format` argument: this is treated the same way as the `thread_load` endpoint and you should refer to its documentation for more info. + +
## set_post_raw -### Args: -**thread_id**: string: the id of the thread the message was posted in. +**Arguments:** -**post_id**: integer: the id of the target message. + * __thread_id__: string: the id of the thread the message was posted in. -**value**: boolean: the new `send_raw` value to apply to the message. + * __post_id__: integer: the id of the target message. + + * __value__: boolean: the new `send_raw` value to apply to the message. @@ -231,12 +245,15 @@ You may optionally set this value as well when using `edit_post`, but if this is the only change you want to make to the message, using this endpoint instead is preferable. + +
## set_thread_pin -### Args: -**thread_id**: string: the id of the thread to modify. +**Arguments:** -**value**: boolean: `true` to pin thread, `false` otherwise. + * __thread_id__: string: the id of the thread to modify. + + * __value__: boolean: `true` to pin thread, `false` otherwise. @@ -247,14 +264,17 @@ has admin status on their account. Returns the same boolean you supply as `value` + +
## thread_create -### Args: -**body**: string: The body of the first message +**Arguments:** -**title**: string: The title name for this thread + * __body__: string: The body of the first message -**OPTIONAL: send_raw**: boolean: formatting mode for the first message. + * __title__: string: The title name for this thread + + * __OPTIONAL: send_raw__: boolean: formatting mode for the first message. @@ -264,10 +284,13 @@ string arguments `body` and `title`. If the argument `send_raw` is specified and has a non-nil value, the OP message will never recieve special formatting. + +
## thread_index -### Args: -**OPTIONAL: include_op**: boolean: Include a `messages` object with the original post +**Arguments:** + + * __OPTIONAL: include_op__: boolean: Include a `messages` object with the original post @@ -278,14 +301,17 @@ Optionally, you may supply the argument `include_op`, which, when non-nil, will include a "messages" key with the object, whose sole content is the original message (post_id 0). + +
## thread_load -### Args: -**thread_id**: string: the thread to load. +**Arguments:** -**OPTIONAL: op_only**: boolean: include only the original message in `messages` + * __thread_id__: string: the thread to load. -**OPTIONAL: format**: string: the formatting type of the returned messages. + * __OPTIONAL: op_only__: boolean: include only the original message in `messages` + + * __OPTIONAL: format__: string: the formatting type of the returned messages. @@ -297,14 +323,17 @@ Currently only "sequential" is supported. You may also supply the parameter `op_only`. When it's value is non-nil, the messages array will only include post_id 0 (the first) + +
## thread_reply -### Args: -**thread_id**: string: the id for the thread this message should post to. +**Arguments:** -**body**: string: the message's body of text. + * __thread_id__: string: the id for the thread this message should post to. -**OPTIONAL: send_raw**: boolean: formatting mode for the posted message. + * __body__: string: the message's body of text. + + * __OPTIONAL: send_raw__: boolean: formatting mode for the posted message. @@ -315,19 +344,19 @@ If the argument `send_raw` is specified and has a non-nil value, the message will never recieve special formatting. - --------- - +
+

# Tools - +------ ## db_validate -### Args: -**key**: string: the identifier for the ruleset to check. +**Arguments:** -**value**: VARIES: the object for which `key` will check for. + * __key__: string: the identifier for the ruleset to check. -**OPTIONAL: error**: boolean: when `true`, will return an API error response instead of a special object. + * __value__: VARIES: the object for which `key` will check for. + + * __OPTIONAL: error__: boolean: when `true`, will return an API error response instead of a special object. @@ -351,12 +380,15 @@ If bool == false, description is a string describing the problem. If bool == true, description is null and the provided value is safe to use. + +
## format_message -### Args: -**body**: string: the message body to apply formatting to. +**Arguments:** -**format**: string: the specifier for the desired formatting engine + * __body__: string: the message body to apply formatting to. + + * __format__: string: the specifier for the desired formatting engine @@ -364,96 +396,113 @@ Requires the arguments `body` and `format`. Applies `format` to `body` and returns the new object. See `thread_load` for supported specifications for `format`. + +
## user_map -### Args: -__No arguments__ +_requires no arguments_ Returns an array with all registered user_ids, with the usermap object populated by their full objects. - --------- - +
+

# Users - +------ ## get_me -### Args: -__No arguments__ +_requires no arguments_ Requires no arguments. Returns your internal user object, including your authorization hash. + +
## is_admin -### Args: -**target_user**: string: user_id or user_name to check against. +**Arguments:** + + * __target_user__: string: user_id or user_name to check against. Requires the argument `target_user`. Returns a boolean of whether that user is an admin. + +
## user_get -### Args: -**target_user**: string: either a user_name or a user_id +**Arguments:** + + * __target_user__: string: either a user_name or a user_id Retreive an external user object for the given `target_user`. Can be a user_id or user_name. + +
## user_is_registered -### Args: -**target_user**: string: either a user_name or a user_id +**Arguments:** + + * __target_user__: string: either a user_name or a user_id Takes the argument `target_user` and returns true or false whether they are in the system or not. + +
## user_register -### Args: -**user_name**: string: the desired display name +**Arguments:** -**auth_hash**: string: a sha256 hash of a password + * __user_name__: string: the desired display name + + * __auth_hash__: string: a sha256 hash of a password -Register a new user into the system and return the new object. -Requires the string arguments `user_name` and `auth_hash`. -Do not send User/Auth headers with this method. +Register a new user into the system and return the new user object +on success. The returned object includes the same `user_name` and +`auth_hash` that you supply, in addition to all the default user +parameters. Returns code 4 errors for any failures. + +
## user_update -### Args: -**Any of the following may be submitted:**: +**Arguments:** -**user_name**: string: a desired display name + * __Any of the following may be submitted__: -**auth_hash**: string: sha256 hash for a new password + * __user_name__: string: a desired display name -**quip**: string: a short string that can be used as a signature + * __auth_hash__: string: sha256 hash for a new password -**bio**: string: a user biography for their profile + * __quip__: string: a short string that can be used as a signature -**color**: integer: 0-6, a display color for the user + * __bio__: string: a user biography for their profile + + * __color__: integer: 0-6, a display color for the user -Receives new parameters and assigns them to the user_object -in the database. The following new parameters can be supplied: -`user_name`, `auth_hash`, `quip`, `bio`, and `color`. Any number -of them may be supplied. +Receives new parameters and assigns them to the user object. +This method requires that you send a valid User/Auth header +pair with your request, and the changes are made to that +account. -The newly updated user object is returned on success. +Take care to keep your client's User/Auth header pair up to date +after using this method. + +The newly updated user object is returned on success, +including the `auth_hash`. - --------- - +
diff --git a/documentation/docs/errors.md b/documentation/docs/errors.md index 5daa714..4144fbd 100644 --- a/documentation/docs/errors.md +++ b/documentation/docs/errors.md @@ -1,10 +1,12 @@ -Errors in BBJ are separated into 6 different codes to help -ease handling a little bit. Errors are all or nothing, there -are no "warnings". If a response has a non-false error field, -then data will always be null. An error response from the api -looks like this... +## Handling Error Responses -``` +Errors in BBJ are separated into 6 different codes, to allow easy mapping to +native exception and signaling systems available in the client's programming +language. Errors are all or nothing, there are no "warnings". If a response has +a non-false error field, then data will always be null. An error response from +the api looks like this... + +```javascript { "error": { "code": // an integer from 0 to 5, @@ -15,13 +17,18 @@ looks like this... } ``` -The codes split errors into a few categories. Some are oriented +The codes split errors into categories. Some are oriented to client developers while others should be shown directly to users. - * 0: Malformed but non-empty json input. An empty json input where it is required is handled by code 3. This is just decoding errors. The exception text is returned as description. - * 1: Internal server error. A short representation of the internal exception as well as the code the server logged it as is returned in the description. Your clients cannot recover from this class of error, and its probably not your fault if you encounter it. If you ever get one, file a bug report. - * 2: Server HTTP error: This is similar to the above but captures errors for the HTTP server rather than BBJs own codebase. The description contains the HTTP error code and server description. This notably covers 404s and thus invalid endpoint names. - * 3: Parameter error: client sent erroneous input for its method. This could mean missing arguments, type errors, etc. It generalizes errors that should be fixed by the client developer and the returned descriptions are geared to them rather than end users. - * 4: User error: These errors regard actions that the user has taken that are invalid, but not really errors in a traditional sense. The description field should be shown to users verbatim, in a clear and noticeable fashion. They are formatted as concise English sentences and end with appropriate punctuation marks. - * 5: Authorization error: This code represents an erroneous User/Auth header pair. This should trigger the user to provide correct credentials or fall back to anon mode. + * **Code 0**: Malformed but non-empty json input. An empty json input where it is required is handled by code 3. This is just decoding errors. The exception text is returned as description. + + * **Code 1**: Internal server error. A short representation of the internal exception as well as the code the server logged it as is returned in the description. Your clients cannot recover from this class of error, and its probably not your fault if you encounter it. If you ever get one, file a bug report. + + * **Code 2**: Server HTTP error: This is similar to the above but captures errors for the HTTP server rather than BBJs own codebase. The description contains the HTTP error code and server description. This notably covers 404s and thus invalid endpoint names. The HTTP error code is left intact, so you may choose to let your HTTP library or tool of choice handle these for you. + + * **Code 3**: Parameter error: client sent erroneous input for its method. This could mean missing arguments, type errors, etc. It generalizes errors that should be fixed by the client developer and the returned descriptions are geared to them rather than end users. + + * **Code 4**: User error: These errors regard actions that the user has taken that are invalid, but not really errors in a traditional sense. The description field should be shown to users verbatim, in a clear and noticeable fashion. They are formatted as concise English sentences and end with appropriate punctuation marks. + + * **Code 5**: Authorization error: This code represents an erroneous User/Auth header pair. This should trigger the user to provide correct credentials or fall back to anon mode. diff --git a/documentation/docs/validation.md b/documentation/docs/validation.md index 5f7ac56..b039941 100644 --- a/documentation/docs/validation.md +++ b/documentation/docs/validation.md @@ -1,6 +1,8 @@ +## Implementing good sanity checks in your client. + The server has an endpoint called `db_validate`. What this does is take -a `key` and a `value` and compares `value` to a set of rules specified by -`key`. This is the same function used internally by the database to scan +a `key` and a `value` argument, and compares `value` to a set of rules specified by +`key`. This is the same function used internally by the database to check values before committing them to the database. By default it returns a descriptive object under `data`, but you can specify the key/value pair `"error": True` to get a standard error response back. A standard call @@ -67,7 +69,6 @@ The following keys are currently available. The descriptions returned are friendly, descriptive, and should be shown directly to users -## Implementing good sanity checks in your client By using this endpoint, you will never have to validate values in your own code before sending them to the server. This means you can do things @@ -78,17 +79,16 @@ This is used in the elisp client when registering users and for the thread title prompt which is shown before opening a composure window. The reason for rejection is displayed clearly to the user and input window is restored. -``` +```lisp (defun bbj-sane-value (prompt key) "Opens an input loop with the user, where the response is passed to the server to check it for validity before the user is allowed to continue. Will recurse until the input is valid, then it is returned." (let* ((value (read-from-minibuffer prompt)) - (response (bbj-request! 'db_validate - 'value value 'key key))) + (response (bbj-request! 'db_validate 'value value 'key key))) (if (alist-get 'bool response) - value + value ;; return the user's input back to the caller (message (alist-get 'description response)) (sit-for 2) (bbj-sane-value prompt key)))) diff --git a/documentation/site/api_overview/index.html b/documentation/site/api_overview/index.html index 912576a..70168f7 100644 --- a/documentation/site/api_overview/index.html +++ b/documentation/site/api_overview/index.html @@ -189,19 +189,35 @@ and the json object that error contains should be inspected. (see t visualation) Errors follow a strict code system, making it easy for your client to map these responses to native exception types or signals in your language of choice. See the full error page for details.

+



Authorization

+

check_auth

-

Args:

+

Arguments:

+

Takes the arguments target_user and target_hash, and returns boolean true or false whether the hash is valid.

-
+


+

Threads & Messages

+

delete_post

-

Args:

+

Arguments:

+

Requires the arguments thread_id and post_id.

Delete a message from a thread. The same rules apply here as edit_post and edit_query: the logged in user @@ -210,12 +226,23 @@ or have admin rights. The same error descriptions and code are returned on falilure. Boolean true is returned on success.

If the post_id is 0, the whole thread is deleted.

+


edit_post

-

Args:

+

Arguments:

+

Replace a post with a new body. Requires the arguments thread_id, post_id, and body. This method verifies that the user can edit a post before commiting the change, @@ -229,18 +256,28 @@ set the message's formatting flag. However, if this is the only change you would like to make, you should use the endpoint set_post_raw instead.

Returns the new message object.

+


edit_query

-

Args:

+

Arguments:

+

Queries the database to ensure the user can edit a given message. Requires the arguments thread_id and post_id (does not require a new body)

Returns the original message object without any formatting on success. Returns a descriptive code 4 otherwise.

+


message_feed

-

Args:

-

time: int/float: epoch/unix time of the earliest point of interest

+

Arguments:

+

Returns a special object representing all activity on the board since the argument time, a unix/epoch timestamp.

{ @@ -263,11 +300,20 @@ out visually.

You may optionally provide a format argument: this is treated the same way as the thread_load endpoint and you should refer to its documentation for more info.

+


set_post_raw

-

Args:

+

Arguments:

+

Requires the boolean argument of value, string argument thread_id, and integer argument post_id. value, when false, means that the message will be passed through message formatters @@ -279,59 +325,106 @@ and the same error objects are returned for violations.

You may optionally set this value as well when using edit_post, but if this is the only change you want to make to the message, using this endpoint instead is preferable.

+


set_thread_pin

-

Args:

+

Arguments:

+

Requires the arguments thread_id and value. value must be a boolean of what the pinned status should be. This method requires that the caller is logged in and has admin status on their account.

Returns the same boolean you supply as value

+


thread_create

-

Args:

+

Arguments:

+

Creates a new thread and returns it. Requires the non-empty string arguments body and title.

If the argument send_raw is specified and has a non-nil value, the OP message will never recieve special formatting.

+


thread_index

-

Args:

-

OPTIONAL: include_op: boolean: Include a messages object with the original post

+

Arguments:

+

Return an array with all the threads, ordered by most recent activity. Requires no arguments.

Optionally, you may supply the argument include_op, which, when non-nil, will include a "messages" key with the object, whose sole content is the original message (post_id 0).

+


thread_load

-

Args:

+

Arguments:

+

Returns the thread object with all of its messages loaded. Requires the argument thread_id. format may also be specified as a formatter to run the messages through. Currently only "sequential" is supported.

You may also supply the parameter op_only. When it's value is non-nil, the messages array will only include post_id 0 (the first)

+


thread_reply

-

Args:

+

Arguments:

+

Creates a new reply for the given thread and returns it. Requires the string arguments thread_id and body

If the argument send_raw is specified and has a non-nil value, the message will never recieve special formatting.

-
+


+

Tools

+

db_validate

-

Args:

+

Arguments:

+

Requires the arguments key and value. Returns an object with information about the database sanity criteria for key. This can be used to validate user input in the client @@ -347,61 +440,104 @@ instead of the special object described below.

If bool == false, description is a string describing the problem. If bool == true, description is null and the provided value is safe to use.

+


format_message

-

Args:

+

Arguments:

+

Requires the arguments body and format. Applies format to body and returns the new object. See thread_load for supported specifications for format.

+


user_map

-

Args:

-

No arguments

+

requires no arguments

Returns an array with all registered user_ids, with the usermap object populated by their full objects.

-
+


+

Users

+

get_me

-

Args:

-

No arguments

+

requires no arguments

Requires no arguments. Returns your internal user object, including your authorization hash.

+


is_admin

-

Args:

-

target_user: string: user_id or user_name to check against.

+

Arguments:

+

Requires the argument target_user. Returns a boolean of whether that user is an admin.

+


user_get

-

Args:

-

target_user: string: either a user_name or a user_id

+

Arguments:

+

Retreive an external user object for the given target_user. Can be a user_id or user_name.

+


user_is_registered

-

Args:

-

target_user: string: either a user_name or a user_id

+

Arguments:

+

Takes the argument target_user and returns true or false whether they are in the system or not.

+


user_register

-

Args:

+

Arguments:

+ +

Register a new user into the system and return the new user object +on success. The returned object includes the same user_name and +auth_hash that you supply, in addition to all the default user +parameters. Returns code 4 errors for any failures.

+


user_update

-

Args:

-

Any of the following may be submitted::

+

Arguments:

+ +

Receives new parameters and assigns them to the user object. +This method requires that you send a valid User/Auth header +pair with your request, and the changes are made to that +account.

+

Take care to keep your client's User/Auth header pair up to date +after using this method.

+

The newly updated user object is returned on success, +including the auth_hash.

+