Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move from restclient to napping #25

Merged
merged 7 commits into from
Sep 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
neoism - Neo4j client for Go
===========================

![Neo4j + Gopher Logo](https://raw.github.com/jmcvetta/neoism/master/neoism.png)
![Neoism Logo](https://raw.github.com/jmcvetta/neoism/master/neoism.png)

Package `neoism` is a [Go](http://golang.org) client library providing access to
the [Neo4j](http://www.neo4j.org) graph database via its REST API.
Expand Down
37 changes: 15 additions & 22 deletions cypher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package neoism
import (
"encoding/json"
"errors"
"github.com/jmcvetta/restclient"
)

// A CypherQuery is a statement in the Cypher query language, with optional
Expand Down Expand Up @@ -64,28 +63,28 @@ type cypherResult struct {
// from the db is used to populate `result`, which should be a pointer to a
// slice of structs. TODO: Or a pointer to a two-dimensional array of structs?
func (db *Database) Cypher(q *CypherQuery) error {
cRes := cypherResult{}
cReq := cypherRequest{
result := cypherResult{}
payload := cypherRequest{
Query: q.Statement,
Parameters: q.Parameters,
}
ne := new(NeoError)
rr := restclient.RequestResponse{
Url: db.HrefCypher,
Method: "POST",
Data: &cReq,
Result: &cRes,
Error: ne,
}
status, err := db.Rc.Do(&rr)
url := db.HrefCypher
// Method: "POST"
// Data: &cReq
// Result: &cRes
// Error: ne
// }
resp, err := db.Session.Post(url, &payload, &result)
if err != nil {
return err
}
if status != 200 {
if resp.Status() != 200 {
resp.Unmarshal(ne)
logPretty(ne)
return *ne
}
q.cr = cRes
q.cr = result
if q.Result != nil {
q.Unmarshal(q.Result)
}
Expand Down Expand Up @@ -124,18 +123,12 @@ func (db *Database) CypherBatch(qs []*CypherQuery) error {
}
res := []batchCypherResponse{}
ne := NeoError{}
rr := restclient.RequestResponse{
Url: db.HrefBatch,
Method: "POST",
Data: payload,
Result: &res,
Error: &ne,
}
status, err := db.Rc.Do(&rr)
url := db.HrefBatch
resp, err := db.Session.Post(url, payload, &res)
if err != nil {
return err
}
if status != 200 {
if resp.Status() != 200 {
logPretty(ne)
return ne
}
Expand Down
25 changes: 11 additions & 14 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
package neoism

import (
"github.com/jmcvetta/restclient"
"github.com/jmcvetta/napping"
"log"
"net/url"
"strconv"
)

// A Database is a REST client connected to a Neo4j database.
type Database struct {
Rc *restclient.Client
Session *napping.Session
Url string `json:"-"` // Root URL for REST API
HrefNode string `json:"node"`
HrefRefNode string `json:"reference_node"`
Expand All @@ -30,28 +30,25 @@ type Database struct {

// Connect establishes a connection to the Neo4j server.
func Connect(uri string) (*Database, error) {
var e NeoError
db := &Database{
Rc: restclient.New(),
Session: &napping.Session{},
}
_, err := url.Parse(uri) // Sanity check
if err != nil {
return nil, err
}
db.Url = uri
req := restclient.RequestResponse{
Url: db.Url,
Method: "GET",
Result: &db,
Error: &e,
}
status, err := db.Rc.Do(&req)
// Url: db.Url,
// Method: "GET",
// Result: &db,
// Error: &e,
resp, err := db.Session.Get(db.Url, nil, &db)
if err != nil {
return nil, err
}
if status != 200 || db.Version == "" {
logPretty(req.RawText)
log.Println("Status " + strconv.Itoa(status) + " trying to connect to " + uri)
if resp.Status() != 200 || db.Version == "" {
logPretty(resp.RawText())
log.Println("Status " + strconv.Itoa(resp.Status()) + " trying to connect to " + uri)
return nil, InvalidDatabase
}
return db, nil
Expand Down
2 changes: 1 addition & 1 deletion database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestConnectInvalidUrl(t *testing.T) {
t.Fatal("Expected error due to missing protocol scheme")
}
//
// Unsupported protocol scheme - restclient.Do should fail
// Unsupported protocol scheme - Session.Get should fail
//
_, err = Connect("foo://bar.com")
if err == nil {
Expand Down
101 changes: 31 additions & 70 deletions entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package neoism

import (
"github.com/jmcvetta/restclient"
"strings"
)

Expand All @@ -19,27 +18,17 @@ type entity struct {
HrefProperties string `json:"properties"`
}

// do is a convenience wrapper around the embedded restclient's Do() method.
func (e *entity) do(rr *restclient.RequestResponse) (status int, err error) {
return e.Db.Rc.Do(rr)
}

// SetProperty sets the single property key to value.
func (e *entity) SetProperty(key string, value string) error {
parts := []string{e.HrefProperties, key}
uri := strings.Join(parts, "/")
ne := NeoError{}
rr := restclient.RequestResponse{
Url: uri,
Method: "PUT",
Data: &value,
Error: &ne,
}
status, err := e.do(&rr)
url := strings.Join(parts, "/")
resp, err := e.Db.Session.Put(url, &value, nil)
if err != nil {
return err
}
if status != 204 {
if resp.Status() != 204 {
ne := NeoError{}
resp.Unmarshal(&ne)
return ne
}
return nil // Success!
Expand All @@ -49,23 +38,19 @@ func (e *entity) SetProperty(key string, value string) error {
func (e *entity) Property(key string) (string, error) {
var val string
parts := []string{e.HrefProperties, key}
uri := strings.Join(parts, "/")
ne := NeoError{}
rr := restclient.RequestResponse{
Url: uri,
Method: "GET",
Result: &val,
Error: &ne,
}
status, err := e.do(&rr)
url := strings.Join(parts, "/")
resp, err := e.Db.Session.Get(url, nil, &val)
if err != nil {
logPretty(err)
return val, err
}
switch status {
switch resp.Status() {
case 200:
case 404:
return val, NotFound
default:
ne := NeoError{}
resp.Unmarshal(&ne)
return val, ne
}
return val, nil // Success!
Expand All @@ -74,47 +59,39 @@ func (e *entity) Property(key string) (string, error) {
// DeleteProperty deletes property key
func (e *entity) DeleteProperty(key string) error {
parts := []string{e.HrefProperties, key}
uri := strings.Join(parts, "/")
ne := NeoError{}
rr := restclient.RequestResponse{
Url: uri,
Method: "DELETE",
Error: &ne,
}
status, err := e.do(&rr)
url := strings.Join(parts, "/")
resp, err := e.Db.Session.Delete(url)
if err != nil {
return err
}
switch status {
switch resp.Status() {
case 204:
return nil // Success!
case 404:
return NotFound
}
ne := NeoError{}
resp.Unmarshal(&ne)
logPretty(ne)
return ne
}

// Delete removes the object from the DB.
func (e *entity) Delete() error {
ne := NeoError{}
rr := restclient.RequestResponse{
Url: e.HrefSelf,
Method: "DELETE",
Error: &ne,
}
status, err := e.do(&rr)
resp, err := e.Db.Session.Delete(e.HrefSelf)
if err != nil {
return err
}
switch status {
switch resp.Status() {
case 204:
case 404:
return NotFound
case 409:
return CannotDelete
default:
logPretty(status)
ne := NeoError{}
resp.Unmarshal(&ne)
logPretty(resp.Status())
logPretty(ne)
return ne
}
Expand All @@ -124,62 +101,46 @@ func (e *entity) Delete() error {
// Properties fetches all properties
func (e *entity) Properties() (Props, error) {
props := Props{}
ne := NeoError{}
rr := restclient.RequestResponse{
Url: e.HrefProperties,
Method: "GET",
Result: &props,
Error: &ne,
}
status, err := e.do(&rr)
resp, err := e.Db.Session.Get(e.HrefProperties, nil, &props)
if err != nil {
return props, err
}
// Status code 204 indicates no properties on this node
if status == 204 {
if resp.Status() == 204 {
props = Props{}
}
return props, nil
}

// SetProperties updates all properties, overwriting any existing properties.
func (e *entity) SetProperties(p Props) error {
ne := NeoError{}
rr := restclient.RequestResponse{
Url: e.HrefProperties,
Method: "PUT",
Data: &p,
Error: &ne,
}
status, err := e.do(&rr)
resp, err := e.Db.Session.Put(e.HrefProperties, &p, nil)
if err != nil {
return err
}
if status == 204 {
if resp.Status() == 204 {
return nil // Success!
}
ne := NeoError{}
resp.Unmarshal(&ne)
logPretty(ne)
return ne
}

// DeleteProperties deletes all properties.
func (e *entity) DeleteProperties() error {
ne := NeoError{}
rr := restclient.RequestResponse{
Url: e.HrefProperties,
Method: "DELETE",
Error: &ne,
}
status, err := e.do(&rr)
resp, err := e.Db.Session.Delete(e.HrefProperties)
if err != nil {
return err
}
switch status {
switch resp.Status() {
case 204:
return nil // Success!
case 404:
return NotFound
}
ne := NeoError{}
resp.Unmarshal(&ne)
logPretty(ne)
return ne
}
Loading