Skip to content
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
26 changes: 18 additions & 8 deletions v2/arangodb/client_database_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,20 @@ func (c clientDatabase) CreateDatabase(ctx context.Context, name string, options
Name: name,
}

resp, err := connection.CallPost(ctx, c.client.connection, url, nil, &createRequest)
response := struct {
shared.ResponseStruct `json:",inline"`
}{}

resp, err := connection.CallPost(ctx, c.client.connection, url, &response, &createRequest)
if err != nil {
return nil, errors.WithStack(err)
}

switch resp.Code() {
switch code := resp.Code(); code {
case http.StatusCreated:
return newDatabase(c.client, name), nil
default:
return nil, connection.NewError(resp.Code(), "unexpected code")
return nil, response.AsArangoError()
}
}

Expand Down Expand Up @@ -93,16 +97,22 @@ func (c clientDatabase) Databases(ctx context.Context) ([]Database, error) {

func (c clientDatabase) Database(ctx context.Context, name string) (Database, error) {
url := connection.NewUrl("_db", name, "_api", "database", "current")
resp, err := connection.CallGet(ctx, c.client.connection, url, nil)

var response struct {
shared.ResponseStruct `json:",inline"`
VersionInfo `json:",inline"`
}

resp, err := connection.CallGet(ctx, c.client.connection, url, &response)
if err != nil {
return nil, errors.WithStack(err)
}

switch resp.Code() {
switch code := resp.Code(); code {
case http.StatusOK:
return newDatabase(c.client, name), nil
default:
return nil, connection.NewError(resp.Code(), "unexpected code")
return nil, response.AsArangoErrorWithCode(code)
}
}

Expand All @@ -117,7 +127,7 @@ func (c clientDatabase) databases(ctx context.Context, url string) ([]Database,
return nil, errors.WithStack(err)
}

switch resp.Code() {
switch code := resp.Code(); code {
case http.StatusOK:
dbs := make([]Database, len(databases.Result))

Expand All @@ -127,6 +137,6 @@ func (c clientDatabase) databases(ctx context.Context, url string) ([]Database,

return dbs, nil
default:
return nil, connection.NewError(resp.Code(), "unexpected code")
return nil, databases.AsArangoErrorWithCode(code)
}
}
15 changes: 10 additions & 5 deletions v2/arangodb/client_server_info_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"context"
"net/http"

"github.com/arangodb/go-driver/v2/arangodb/shared"

"github.com/arangodb/go-driver/v2/connection"
"github.com/pkg/errors"
)
Expand All @@ -45,17 +47,20 @@ type clientServerInfo struct {
func (c clientServerInfo) Version(ctx context.Context) (VersionInfo, error) {
url := connection.NewUrl("_api", "version")

var version VersionInfo
var response struct {
shared.ResponseStruct `json:",inline"`
VersionInfo
}

resp, err := connection.CallGet(ctx, c.client.connection, url, &version)
resp, err := connection.CallGet(ctx, c.client.connection, url, &response)
if err != nil {
return VersionInfo{}, errors.WithStack(err)
}

switch resp.Code() {
switch code := resp.Code(); code {
case http.StatusOK:
return version, nil
return response.VersionInfo, nil
default:
return VersionInfo{}, connection.NewError(resp.Code(), "unexpected code")
return VersionInfo{}, response.AsArangoErrorWithCode(code)
}
}
1 change: 1 addition & 0 deletions v2/arangodb/collection_documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ type CollectionDocuments interface {

CollectionDocumentCreate
CollectionDocumentRead
CollectionDocumentUpdate
}
7 changes: 3 additions & 4 deletions v2/arangodb/collection_documents_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,13 @@ type CollectionDocumentCreate interface {
}

type CollectionDocumentCreateResponseReader interface {
Read() (CollectionDocumentCreateResponse, bool, error)
Read() (CollectionDocumentCreateResponse, error)
}

type CollectionDocumentCreateResponse struct {
DocumentMeta
shared.ResponseStruct

Old, New interface{}
shared.ResponseStruct `json:",inline"`
Old, New interface{}
}

type CollectionDocumentCreateOverwriteMode string
Expand Down
176 changes: 176 additions & 0 deletions v2/arangodb/collection_documents_create_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//
// DISCLAIMER
//
// Copyright 2020 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Adam Janikowski
//

package arangodb

import (
"context"
"io"
"net/http"

"github.com/arangodb/go-driver/v2/arangodb/shared"
"github.com/arangodb/go-driver/v2/connection"
"github.com/arangodb/go-driver/v2/utils"
"github.com/pkg/errors"
)

func newCollectionDocumentCreate(collection *collection) *collectionDocumentCreate {
return &collectionDocumentCreate{
collection: collection,
}
}

var _ CollectionDocumentCreate = &collectionDocumentCreate{}

type collectionDocumentCreate struct {
collection *collection
}

func (c collectionDocumentCreate) CreateDocumentsWithOptions(ctx context.Context, documents interface{}, opts *CollectionDocumentCreateOptions) (CollectionDocumentCreateResponseReader, error) {
if !utils.IsListPtr(documents) && !utils.IsList(documents) {
return nil, errors.Errorf("Input documents should be list")
}

url := c.collection.url("document")

req, err := c.collection.connection().NewRequest(http.MethodPost, url)
if err != nil {
return nil, err
}

for _, modifier := range c.collection.withModifiers(opts.modifyRequest, connection.WithBody(documents), connection.WithFragment("multiple")) {
if err = modifier(req); err != nil {
return nil, err
}
}

var arr connection.Array

resp, err := c.collection.connection().Do(ctx, req, &arr)
if err != nil {
return nil, err
}

switch code := resp.Code(); code {
case http.StatusCreated:
fallthrough
case http.StatusAccepted:
return newCollectionDocumentCreateResponseReader(arr, opts), nil
default:
return nil, shared.NewResponseStruct().AsArangoErrorWithCode(code)
}
}

func (c collectionDocumentCreate) CreateDocuments(ctx context.Context, documents interface{}) (CollectionDocumentCreateResponseReader, error) {
return c.CreateDocumentsWithOptions(ctx, documents, nil)
}

func (c collectionDocumentCreate) CreateDocumentWithOptions(ctx context.Context, document interface{}, options *CollectionDocumentCreateOptions) (CollectionDocumentCreateResponse, error) {
url := c.collection.url("document")

var meta CollectionDocumentCreateResponse

if options != nil {
meta.Old = options.OldObject
meta.New = options.NewObject
}

response := struct {
*DocumentMeta `json:",inline"`
*shared.ResponseStruct `json:",inline"`
Old *UnmarshalInto `json:"old,omitempty"`
New *UnmarshalInto `json:"new,omitempty"`
}{
DocumentMeta: &meta.DocumentMeta,
ResponseStruct: &meta.ResponseStruct,

Old: newUnmarshalInto(meta.Old),
New: newUnmarshalInto(meta.New),
}

resp, err := connection.CallPost(ctx, c.collection.connection(), url, &response, document, c.collection.withModifiers(options.modifyRequest)...)
if err != nil {
return CollectionDocumentCreateResponse{}, err
}

switch code := resp.Code(); code {
case http.StatusCreated:
fallthrough
case http.StatusAccepted:
return meta, nil
default:
return CollectionDocumentCreateResponse{}, response.AsArangoErrorWithCode(code)
}
}

func (c collectionDocumentCreate) CreateDocument(ctx context.Context, document interface{}) (CollectionDocumentCreateResponse, error) {
return c.CreateDocumentWithOptions(ctx, document, nil)
}

func newCollectionDocumentCreateResponseReader(array connection.Array, options *CollectionDocumentCreateOptions) *collectionDocumentCreateResponseReader {
c := &collectionDocumentCreateResponseReader{array: array, options: options}

if c.options != nil {
c.response.Old = newUnmarshalInto(c.options.OldObject)
c.response.New = newUnmarshalInto(c.options.NewObject)
}

return c
}

var _ CollectionDocumentCreateResponseReader = &collectionDocumentCreateResponseReader{}

type collectionDocumentCreateResponseReader struct {
array connection.Array
options *CollectionDocumentCreateOptions
response struct {
*DocumentMeta
*shared.ResponseStruct `json:",inline"`
Old *UnmarshalInto `json:"old,omitempty"`
New *UnmarshalInto `json:"new,omitempty"`
}
}

func (c *collectionDocumentCreateResponseReader) Read() (CollectionDocumentCreateResponse, error) {
if !c.array.More() {
return CollectionDocumentCreateResponse{}, shared.NoMoreDocumentsError{}
}

var meta CollectionDocumentCreateResponse

if c.options != nil {
meta.Old = c.options.OldObject
meta.New = c.options.NewObject
}

c.response.DocumentMeta = &meta.DocumentMeta
c.response.ResponseStruct = &meta.ResponseStruct

if err := c.array.Unmarshal(&c.response); err != nil {
if err == io.EOF {
return CollectionDocumentCreateResponse{}, shared.NoMoreDocumentsError{}
}
return CollectionDocumentCreateResponse{}, err
}

return meta, nil
}
Loading