From 86499de120e2f0565f788b04c26f905d1054eb1e Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Fri, 9 Aug 2019 10:03:10 +0300 Subject: [PATCH] APIv2. Closes #4 --- README.md | 196 +++++++++++++++++++++++++++++++++++++++++++++++++++- rest/api.go | 8 +++ rest/go.mod | 9 +++ rest/go.sum | 6 ++ 4 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 rest/go.mod create mode 100644 rest/go.sum diff --git a/README.md b/README.md index ef0d96b..96bc772 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Zookeeper REST API - [Zoorest](#zoorest) - [Usage](#usage) - - [API](#api) + - [API v1](#api-v1) - [List node childrens](#list-node-childrens) - [Errors](#errors) - [Get node data](#get-node-data) @@ -20,7 +20,20 @@ Zookeeper REST API - [Update node](#update-node) - [Errors](#errors-4) - [Delete node recursive](#delete-node-recursive) + - [API v2](#api-v2) + - [List node childrens](#list-node-childrens-1) - [Errors](#errors-5) + - [Get node data](#get-node-data-1) + - [Errors](#errors-6) + - [Get node data as JSON](#get-node-data-as-json-1) + - [Errors](#errors-7) + - [Create node recursive](#create-node-recursive-1) + - [Create node children](#create-node-children-1) + - [Errors](#errors-8) + - [Update node](#update-node-1) + - [Errors](#errors-9) + - [Delete node recursive](#delete-node-recursive-1) + - [Errors](#errors-10) - [Build](#build) - [Binary](#binary) - [Docker build](#docker-build) @@ -57,7 +70,7 @@ Typical usage scheme: [![tupical usage](img/usage.png)](img/usage.png) -## API +## API v1 ### List node childrens @@ -246,9 +259,186 @@ curl -XDELETE http://127.0.0.1:8889/v1/rmr/two /zoorest/two ``` +## API v2 + +### List node childrens + +Method: **LIST** + +Return JSON +```json +curl -s -XGET http://127.0.0.1:8889/v2/ | jq +{ + "childrens": [ + "two", + "three", + "one" + ], + "path": "/zoorest", + "state": "OK", + "error": "" +} +``` + +#### Errors + +```json +curl -s -XGET http://127.0.0.1:8889/v2/does/not/exist | jq +{ + "childrens": null, + "path": "", + "state": "ERROR", + "error": "zk: node does not exist" +} +``` + +### Get node data + +Method: **GET** + +Return JSON +``` +curl -s -XGET http://127.0.0.1:8889/v2/one/data | jq +{ + "path": "/zoorest/one/data", + "state": "OK", + "error": "", + "data": "eyJzb21lIjogImpzb24ifQ==" +} +``` +Node data stored in *data* field as base64 encoded string +``` +echo eyJzb21lIjogImpzb24ifQ== | base64 -d +{"some": "json"} +``` + +#### Errors + +```json + curl -s -XGET http://127.0.0.1:8889/v2/does/not/exist | jq +{ + "path": "", + "state": "ERROR", + "error": "zk: node does not exist", + "data": null +} +``` + +### Get node data as JSON + +Method: **GET** + +Location: **/v2/path/to/node+json** + +Simple add to the end of the path `+json` +Return JSON +``` +curl -s -XGET http://127.0.0.1:8889/v2/json/one/data+json | jq +{ + "path": "/json/one/data", + "state": "OK", + "error": "", + "zkstat": { + "Czxid": 45, + "Mzxid": 55, + "Ctime": 1564645641612, + "Mtime": 1564646317882, + "Version": 6, + "Cversion": 0, + "Aversion": 0, + "EphemeralOwner": 0, + "DataLength": 28, + "NumChildren": 0, + "Pzxid": 45 + }, + "data": { + "ok": true, + "some": "data" + } +} +``` + +#### Errors + +```json +curl -s -XGET http://127.0.0.1:8889/v2/invalid/json+json | jq +{ + "path": "/invalid/json", + "state": "ERROR", + "error": "JSON parsing failure: invalid character 'i' looking for beginning of value", + "zkstat": { + "Czxid": 45, + "Mzxid": 56, + "Ctime": 1564645641612, + "Mtime": 1564646350753, + "Version": 7, + "Cversion": 0, + "Aversion": 0, + "EphemeralOwner": 0, + "DataLength": 17, + "NumChildren": 0, + "Pzxid": 45 + }, + "data": null +} +``` + +### Create node recursive + +Method: **PUT** + +Return string with created path +``` +curl -XPUT http://127.0.0.1:8889/v2/two/three/four -d '{"four": "json"}' +/zoorest/two/three/four +``` + +### Create node children + +Method: **PATCH** + +Return string with created children path +``` +curl -XPATCH http://127.0.0.1:8889/v2/one/test -d 'test' +/one/test +``` + +#### Errors + +``` +curl -XPATCH http://127.0.0.1:8889/v2/six/test -d '{"six": "json"}' +zk: node does not exist +``` + +### Update node + +Method: **POST** + +Return string with updated path +``` +curl -XPOST http://127.0.0.1:8889/v2/two -d '{"two": "json"}' +/zoorest/two +``` + +#### Errors + +``` +curl -XPOST http://127.0.0.1:8889/v2/twa -d '{"two": "json"}' +zk: node does not exist +``` + +### Delete node recursive +Method: **DELETE** + +Return string with removed path +``` +curl -XDELETE http://127.0.0.1:8889/v2/two +/zoorest/two +``` + #### Errors ``` -curl -XPOST http://127.0.0.1:8889/v1/rmr/two +curl -XPOST http://127.0.0.1:8889/v2/two Method POST not alowed ``` diff --git a/rest/api.go b/rest/api.go index 1bf3f2d..b922e3c 100644 --- a/rest/api.go +++ b/rest/api.go @@ -212,12 +212,20 @@ func (zk ZooNode) GetJSON(w http.ResponseWriter, r *http.Request) { func Serve(listen string, zk ZooNode) { r := mux.NewRouter() + // API v1 r.HandleFunc("/v1/ls{path:[A-Za-z0-9-_/.:]+}", zk.LS).Methods("GET", "LIST") r.HandleFunc("/v1/get{path:[A-Za-z0-9-_/.:]+}", zk.GET).Methods("GET") r.HandleFunc("/v1/get{path:[A-Za-z0-9-_/.:]+}+json", zk.GetJSON).Methods("GET") r.HandleFunc("/v1/rmr{path:[A-Za-z0-9-_/.:]+}", zk.RM).Methods("DELETE") r.HandleFunc("/v1/up{path:[A-Za-z0-9-_/.:]+}", zk.UP).Methods("PUT", "POST", "PATCH") + // API v2 + r.HandleFunc("/v2{path:[A-Za-z0-9-_/.:]+}", zk.LS).Methods("LIST") + r.HandleFunc("/v2{path:[A-Za-z0-9-_/.:]+}", zk.GET).Methods("GET") + r.HandleFunc("/v2{path:[A-Za-z0-9-_/.:]+}+json", zk.GetJSON).Methods("GET") + r.HandleFunc("/v2{path:[A-Za-z0-9-_/.:]+}", zk.RM).Methods("DELETE") + r.HandleFunc("/v2{path:[A-Za-z0-9-_/.:]+}", zk.UP).Methods("PUT", "POST", "PATCH") + http.Handle("/", r) srv := http.Server{ diff --git a/rest/go.mod b/rest/go.mod new file mode 100644 index 0000000..553f1c0 --- /dev/null +++ b/rest/go.mod @@ -0,0 +1,9 @@ +module github.com/Difrex/zoorest/rest + +go 1.12 + +require ( + github.com/bradfitz/gomemcache v0.0.0-20190329173943-551aad21a668 + github.com/gorilla/mux v1.7.3 + github.com/samuel/go-zookeeper v0.0.0-20190801204459-3c104360edc8 +) diff --git a/rest/go.sum b/rest/go.sum new file mode 100644 index 0000000..917d49f --- /dev/null +++ b/rest/go.sum @@ -0,0 +1,6 @@ +github.com/bradfitz/gomemcache v0.0.0-20190329173943-551aad21a668 h1:U/lr3Dgy4WK+hNk4tyD+nuGjpVLPEHuJSFXMw11/HPA= +github.com/bradfitz/gomemcache v0.0.0-20190329173943-551aad21a668/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA= +github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= +github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/samuel/go-zookeeper v0.0.0-20190801204459-3c104360edc8 h1:7JRiTToRlA2H26eFPgVIVNfTG4LZzRUDnlWJ3Eco3XA= +github.com/samuel/go-zookeeper v0.0.0-20190801204459-3c104360edc8/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=