示例#1
0
        /// <summary>
        /// Updates existing edge identified by its handle with new edge data.
        /// </summary>
        /// <exception cref="ArgumentException">Specified id value has invalid format.</exception>
        public AResult <Dictionary <string, object> > Update(string id, string json)
        {
            if (!ADocument.IsID(id))
            {
                throw new ArgumentException("Specified id value (" + id + ") has invalid format.");
            }

            var request = new Request(HttpMethod.PATCH, ApiBaseUri.Edge, "/" + id);

            // optional
            request.TrySetQueryStringParameter(ParameterName.WaitForSync, _parameters);
            // optional
            request.TrySetHeaderParameter(ParameterName.IfMatch, _parameters);
            // optional
            if (_parameters.Has(ParameterName.IfMatch))
            {
                request.TrySetQueryStringParameter(ParameterName.Policy, _parameters);
            }
            // optional
            request.TrySetQueryStringParameter(ParameterName.KeepNull, _parameters);
            // optional
            request.TrySetQueryStringParameter(ParameterName.MergeObjects, _parameters);

            request.Body = json;

            var response = _connection.Send(request);
            var result   = new AResult <Dictionary <string, object> >(response);

            switch (response.StatusCode)
            {
            case 201:
            case 202:
                if (response.DataType == DataType.Document)
                {
                    result.Value   = (response.Data as Dictionary <string, object>);
                    result.Success = (result.Value != null);
                }
                break;

            case 412:
                if (response.DataType == DataType.Document)
                {
                    result.Value = (response.Data as Dictionary <string, object>);
                }
                break;

            case 400:
            case 404:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }
示例#2
0
        /// <summary>
        /// Executes specified transaction.
        /// </summary>
        public AResult <T> Execute <T>(string action)
        {
            var request      = new Request(HttpMethod.POST, ApiBaseUri.Transaction, "");
            var bodyDocument = new Dictionary <string, object>();

            // required
            bodyDocument.String(ParameterName.Action, action);
            // required
            if (_readCollections.Count > 0)
            {
                bodyDocument.List(ParameterName.Collections + ".read", _readCollections);
            }
            // required
            if (_writeCollections.Count > 0)
            {
                bodyDocument.List(ParameterName.Collections + ".write", _writeCollections);
            }
            // optional
            Request.TrySetBodyParameter(ParameterName.WaitForSync, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.LockTimeout, _parameters, bodyDocument);
            // optional
            if (_transactionParams.Count > 0)
            {
                bodyDocument.Document(ParameterName.Params, _transactionParams);
            }

            request.Body = JSON.ToJSON(bodyDocument, ASettings.JsonParameters);

            var response = _connection.Send(request);
            var result   = new AResult <T>(response);

            switch (response.StatusCode)
            {
            case 200:
                var body = response.ParseBody <Body <T> >();

                result.Success = (body != null);
                result.Value   = body.Result;
                break;

            case 400:
            case 404:
            case 409:
            case 500:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();
            _readCollections.Clear();
            _writeCollections.Clear();
            _transactionParams.Clear();

            return(result);
        }
示例#3
0
        /// <summary>
        /// Creates new collection in current database context.
        /// </summary>
        public AResult <Dictionary <string, object> > Create(string collectionName)
        {
            var request      = new Request(HttpMethod.POST, ApiBaseUri.Collection, "");
            var bodyDocument = new Dictionary <string, object>();

            // required
            bodyDocument.String(ParameterName.Name, collectionName);
            // optional
            Request.TrySetBodyParameter(ParameterName.Type, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.WaitForSync, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.JournalSize, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.DoCompact, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.IsSystem, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.IsVolatile, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.KeyOptionsType, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.KeyOptionsAllowUserKeys, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.KeyOptionsIncrement, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.KeyOptionsOffset, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.NumberOfShards, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.ShardKeys, _parameters, bodyDocument);

            request.Body = JSON.ToJSON(bodyDocument, ASettings.JsonParameters);

            var response = _connection.Send(request);
            var result   = new AResult <Dictionary <string, object> >(response);

            switch (response.StatusCode)
            {
            case 200:
                if (response.DataType == DataType.Document)
                {
                    result.Value   = (response.Data as Dictionary <string, object>);
                    result.Success = (result.Value != null);
                }
                break;

            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }
        /// <summary>
        /// Updates existing document identified by its handle with new document data.
        /// </summary>
        /// <exception cref="ArgumentException">Specified 'id' value has invalid format.</exception>
        public AResult <Dictionary <string, object> > Update(string id, string json)
        {
            if (!IsID(id))
            {
                throw new ArgumentException("Specified 'id' value (" + id + ") has invalid format.");
            }

            var request = new Request(HttpMethod.PATCH, ApiBaseUri.Document, "/" + id);

            // optional
            request.TrySetQueryStringParameter(ParameterName.WaitForSync, _parameters);
            // optional
            request.TrySetQueryStringParameter(ParameterName.KeepNull, _parameters);
            // optional
            request.TrySetQueryStringParameter(ParameterName.MergeObjects, _parameters);
            // optional
            request.TrySetQueryStringParameter(ParameterName.IgnoreRevs, _parameters);
            // optional
            request.TrySetQueryStringParameter(ParameterName.ReturnNew, _parameters);
            // optional
            request.TrySetQueryStringParameter(ParameterName.ReturnOld, _parameters);
            // optional
            request.TrySetHeaderParameter(ParameterName.IfMatch, _parameters);

            request.Body = json;

            var response = _connection.Send(request);
            var result   = new AResult <Dictionary <string, object> >(response);

            switch (response.StatusCode)
            {
            case 201:
            case 202:
                var body = response.ParseBody <Dictionary <string, object> >();

                result.Success = (body != null);
                result.Value   = body;
                break;

            case 412:
                body = response.ParseBody <Dictionary <string, object> >();

                result.Value = body;
                break;

            case 400:
            case 404:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }
示例#5
0
        /// <summary>
        /// Retrieves result value as list of objects.
        /// </summary>
        public AResult <List <T> > ToList <T>()
        {
            var response = GetResponse();

            var result = new AResult <List <T> >(response);

            switch (response.StatusCode)
            {
            case 201:
                var body = response.ParseBody <Body <List <T> > >();

                result.Success = (body != null);

                if (result.Success)
                {
                    result.Value = new List <T>();
                    result.Value.AddRange(body.Result);
                    result.Extra = new Dictionary <string, object>();

                    CopyExtraBodyFields <List <T> >(body, result.Extra);

                    if (body.HasMore)
                    {
                        var putResult = Put <T>(body.ID);

                        result.Success    = putResult.Success;
                        result.StatusCode = putResult.StatusCode;

                        if (putResult.Success)
                        {
                            result.Value.AddRange(putResult.Value);
                        }
                        else
                        {
                            result.Error = putResult.Error;
                        }
                    }
                }
                break;

            case 400:
            case 404:
            case 405:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();
            _bindVars.Clear();
            _query.Clear();

            return(result);
        }
示例#6
0
        /// <summary>
        /// Creates new edge with document data within specified collection between two document vertices in current database context.
        /// </summary>
        public AResult <Dictionary <string, object> > Create(string collectionName, string fromID, string toID, string json)
        {
            if (!ADocument.IsID(fromID))
            {
                throw new ArgumentException("Specified fromID value (" + fromID + ") has invalid format.");
            }

            if (!ADocument.IsID(toID))
            {
                throw new ArgumentException("Specified toID value (" + toID + ") has invalid format.");
            }

            var request = new Request(HttpMethod.POST, ApiBaseUri.Edge, "");

            // required
            request.QueryString.Add(ParameterName.Collection, collectionName);
            // required
            request.QueryString.Add(ParameterName.From, fromID);
            // required
            request.QueryString.Add(ParameterName.To, toID);
            // optional
            request.TrySetQueryStringParameter(ParameterName.CreateCollection, _parameters);
            // optional
            request.TrySetQueryStringParameter(ParameterName.WaitForSync, _parameters);

            request.Body = json;

            var response = _connection.Send(request);
            var result   = new AResult <Dictionary <string, object> >(response);

            switch (response.StatusCode)
            {
            case 201:
            case 202:
                if (response.DataType == DataType.Document)
                {
                    result.Value   = (response.Data as Dictionary <string, object>);
                    result.Success = (result.Value != null);
                }
                break;

            case 400:
            case 404:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }
示例#7
0
        /// <summary>
        /// Retrieves result which does not contain value. This can be used to execute non-query operations where only success information is relevant.
        /// </summary>
        public AResult <object> ExecuteNonQuery()
        {
            var listResult = ToList <Dictionary <string, object> >();
            var result     = new AResult <object>();

            result.StatusCode = listResult.StatusCode;
            result.Success    = listResult.Success;
            result.Extra      = listResult.Extra;
            result.Error      = listResult.Error;
            result.Value      = null;

            return(result);
        }
示例#8
0
        internal AResult <List <object> > Put(string cursorID)
        {
            var request = new Request(HttpMethod.PUT, ApiBaseUri.Cursor, "/" + cursorID);

            var response = _connection.Send(request);
            var result   = new AResult <List <object> >(response);

            switch (response.StatusCode)
            {
            case 200:
                if (response.DataType == DataType.Document)
                {
                    var responseDocument = (response.Data as Dictionary <string, object>);

                    result.Success = (responseDocument != null);

                    if (result.Success)
                    {
                        result.Value = new List <object>();
                        result.Value.AddRange(responseDocument.List <object>("result"));

                        if (responseDocument.IsBool("hasMore") && responseDocument.Bool("hasMore"))
                        {
                            var resultCursorID = responseDocument.String("id");
                            var putResult      = Put(resultCursorID);

                            result.Success    = putResult.Success;
                            result.StatusCode = putResult.StatusCode;

                            if (putResult.Success)
                            {
                                result.Value.AddRange(putResult.Value);
                            }
                            else
                            {
                                result.Error = putResult.Error;
                            }
                        }
                    }
                }
                break;

            case 400:
            case 404:
            default:
                break;
            }

            return(result);
        }
示例#9
0
        public string ToJson()
        {
            var response = GetResponse();

            var result = new AResult <string>(response);

            if (result.StatusCode != 201)
            {
                throw new Exception("Arango returned: " + result.Error + "," + result.Extra + ", " + result.StatusCode);
            }
            else
            {
                return(response.Body);
            }
        }
示例#10
0
        public AResult <List <T> > ToListFast <T>()
        {
            var listResult = ToList();
            var jsonx      = Arango.fastJSON.JSON.ToObject <ArangoResultWrapper <T> >(LastResponse);
            var result     = new AResult <List <T> >();

            result.Value = new List <T>();
            result.Value = jsonx.result.ToList();

            result.StatusCode = listResult.StatusCode;
            result.Success    = listResult.Success;
            result.Extra      = listResult.Extra;
            result.Error      = listResult.Error;

            return(result);
        }
示例#11
0
        /// <summary>
        /// Retrieves specified edge.
        /// </summary>
        /// <exception cref="ArgumentException">Specified id value has invalid format.</exception>
        public AResult <T> Get <T>(string id)
        {
            var getResult = Get(id);
            var result    = new AResult <T>();

            result.StatusCode = getResult.StatusCode;
            result.Success    = getResult.Success;
            result.Extra      = getResult.Extra;
            result.Error      = getResult.Error;

            if (getResult.Success)
            {
                result.Value = getResult.Value.ToObject <T>();
            }

            return(result);
        }
示例#12
0
        internal AResult <List <T> > Put <T>(string cursorID)
        {
            var request = new Request(HttpMethod.PUT, ApiBaseUri.Cursor, "/" + cursorID);

            var response = _connection.Send(request);
            var result   = new AResult <List <T> >(response);

            switch (response.StatusCode)
            {
            case 200:
                var body = response.ParseBody <Body <List <T> > >();

                result.Success = (body.Result != null);

                if (result.Success)
                {
                    result.Value = new List <T>();
                    result.Value.AddRange(body.Result);

                    if (body.HasMore)
                    {
                        var putResult = Put <T>(body.ID);

                        result.Success    = putResult.Success;
                        result.StatusCode = putResult.StatusCode;

                        if (putResult.Success)
                        {
                            result.Value.AddRange(putResult.Value);
                        }
                        else
                        {
                            result.Error = putResult.Error;
                        }
                    }
                }
                break;

            case 400:
            case 404:
            default:
                break;
            }

            return(result);
        }
示例#13
0
        /// <summary>
        /// Checks for existence of specified document.
        /// </summary>
        /// <exception cref="ArgumentException">Specified 'id' value has invalid format.</exception>
        public AResult <string> Check(string id)
        {
            if (!IsID(id))
            {
                throw new ArgumentException("Specified 'id' value (" + id + ") has invalid format.");
            }

            var request = new Request(HttpMethod.HEAD, ApiBaseUri.Document, "/" + id);

            // optional
            request.TrySetHeaderParameter(ParameterName.IfMatch, _parameters);
            // optional: If revision is different -> HTTP 200. If revision is identical -> HTTP 304.
            request.TrySetHeaderParameter(ParameterName.IfNoneMatch, _parameters);

            var response = _connection.Send(request);
            var result   = new AResult <string>(response);

            switch (response.StatusCode)
            {
            case 200:
                if ((response.Headers["ETag"] ?? "").Trim().Length > 0)
                {
                    result.Value   = response.Headers["ETag"].Replace("\"", "");
                    result.Success = (result.Value != null);
                }
                break;

            case 304:
            case 412:
                if ((response.Headers["ETag"] ?? "").Trim().Length > 0)
                {
                    result.Value = response.Headers["ETag"].Replace("\"", "");
                }
                break;

            case 404:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }
示例#14
0
        /// <summary>
        /// Retrieves specified edge.
        /// </summary>
        /// <exception cref="ArgumentException">Specified id value has invalid format.</exception>
        public AResult <Dictionary <string, object> > Get(string id)
        {
            if (!ADocument.IsID(id))
            {
                throw new ArgumentException("Specified id value (" + id + ") has invalid format.");
            }

            var request = new Request(HttpMethod.GET, ApiBaseUri.Edge, "/" + id);

            // optional
            request.TrySetHeaderParameter(ParameterName.IfMatch, _parameters);
            // optional: If revision is different -> HTTP 200. If revision is identical -> HTTP 304.
            request.TrySetHeaderParameter(ParameterName.IfNoneMatch, _parameters);

            var response = _connection.Send(request);
            var result   = new AResult <Dictionary <string, object> >(response);

            switch (response.StatusCode)
            {
            case 200:
                if (response.DataType == DataType.Document)
                {
                    result.Value   = (response.Data as Dictionary <string, object>);
                    result.Success = (result.Value != null);
                }
                break;

            case 412:
                if (response.DataType == DataType.Document)
                {
                    result.Value = (response.Data as Dictionary <string, object>);
                }
                break;

            case 304:
            case 404:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }
示例#15
0
        private AResult <T> Request <T>(HttpMethod httpMethod, string relativeUri)
        {
            var request = new Request(httpMethod, relativeUri);

            if (_parameters.Has(ParameterName.Body))
            {
                request.Body = JSON.ToJSON(_parameters.Object(ParameterName.Body), ASettings.JsonParameters);
            }

            var response = _connection.Send(request);
            var result   = new AResult <T>(response);

            result.Value = response.ParseBody <T>();

            _parameters.Clear();

            return(result);
        }
示例#16
0
        /// <summary>
        /// Retrieves result value as list of documents.
        /// </summary>
        public AResult <List <Dictionary <string, object> > > ToDocuments()
        {
            var type       = typeof(Dictionary <string, object>);
            var listResult = ToList();
            var result     = new AResult <List <Dictionary <string, object> > >();

            result.StatusCode = listResult.StatusCode;
            result.Success    = listResult.Success;
            result.Extra      = listResult.Extra;
            result.Error      = listResult.Error;

            if (listResult.Success)
            {
                result.Value = listResult.Value.Select(o => Convert.ChangeType(o, type)).Cast <Dictionary <string, object> >().ToList();
            }

            return(result);
        }
示例#17
0
        /// <summary>
        /// Retrieves specified document.
        /// </summary>
        /// <exception cref="ArgumentException">Specified 'id' value has invalid format.</exception>
        public AResult <T> Get <T>(string id)
        {
            if (!IsID(id))
            {
                throw new ArgumentException("Specified 'id' value (" + id + ") has invalid format.");
            }

            var request = new Request(HttpMethod.GET, ApiBaseUri.Document, "/" + id);

            // optional
            request.TrySetHeaderParameter(ParameterName.IfMatch, _parameters);
            // optional: If revision is different -> HTTP 200. If revision is identical -> HTTP 304.
            request.TrySetHeaderParameter(ParameterName.IfNoneMatch, _parameters);

            var response = _connection.Send(request);
            var result   = new AResult <T>(response);

            switch (response.StatusCode)
            {
            case 200:
                var body = response.ParseBody <T>();

                result.Success = (body != null);
                result.Value   = body;
                break;

            case 412:
                body = response.ParseBody <T>();

                result.Value = body;
                break;

            case 304:
            case 404:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }
示例#18
0
        /// <summary>
        /// Retrieves list of edges from specified edge type collection to specified document vertex with given direction.
        /// </summary>
        /// <exception cref="ArgumentException">Specified 'startVertexID' value has invalid format.</exception>
        public AResult <List <Dictionary <string, object> > > GetEdges(string collectionName, string startVertexID, ADirection direction)
        {
            if (!IsID(startVertexID))
            {
                throw new ArgumentException("Specified 'startVertexID' value (" + startVertexID + ") has invalid format.");
            }

            var request = new Request(HttpMethod.GET, ApiBaseUri.Edges, "/" + collectionName);

            // required
            request.QueryString.Add(ParameterName.Vertex, startVertexID);
            // required
            request.QueryString.Add(ParameterName.Direction, direction.ToString().ToLower());

            var response = _connection.Send(request);
            var result   = new AResult <List <Dictionary <string, object> > >(response);

            switch (response.StatusCode)
            {
            case 200:
                var body = response.ParseBody <Dictionary <string, object> >();

                result.Success = (body != null);

                if (result.Success)
                {
                    result.Value = body.List <Dictionary <string, object> >("edges");
                }
                break;

            case 400:
            case 404:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }
示例#19
0
        /// <summary>
        /// Analyzes specified AQL query.
        /// </summary>
        public AResult <Dictionary <string, object> > Parse(string query)
        {
            var request      = new Request(HttpMethod.POST, ApiBaseUri.Query, "");
            var bodyDocument = new Dictionary <string, object>();

            // required
            bodyDocument.String(ParameterName.Query, Minify(query));

            request.Body = JSON.ToJSON(bodyDocument, ASettings.JsonParameters);

            var response = _connection.Send(request);
            var result   = new AResult <Dictionary <string, object> >(response);

            switch (response.StatusCode)
            {
            case 200:
                var body = response.ParseBody <Dictionary <string, object> >();

                result.Success = (body != null);

                if (result.Success)
                {
                    result.Value = body.CloneExcept("code", "error");
                }
                break;

            case 400:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();
            _bindVars.Clear();
            _query.Clear();

            return(result);
        }
示例#20
0
        /// <summary>
        /// Creates new document within specified collection in current database context.
        /// </summary>
        public AResult <Dictionary <string, object> > Create(string collectionName, string json)
        {
            var request = new Request(HttpMethod.POST, ApiBaseUri.Document, "");

            // required
            request.QueryString.Add(ParameterName.Collection, collectionName);
            // optional
            request.TrySetQueryStringParameter(ParameterName.CreateCollection, _parameters);
            // optional
            request.TrySetQueryStringParameter(ParameterName.WaitForSync, _parameters);

            request.Body = json;

            var response = _connection.Send(request);
            var result   = new AResult <Dictionary <string, object> >(response);

            switch (response.StatusCode)
            {
            case 201:
            case 202:
                if (response.DataType == DataType.Document)
                {
                    result.Value   = (response.Data as Dictionary <string, object>);
                    result.Success = (result.Value != null);
                }
                break;

            case 400:
            case 404:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }
示例#21
0
        /// <summary>
        /// Loads specified collection into memory.
        /// </summary>
        public AResult <Dictionary <string, object> > Load(string collectionName)
        {
            var request = new Request(HttpMethod.PUT, ApiBaseUri.Collection, "/" + collectionName + "/load");

            if (_parameters.Has(ParameterName.Count))
            {
                var bodyDocument = new Dictionary <string, object>();

                // optional
                Request.TrySetBodyParameter(ParameterName.Count, _parameters, bodyDocument);

                request.Body = JSON.ToJSON(bodyDocument, ASettings.JsonParameters);
            }

            var response = _connection.Send(request);
            var result   = new AResult <Dictionary <string, object> >(response);

            switch (response.StatusCode)
            {
            case 200:
                if (response.DataType == DataType.Document)
                {
                    result.Value   = (response.Data as Dictionary <string, object>);
                    result.Success = (result.Value != null);
                }
                break;

            case 400:
            case 404:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }
示例#22
0
        /// <summary>
        /// Creates new or replaces existing AQL user function with specified name and code.
        /// </summary>
        public AResult <bool> Register(string name, string code)
        {
            var request      = new Request(HttpMethod.POST, ApiBaseUri.AqlFunction, "");
            var bodyDocument = new Dictionary <string, object>();

            // required
            bodyDocument.String(ParameterName.Name, name);
            // required
            bodyDocument.String(ParameterName.Code, code);
            // optional
            Request.TrySetBodyParameter(ParameterName.IsDeterministic, _parameters, bodyDocument);

            request.Body = JSON.ToJSON(bodyDocument, ASettings.JsonParameters);

            var response = _connection.Send(request);
            var result   = new AResult <bool>(response);

            switch (response.StatusCode)
            {
            case 200:
            case 201:
                if (response.DataType == DataType.Document)
                {
                    result.Success = true;
                    result.Value   = true;
                }
                break;

            case 400:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }
示例#23
0
        /// <summary>
        /// Retrieves result value as single generic object.
        /// </summary>
        public AResult <T> ToObject <T>()
        {
            var listResult = ToList <T>();
            var result     = new AResult <T>();

            result.StatusCode = listResult.StatusCode;
            result.Success    = listResult.Success;
            result.Extra      = listResult.Extra;
            result.Error      = listResult.Error;

            if (listResult.Success)
            {
                if (listResult.Value.Count > 0)
                {
                    result.Value = (T)listResult.Value[0];
                }
                else
                {
                    result.Value = (T)Activator.CreateInstance(typeof(T));
                }
            }

            return(result);
        }
示例#24
0
        /// <summary>
        /// Retrieves result value as single object.
        /// </summary>
        public AResult <object> ToObject()
        {
            var listResult = ToList <object>();
            var result     = new AResult <object>();

            result.StatusCode = listResult.StatusCode;
            result.Success    = listResult.Success;
            result.Extra      = listResult.Extra;
            result.Error      = listResult.Error;

            if (listResult.Success)
            {
                if (listResult.Value.Count > 0)
                {
                    result.Value = listResult.Value[0];
                }
                else
                {
                    result.Value = new object();
                }
            }

            return(result);
        }
示例#25
0
        /// <summary>
        /// Retrieves result value as list of objects.
        /// </summary>
        public AResult <List <T> > ToList <T>()
        {
            var request      = new Request(HttpMethod.POST, ApiBaseUri.Cursor, "");
            var bodyDocument = new Dictionary <string, object>();

            // required
            bodyDocument.String(ParameterName.Query, _query.ToString());
            // optional
            Request.TrySetBodyParameter(ParameterName.Count, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.BatchSize, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.TTL, _parameters, bodyDocument);
            // optional
            if (_bindVars.Count > 0)
            {
                bodyDocument.Document(ParameterName.BindVars, _bindVars);
            }

            // TODO: options parameter

            request.Body = JSON.ToJSON(bodyDocument, ASettings.JsonParameters);
            //this.LastRequest = request.Body;
            var response = _connection.Send(request);
            //this.LastResponse = response.Body;
            var result = new AResult <List <T> >(response);

            switch (response.StatusCode)
            {
            case 201:
                var body = response.ParseBody <Body <List <T> > >();

                result.Success = (body != null);

                if (result.Success)
                {
                    result.Value = new List <T>();
                    result.Value.AddRange(body.Result);
                    result.Extra = new Dictionary <string, object>();

                    CopyExtraBodyFields <List <T> >(body, result.Extra);

                    if (body.HasMore)
                    {
                        var putResult = Put <T>(body.ID);

                        result.Success    = putResult.Success;
                        result.StatusCode = putResult.StatusCode;

                        if (putResult.Success)
                        {
                            result.Value.AddRange(putResult.Value);
                        }
                        else
                        {
                            result.Error = putResult.Error;
                        }
                    }
                }
                break;

            case 400:
            case 404:
            case 405:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();
            _bindVars.Clear();
            _query.Clear();

            return(result);
        }
示例#26
0
        /// <summary>
        /// Retrieves result value as list of objects.
        /// </summary>
        public AResult <List <object> > ToList()
        {
            var request      = new Request(HttpMethod.POST, ApiBaseUri.Cursor, "");
            var bodyDocument = new Dictionary <string, object>();

            // required
            bodyDocument.String(ParameterName.Query, _query.ToString());
            // optional
            Request.TrySetBodyParameter(ParameterName.Count, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.BatchSize, _parameters, bodyDocument);
            // optional
            Request.TrySetBodyParameter(ParameterName.TTL, _parameters, bodyDocument);
            // optional
            if (_bindVars.Count > 0)
            {
                bodyDocument.Document(ParameterName.BindVars, _bindVars);
            }

            // TODO: options parameter

            request.Body     = JSON.ToJSON(bodyDocument, ASettings.JsonParameters);
            this.LastRequest = request.Body;
            var response = _connection.Send(request);

            this.LastResponse = response.Body;
            var result = new AResult <List <object> >(response);

            switch (response.StatusCode)
            {
            case 201:
                if (response.DataType == DataType.Document)
                {
                    var responseDocument = (response.Data as Dictionary <string, object>);

                    result.Success = (responseDocument != null);

                    if (result.Success)
                    {
                        result.Value = new List <object>();
                        result.Value.AddRange(responseDocument.List <object>("result"));
                        result.Extra = responseDocument.CloneExcept("code", "error", "hasMore", "result");

                        if (responseDocument.IsBool("hasMore") && responseDocument.Bool("hasMore"))
                        {
                            var cursorID  = responseDocument.String("id");
                            var putResult = Put(cursorID);

                            result.Success    = putResult.Success;
                            result.StatusCode = putResult.StatusCode;

                            if (putResult.Success)
                            {
                                result.Value.AddRange(putResult.Value);
                            }
                            else
                            {
                                result.Error = putResult.Error;
                            }
                        }
                    }
                }
                break;

            case 400:
            case 404:
            case 405:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();
            _bindVars.Clear();
            _query.Clear();

            return(result);
        }
示例#27
0
        /// <summary>
        /// Creates index within specified collection in current database context.
        /// </summary>
        public AResult <Dictionary <string, object> > Create(string collectionName)
        {
            var request      = new Request(HttpMethod.POST, ApiBaseUri.Index, "");
            var bodyDocument = new Dictionary <string, object>();

            // required
            request.QueryString.Add(ParameterName.Collection, collectionName);

            // required
            bodyDocument.String(ParameterName.Type, _parameters.String(ParameterName.Type));

            switch (_parameters.Enum <AIndexType>(ParameterName.Type))
            {
            case AIndexType.Cap:
                Request.TrySetBodyParameter(ParameterName.ByteSize, _parameters, bodyDocument);
                Request.TrySetBodyParameter(ParameterName.Size, _parameters, bodyDocument);
                break;

            case AIndexType.Fulltext:
                Request.TrySetBodyParameter(ParameterName.Fields, _parameters, bodyDocument);
                Request.TrySetBodyParameter(ParameterName.MinLength, _parameters, bodyDocument);
                break;

            case AIndexType.Geo:
                Request.TrySetBodyParameter(ParameterName.Fields, _parameters, bodyDocument);
                Request.TrySetBodyParameter(ParameterName.GeoJson, _parameters, bodyDocument);
                break;

            case AIndexType.Hash:
                Request.TrySetBodyParameter(ParameterName.Fields, _parameters, bodyDocument);
                Request.TrySetBodyParameter(ParameterName.Sparse, _parameters, bodyDocument);
                Request.TrySetBodyParameter(ParameterName.Unique, _parameters, bodyDocument);
                break;

            case AIndexType.Skiplist:
                Request.TrySetBodyParameter(ParameterName.Fields, _parameters, bodyDocument);
                Request.TrySetBodyParameter(ParameterName.Sparse, _parameters, bodyDocument);
                Request.TrySetBodyParameter(ParameterName.Unique, _parameters, bodyDocument);
                break;

            default:
                break;
            }

            request.Body = JSON.ToJSON(bodyDocument, ASettings.JsonParameters);

            var response = _connection.Send(request);
            var result   = new AResult <Dictionary <string, object> >(response);

            switch (response.StatusCode)
            {
            case 200:
            case 201:
                if (response.DataType == DataType.Document)
                {
                    result.Value   = (response.Data as Dictionary <string, object>);
                    result.Success = (result.Value != null);
                }
                break;

            case 400:
            case 404:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }
示例#28
0
        /// <summary>
        /// Creates new database with given name and user list.
        /// </summary>
        public AResult <bool> Create(string databaseName, List <AUser> users)
        {
            var request      = new Request(HttpMethod.POST, ApiBaseUri.Database, "");
            var bodyDocument = new Dictionary <string, object>();

            // required: database name
            bodyDocument.String("name", databaseName);

            // optional: list of users
            if ((users != null) && (users.Count > 0))
            {
                var userList = new List <Dictionary <string, object> >();

                foreach (var user in users)
                {
                    var userItem = new Dictionary <string, object>();

                    if (!string.IsNullOrEmpty(user.Username))
                    {
                        userItem.String("username", user.Username);
                    }

                    if (!string.IsNullOrEmpty(user.Password))
                    {
                        userItem.String("passwd", user.Password);
                    }

                    userItem.Bool("active", user.Active);

                    if (user.Extra != null)
                    {
                        userItem.Document("extra", user.Extra);
                    }

                    userList.Add(userItem);
                }

                bodyDocument.List("users", userList);
            }

            request.Body = JSON.ToJSON(bodyDocument, ASettings.JsonParameters);

            var response = _connection.Send(request);
            var result   = new AResult <bool>(response);

            switch (response.StatusCode)
            {
            case 201:
                var body = response.ParseBody <Body <bool> >();

                result.Success = (body != null);
                result.Value   = body.Result;
                break;

            case 400:
            case 403:
            case 404:
            default:
                // Arango error
                break;
            }

            _parameters.Clear();

            return(result);
        }