diff --git a/README.md b/README.md index 4d9f540..621add4 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/cypher.go b/cypher.go index a8e314f..47b2df7 100644 --- a/cypher.go +++ b/cypher.go @@ -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 @@ -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) } @@ -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 } diff --git a/database.go b/database.go index 85b3d8e..f00de58 100644 --- a/database.go +++ b/database.go @@ -5,7 +5,7 @@ package neoism import ( - "github.com/jmcvetta/restclient" + "github.com/jmcvetta/napping" "log" "net/url" "strconv" @@ -13,7 +13,7 @@ import ( // 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"` @@ -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 diff --git a/database_test.go b/database_test.go index e8196cb..d1f2b76 100644 --- a/database_test.go +++ b/database_test.go @@ -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 { diff --git a/entity.go b/entity.go index 515c709..04732a1 100644 --- a/entity.go +++ b/entity.go @@ -5,7 +5,6 @@ package neoism import ( - "github.com/jmcvetta/restclient" "strings" ) @@ -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! @@ -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! @@ -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 } @@ -124,19 +101,12 @@ 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 @@ -144,42 +114,33 @@ func (e *entity) Properties() (Props, error) { // 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 } diff --git a/legacy_index.go b/legacy_index.go index 099fa0f..cc7c03b 100644 --- a/legacy_index.go +++ b/legacy_index.go @@ -5,7 +5,6 @@ package neoism import ( - "github.com/jmcvetta/restclient" "net/url" ) @@ -32,21 +31,14 @@ func (db *Database) createIndex(href, name, idxType, provider string) (*index, e payload.Config = config } res := new(indexResponse) - ne := NeoError{} - rr := restclient.RequestResponse{ - Url: href, - Method: "POST", - Data: &payload, - Result: res, - Error: &ne, - ExpectedStatus: 201, - } - status, err := db.Rc.Do(&rr) + resp, err := db.Session.Post(href, &payload, &res) if err != nil { logPretty(err) return nil, err } - if status != 201 { + if resp.Status() != 201 { + ne := NeoError{} + resp.Unmarshal(&ne) return nil, ne } idx.populate(res) @@ -57,18 +49,13 @@ func (db *Database) createIndex(href, name, idxType, provider string) (*index, e func (db *Database) indexes(href string) ([]*index, error) { res := map[string]indexResponse{} nis := []*index{} - ne := NeoError{} - req := restclient.RequestResponse{ - Url: href, - Method: "GET", - Result: &res, - Error: &ne, - } - status, err := db.Rc.Do(&req) + resp, err := db.Session.Get(href, nil, &res) if err != nil { return nis, err } - if status != 200 { + if resp.Status() != 200 { + ne := NeoError{} + resp.Unmarshal(&ne) logPretty(ne) return nis, ne } @@ -93,22 +80,18 @@ func (db *Database) index(href, name string) (*index, error) { if err != nil { return idx, err } - ne := NeoError{} - req := restclient.RequestResponse{ - Url: u.String(), - Method: "GET", - Error: &ne, - } - status, err := db.Rc.Do(&req) + resp, err := db.Session.Get(u.String(), nil, nil) if err != nil { return nil, err } - switch status { + switch resp.Status() { // Success! case 200: case 404: return nil, NotFound default: + ne := NeoError{} + resp.Unmarshal(&ne) logPretty(ne) return idx, ne } @@ -154,17 +137,13 @@ func (idx *index) Delete() error { if err != nil { return err } - ne := NeoError{} - req := restclient.RequestResponse{ - Url: uri, - Method: "DELETE", - Error: &ne, - } - status, err := idx.db.Rc.Do(&req) + resp, err := idx.db.Session.Delete(uri) if err != nil { return err } - if status != 204 { + if resp.Status() != 204 { + ne := NeoError{} + resp.Unmarshal(&ne) logPretty(ne) return ne } @@ -177,28 +156,23 @@ func (idx *index) add(e entity, key string, value interface{}) error { if err != nil { return err } - ne := NeoError{} type s struct { Uri string `json:"uri"` Key string `json:"key"` Value interface{} `json:"value"` } - data := s{ + payload := s{ Uri: e.HrefSelf, Key: key, Value: value, } - req := restclient.RequestResponse{ - Url: uri, - Method: "POST", - Data: data, - Error: &ne, - } - status, err := idx.db.Rc.Do(&req) + resp, err := idx.db.Session.Post(uri, &payload, nil) if err != nil { return err } - if status != 201 { + if resp.Status() != 201 { + ne := NeoError{} + resp.Unmarshal(&ne) logPretty(ne) return ne } @@ -218,18 +192,14 @@ func (idx *index) remove(e entity, id, key, value string) error { uri = join(uri, key, value) } uri = join(uri, id) - ne := NeoError{} - req := restclient.RequestResponse{ - Url: uri, - Method: "DELETE", - Error: &ne, - } - status, err := idx.db.Rc.Do(&req) + resp, err := idx.db.Session.Delete(uri) if err != nil { return err } - if status != 204 { - logPretty(req) + if resp.Status() != 204 { + ne := NeoError{} + resp.Unmarshal(&ne) + logPretty(ne) return ne } return nil // Success! diff --git a/node.go b/node.go index 615cb02..fd06dfa 100644 --- a/node.go +++ b/node.go @@ -5,7 +5,6 @@ package neoism import ( - "github.com/jmcvetta/restclient" "strconv" "strings" ) @@ -14,18 +13,11 @@ import ( func (db *Database) CreateNode(p Props) (*Node, error) { n := Node{} n.Db = db - ne := new(NeoError) - rr := restclient.RequestResponse{ - Url: db.HrefNode, - Method: "POST", - Data: &p, - Result: &n, - Error: ne, - ExpectedStatus: 201, - } - status, err := db.Rc.Do(&rr) - if err != nil { - logPretty(status) + resp, err := db.Session.Post(db.HrefNode, &p, &n) + if err != nil || resp.Status() != 201 { + logPretty(resp.Status()) + ne := NeoError{} + resp.Unmarshal(&ne) logPretty(ne) return &n, err } @@ -40,23 +32,19 @@ func (db *Database) Node(id int) (*Node, error) { // getNodeByUri fetches a Node from the database based on its URI. func (db *Database) getNodeByUri(uri string) (*Node, error) { - ne := NeoError{} n := Node{} n.Db = db - rr := restclient.RequestResponse{ - Url: uri, - Method: "GET", - Result: &n, - Error: &ne, - } - status, err := db.Rc.Do(&rr) + resp, err := db.Session.Get(uri, nil, &n) if err != nil { return nil, err } + status := resp.Status() switch { case status == 404: return &n, NotFound case status != 200 || n.HrefSelf == "": + ne := NeoError{} + resp.Unmarshal(&ne) logPretty(ne) return nil, ne } @@ -101,18 +89,13 @@ func (n *Node) getRels(uri string, types ...string) (Rels, error) { uri = strings.Join(parts, "/") } rels := Rels{} - ne := NeoError{} - rr := restclient.RequestResponse{ - Url: uri, - Method: "GET", - Result: &rels, - Error: &ne, - } - status, err := n.Db.Rc.Do(&rr) + resp, err := n.Db.Session.Get(uri, nil, &rels) if err != nil { return rels, err } - if status != 200 { + if resp.Status() != 200 { + ne := NeoError{} + resp.Unmarshal(&ne) logPretty(ne) return rels, ne } @@ -140,7 +123,6 @@ func (n *Node) Outgoing(types ...string) (Rels, error) { func (n *Node) Relate(relType string, destId int, p Props) (*Relationship, error) { rel := Relationship{} rel.Db = n.Db - ne := NeoError{} srcUri := join(n.HrefSelf, "relationships") destUri := join(n.Db.HrefNode, strconv.Itoa(destId)) content := map[string]interface{}{ @@ -150,18 +132,13 @@ func (n *Node) Relate(relType string, destId int, p Props) (*Relationship, error if p != nil { content["data"] = &p } - c := restclient.RequestResponse{ - Url: srcUri, - Method: "POST", - Data: content, - Result: &rel, - Error: &ne, - } - status, err := n.Db.Rc.Do(&c) + resp, err := n.Db.Session.Post(srcUri, content, &rel) if err != nil { return &rel, err } - if status != 201 { + if resp.Status() != 201 { + ne := NeoError{} + resp.Unmarshal(&ne) logPretty(ne) return &rel, ne } @@ -170,21 +147,16 @@ func (n *Node) Relate(relType string, destId int, p Props) (*Relationship, error // AddLabels adds one or more labels to a node. func (n *Node) AddLabel(labels ...string) error { - ne := NeoError{} - rr := restclient.RequestResponse{ - Url: n.HrefLabels, - Method: "POST", - Data: labels, - Error: &ne, - } - status, err := n.Db.Rc.Do(&rr) + resp, err := n.Db.Session.Post(n.HrefLabels, labels, nil) if err != nil { return err } - if status == 404 { + if resp.Status() == 404 { return NotFound } - if status != 204 { + if resp.Status() != 204 { + ne := NeoError{} + resp.Unmarshal(&ne) return ne } return nil // Success @@ -192,22 +164,17 @@ func (n *Node) AddLabel(labels ...string) error { // Labels lists labels for a node. func (n *Node) Labels() ([]string, error) { - ne := NeoError{} res := []string{} - rr := restclient.RequestResponse{ - Url: n.HrefLabels, - Method: "GET", - Error: &ne, - Result: &res, - } - status, err := n.Db.Rc.Do(&rr) + resp, err := n.Db.Session.Get(n.HrefLabels, nil, &res) if err != nil { return res, err } - if status == 404 { + if resp.Status() == 404 { return res, NotFound } - if status != 200 { + if resp.Status() != 200 { + ne := NeoError{} + resp.Unmarshal(&ne) return res, ne } return res, nil // Success @@ -215,21 +182,17 @@ func (n *Node) Labels() ([]string, error) { // RemoveLabel removes a label from a node. func (n *Node) RemoveLabel(label string) error { - ne := NeoError{} - url := join(n.HrefLabels, label) - rr := restclient.RequestResponse{ - Url: url, - Method: "DELETE", - Error: &ne, - } - status, err := n.Db.Rc.Do(&rr) + uri := join(n.HrefLabels, label) + resp, err := n.Db.Session.Delete(uri) if err != nil { return err } - if status == 404 { + if resp.Status() == 404 { return NotFound } - if status != 204 { + if resp.Status() != 204 { + ne := NeoError{} + resp.Unmarshal(&ne) return ne } return nil // Success @@ -238,21 +201,16 @@ func (n *Node) RemoveLabel(label string) error { // SetLabels removes any labels currently on a node, and replaces them with the // labels provided as argument. func (n *Node) SetLabels(labels []string) error { - ne := NeoError{} - rr := restclient.RequestResponse{ - Url: n.HrefLabels, - Method: "PUT", - Data: labels, - Error: &ne, - } - status, err := n.Db.Rc.Do(&rr) + resp, err := n.Db.Session.Put(n.HrefLabels, labels, nil) if err != nil { return err } - if status == 404 { + if resp.Status() == 404 { return NotFound } - if status != 204 { + if resp.Status() != 204 { + ne := NeoError{} + resp.Unmarshal(&ne) return ne } return nil // Success @@ -260,23 +218,18 @@ func (n *Node) SetLabels(labels []string) error { // NodesByLabel gets all nodes with a given label. func (db *Database) NodesByLabel(label string) ([]*Node, error) { - url := join(db.Url, "label", label, "nodes") - ne := NeoError{} + uri := join(db.Url, "label", label, "nodes") res := []*Node{} - rr := restclient.RequestResponse{ - Url: url, - Method: "GET", - Result: &res, - Error: &ne, - } - status, err := db.Rc.Do(&rr) + resp, err := db.Session.Get(uri, nil, &res) if err != nil { return res, err } - if status == 404 { + if resp.Status() == 404 { return res, NotFound } - if status != 200 { + if resp.Status() != 200 { + ne := NeoError{} + resp.Unmarshal(&ne) return res, ne } for _, n := range res { @@ -287,20 +240,15 @@ func (db *Database) NodesByLabel(label string) ([]*Node, error) { // Labels lists all labels. func (db *Database) Labels() ([]string, error) { - url := join(db.Url, "labels") - ne := NeoError{} + uri := join(db.Url, "labels") labels := []string{} - rr := restclient.RequestResponse{ - Url: url, - Method: "GET", - Result: &labels, - Error: &ne, - } - status, err := db.Rc.Do(&rr) + resp, err := db.Session.Get(uri, nil, &labels) if err != nil { return labels, err } - if status != 200 { + if resp.Status() != 200 { + ne := NeoError{} + resp.Unmarshal(&ne) return labels, ne } return labels, nil diff --git a/node_index.go b/node_index.go index 97f57db..8f5be8b 100644 --- a/node_index.go +++ b/node_index.go @@ -5,7 +5,6 @@ package neoism import ( - "github.com/jmcvetta/restclient" "net/url" "strconv" ) @@ -72,23 +71,18 @@ func (idx *LegacyNodeIndex) Find(key, value string) (map[int]*Node, error) { if err != nil { return nm, err } - ne := NeoError{} - resp := []Node{} - req := restclient.RequestResponse{ - Url: u.String(), - Method: "GET", - Result: &resp, - Error: &ne, - } - status, err := idx.db.Rc.Do(&req) + result := []Node{} + resp, err := idx.db.Session.Get(u.String(), nil, &result) if err != nil { return nm, err } - if status != 200 { + if resp.Status() != 200 { + ne := NeoError{} + resp.Unmarshal(&ne) logPretty(ne) return nm, ne } - for _, n := range resp { + for _, n := range result { n.Db = idx.db nm[n.Id()] = &n } @@ -110,19 +104,14 @@ func (idx *index) Query(query string) (map[int]*Node, error) { return nm, err } result := []Node{} - ne := NeoError{} - req := restclient.RequestResponse{ - Url: u.String(), - Method: "GET", - Result: &result, - Error: &ne, - } - status, err := idx.db.Rc.Do(&req) + resp, err := idx.db.Session.Get(u.String(), nil, &result) if err != nil { return nm, err } - if status != 200 { - logPretty(req) + if resp.Status() != 200 { + ne := NeoError{} + resp.Unmarshal(&ne) + logPretty(ne) return nm, ne } for _, n := range result { diff --git a/relationship.go b/relationship.go index de02fbe..dd7cf91 100644 --- a/relationship.go +++ b/relationship.go @@ -5,7 +5,6 @@ package neoism import ( - "github.com/jmcvetta/restclient" "sort" "strconv" "strings" @@ -16,19 +15,14 @@ func (db *Database) Relationship(id int) (*Relationship, error) { rel := Relationship{} rel.Db = db uri := join(db.Url, "relationship", strconv.Itoa(id)) - ne := NeoError{} - rr := restclient.RequestResponse{ - Url: uri, - Method: "GET", - Result: &rel, - Error: &ne, - } - status, err := db.Rc.Do(&rr) + resp, err := db.Session.Get(uri, nil, &rel) if err != nil { return &rel, err } - switch status { + switch resp.Status() { default: + ne := NeoError{} + resp.Unmarshal(&ne) logPretty(ne) err = ne case 200: @@ -42,21 +36,16 @@ func (db *Database) Relationship(id int) (*Relationship, error) { // Types lists all existing relationship types func (db *Database) RelTypes() ([]string, error) { reltypes := []string{} - ne := NeoError{} - c := restclient.RequestResponse{ - Url: db.HrefRelTypes, - Method: "GET", - Result: &reltypes, - Error: &ne, - } - status, err := db.Rc.Do(&c) + resp, err := db.Session.Get(db.HrefRelTypes, nil, &reltypes) if err != nil { return reltypes, err } - if status == 200 { + if resp.Status() == 200 { sort.Sort(sort.StringSlice(reltypes)) return reltypes, nil // Success! } + ne := NeoError{} + resp.Unmarshal(&ne) logPretty(ne) return reltypes, ne } diff --git a/schema.go b/schema.go index f16fb49..c550674 100644 --- a/schema.go +++ b/schema.go @@ -4,10 +4,6 @@ package neoism -import ( - "github.com/jmcvetta/restclient" -) - type indexRequest struct { PropertyKeys []string `json:"property_keys"` } @@ -21,21 +17,17 @@ type Index struct { // Drop removes the index. func (idx *Index) Drop() error { - url := join(idx.db.Url, "schema/index", idx.Label, idx.PropertyKeys[0]) - ne := NeoError{} - rr := restclient.RequestResponse{ - Url: url, - Method: "DELETE", - Error: &ne, - } - status, err := idx.db.Rc.Do(&rr) + uri := join(idx.db.Url, "schema/index", idx.Label, idx.PropertyKeys[0]) + resp, err := idx.db.Session.Delete(uri) if err != nil { return err } - if status == 404 { + if resp.Status() == 404 { return NotFound } - if status != 204 { + if resp.Status() != 204 { + ne := NeoError{} + resp.Unmarshal(&ne) return ne } return nil @@ -44,53 +36,42 @@ func (idx *Index) Drop() error { // CreateIndex starts a background job in the database that will create and // populate the new index of a specified property on nodes of a given label. func (db *Database) CreateIndex(label, property string) (*Index, error) { - url := join(db.Url, "schema/index", label) + uri := join(db.Url, "schema/index", label) payload := indexRequest{[]string{property}} - ne := NeoError{} - res := Index{db: db} - rr := restclient.RequestResponse{ - Url: url, - Method: "POST", - Data: payload, - Result: &res, - Error: &ne, - } - status, err := db.Rc.Do(&rr) + result := Index{db: db} + resp, err := db.Session.Post(uri, payload, &result) if err != nil { return nil, err } - if status == 404 { + switch resp.Status() { + case 200: + return &result, nil // Success + case 404: return nil, NotFound } - if status != 200 { - return nil, ne - } - return &res, nil + ne := NeoError{} + resp.Unmarshal(&ne) + return nil, ne } // Indexes lists indexes for a label. func (db *Database) Indexes(label string) ([]*Index, error) { - url := join(db.Url, "schema/index", label) - ne := NeoError{} - res := []*Index{} - rr := restclient.RequestResponse{ - Url: url, - Method: "GET", - Result: &res, - Error: &ne, - } - status, err := db.Rc.Do(&rr) + uri := join(db.Url, "schema/index", label) + result := []*Index{} + resp, err := db.Session.Get(uri, nil, &result) if err != nil { - return res, err + return result, err } - if status == 404 { - return res, NotFound + if resp.Status() == 404 { + return result, NotFound } - if status != 200 { - return res, ne + if resp.Status() != 200 { + ne := NeoError{} + resp.Unmarshal(&ne) + return result, ne } - for _, idx := range res { + for _, idx := range result { idx.db = db } - return res, nil + return result, nil } diff --git a/transaction.go b/transaction.go index e5074a9..720605f 100644 --- a/transaction.go +++ b/transaction.go @@ -6,7 +6,6 @@ package neoism import ( "errors" - "github.com/jmcvetta/restclient" ) // A Tx is an in-progress database transaction. @@ -62,31 +61,25 @@ func (tr *txResponse) unmarshal(qs []*CypherQuery) error { // Begin opens a new transaction, executing zero or more cypher queries // inside the transaction. func (db *Database) Begin(qs []*CypherQuery) (*Tx, error) { - ne := NeoError{} payload := txRequest{Statements: qs} - res := txResponse{} - rr := restclient.RequestResponse{ - Url: db.HrefTransaction, - Method: "POST", - Data: payload, - Result: &res, - Error: &ne, - } - status, err := db.Rc.Do(&rr) + result := txResponse{} + resp, err := db.Session.Post(db.HrefTransaction, payload, &result) if err != nil { return nil, err } - if status != 201 { + if resp.Status() != 201 { + ne := NeoError{} + resp.Unmarshal(&ne) return nil, ne } t := Tx{ db: db, - hrefCommit: res.Commit, - Location: rr.HttpResponse.Header.Get("location"), - Errors: res.Errors, - Expires: res.Transaction.Expires, + hrefCommit: result.Commit, + Location: resp.HttpResponse().Header.Get("location"), + Errors: result.Errors, + Expires: result.Transaction.Expires, } - err = res.unmarshal(qs) + err = result.unmarshal(qs) if err != nil { return &t, err } @@ -101,17 +94,13 @@ func (t *Tx) Commit() error { if len(t.Errors) > 0 { return TxQueryError } - ne := NeoError{} - rr := restclient.RequestResponse{ - Url: t.hrefCommit, - Method: "POST", - Error: &ne, - } - status, err := t.db.Rc.Do(&rr) + resp, err := t.db.Session.Post(t.hrefCommit, nil, nil) if err != nil { return err } - if status != 200 { + if resp.Status() != 200 { + ne := NeoError{} + resp.Unmarshal(&ne) return ne } return nil // Success @@ -119,29 +108,23 @@ func (t *Tx) Commit() error { // Query executes statements in an open transaction. func (t *Tx) Query(qs []*CypherQuery) error { - ne := NeoError{} payload := txRequest{Statements: qs} - res := txResponse{} - rr := restclient.RequestResponse{ - Url: t.Location, - Method: "POST", - Data: payload, - Result: &res, - Error: &ne, - } - status, err := t.db.Rc.Do(&rr) + result := txResponse{} + resp, err := t.db.Session.Post(t.Location, payload, &result) if err != nil { return err } - if status == 404 { + if resp.Status() == 404 { return NotFound } - if status != 200 { + if resp.Status() != 200 { + ne := NeoError{} + resp.Unmarshal(&ne) return &ne } - t.Expires = res.Transaction.Expires - t.Errors = append(t.Errors, res.Errors...) - err = res.unmarshal(qs) + t.Expires = result.Transaction.Expires + t.Errors = append(t.Errors, result.Errors...) + err = result.unmarshal(qs) if err != nil { return err } @@ -153,17 +136,13 @@ func (t *Tx) Query(qs []*CypherQuery) error { // Rollback rolls back an open transaction. func (t *Tx) Rollback() error { - ne := NeoError{} - rr := restclient.RequestResponse{ - Url: t.Location, - Method: "DELETE", - Error: &ne, - } - status, err := t.db.Rc.Do(&rr) + resp, err := t.db.Session.Delete(t.Location) if err != nil { return err } - if status != 200 { + if resp.Status() != 200 { + ne := NeoError{} + resp.Unmarshal(&ne) return ne } return nil // Success