Skip to content

Commit

Permalink
* New method: PATCH
Browse files Browse the repository at this point in the history
* Fix return path string in rmr

* Version bump
  • Loading branch information
Denis Zheleztsov committed May 4, 2017
1 parent 8a575ea commit d14ad45
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Zookeeper HTTP rest API
- [Get node data](#get-node-data)
- [Errors](#errors)
- [Create node recursive](#create-node-recursive)
- [Create node children](#create-node-children)
- [Errors](#errors)
- [Update node](#update-node)
- [Errors](#errors)
- [Delete node recursive](#delete-node-recursive)
Expand All @@ -22,7 +24,6 @@ Zookeeper HTTP rest API
- [Binary](#binary)
- [Docker build](#docker-build)
- [Binary file](#binary-file)
- [Docker image](#docker-image)
- [AUTHORS](#authors)
- [LICENSE](#license)

Expand Down Expand Up @@ -122,6 +123,25 @@ curl -XPUT http://127.0.0.1:8889/v1/up/two/three/four -d '{"four": "json"}'
/zoorest/two/three/four
```

### Create node children

Method: **PATCH**

Location: **/v1/up**

Return string with created children path
```
curl -XPATCH http://127.0.0.1:8889/v1/up/one/test -d 'test'
/one/test
```

#### Errors

```
curl -XPATCH http://127.0.0.1:8889/v1/up/six/test -d '{"six": "json"}'
zk: node does not exist
```

### Update node

Method: **POST**
Expand Down
15 changes: 9 additions & 6 deletions rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func (zk ZooNode) UP(w http.ResponseWriter, r *http.Request) {
go func() { ch <- zk.CreateNode(path, content) }()
} else if r.Method == "POST" {
go func() { ch <- zk.UpdateNode(path, content) }()
} else if r.Method == "PATCH" {
go func() { ch <- zk.CreateChild(path, content) }()
} else {
e := strings.Join([]string{"Method", r.Method, "not alowed"}, " ")
w.WriteHeader(500)
Expand All @@ -115,15 +117,16 @@ func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
path := vars["path"]

var rmPath string
rmPath = strings.Join([]string{zk.Path, path}, "")
if path == "/" {
e := "Skiping root path"
w.WriteHeader(500)
w.Write([]byte(e))
return
}

var rmPath string
rmPath = strings.Join([]string{zk.Path, path}, "")

if strings.Contains(rmPath, "//") {
rmPath = strings.Replace(rmPath, "//", "/", 1)
}
Expand Down Expand Up @@ -171,10 +174,10 @@ func (zk ZooNode) GET(w http.ResponseWriter, r *http.Request) {
func Serve(listen string, zk ZooNode) {
r := mux.NewRouter()

r.HandleFunc("/v1/ls{path:[a-z0-9-_/.:]+}", zk.LS)
r.HandleFunc("/v1/get{path:[a-z0-9-_/.:]+}", zk.GET)
r.HandleFunc("/v1/rmr{path:[a-z0-9-_/.:]+}", zk.RM)
r.HandleFunc("/v1/up{path:[a-z0-9-_/.:]+}", zk.UP)
r.HandleFunc("/v1/ls{path:[A-Za-z0-9-_/.:]+}", zk.LS)
r.HandleFunc("/v1/get{path:[A-Za-z0-9-_/.:]+}", zk.GET)
r.HandleFunc("/v1/rmr{path:[A-Za-z0-9-_/.:]+}", zk.RM)
r.HandleFunc("/v1/up{path:[A-Za-z0-9-_/.:]+}", zk.UP)

http.Handle("/", r)

Expand Down
20 changes: 19 additions & 1 deletion rest/zoo.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (z ZooNode) CreateNode(path string, content []byte) string {
return z.UpdateNode(path, content)
}

// UpdateNode ...
// UpdateNode update existing node
func (z ZooNode) UpdateNode(path string, content []byte) string {
upPath := strings.Join([]string{z.Path, path}, "")
if strings.Contains(upPath, "//") {
Expand All @@ -152,6 +152,24 @@ func (z ZooNode) UpdateNode(path string, content []byte) string {
return path
}

// CreateChild create child in /node/path
func (z ZooNode) CreateChild(path string, content []byte) string {
crPath := strings.Join([]string{z.Path, path}, "")
if strings.Contains(crPath, "//") {
crPath = strings.Replace(crPath, "//", "/", 1)
}
if crPath == "/" {
return "Not updating root path"
}

_, err := z.Conn.Create(crPath, content, 0, zk.WorldACL(zk.PermAll))
if err != nil {
return err.Error()
}

return path
}

//EnsureZooPath create zookeeper path
func (z ZooNode) EnsureZooPath(path string) (string, error) {
flag := int32(0)
Expand Down

0 comments on commit d14ad45

Please sign in to comment.