Esempio n. 1
0
        /// <summary>
        /// Executes specified transaction.
        /// </summary>
        public async Task <AResult <T> > Execute <T>(string action)
        {
            var request  = _requestFactory.Create(HttpMethod.Post, ApiBaseUri.Transaction, "");
            var document = new Dictionary <string, object>();

            // required
            document.Add(ParameterName.Action, action);
            // required
            if (_readCollections.Count > 0)
            {
                document.Add(ParameterName.Collections + ".read", _readCollections);
            }
            // required
            if (_writeCollections.Count > 0)
            {
                document.Add(ParameterName.Collections + ".write", _writeCollections);
            }
            // optional
            if (_waitForSync.HasValue)
            {
                document.Add(ParameterName.WaitForSync, _waitForSync.Value);
            }
            // optional
            if (_lockTimeout.HasValue)
            {
                document.Add(ParameterName.LockTimeout, _lockTimeout.Value);
            }
            // optional
            if (_transactionParams.Count > 0)
            {
                document.Add(ParameterName.Params, _transactionParams);
            }

            request.SetBody(document);

            var response = await _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:
                throw new ArangoException(response.Body);
            }

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Deletes specified document.
        /// </summary>
        /// <exception cref="ArgumentException">Specified 'id' value has invalid format.</exception>
        public async Task <AResult <Dictionary <string, object> > > ById(string id)
        {
            if (!Helpers.IsID(id))
            {
                throw new ArgumentException("Specified 'id' value (" + id + ") has invalid format.");
            }

            var request = _requestFactory.Create(HttpMethod.Delete, ApiBaseUri.Document, "/" + id);

            if (_waitForSync.HasValue)
            {
                request.QueryString.Add(ParameterName.WaitForSync, _waitForSync.Value.ToString().ToLower());
            }
            if (_returnOld.HasValue)
            {
                request.QueryString.Add(ParameterName.ReturnOld, _returnOld.Value.ToString().ToLower());
            }
            if (!String.IsNullOrEmpty(_ifMatch))
            {
                request.Headers.Add(ParameterName.IfMatch, _ifMatch);
            }

            using var response = await _collection.Request(request);

            var result = new AResult <Dictionary <string, object> >()
            {
                StatusCode = (int)response.StatusCode,
                Success    = response.IsSuccessStatusCode
            };

            switch (result.StatusCode)
            {
            case 200:
            case 202:
                var body = _jsonSerializer.Deserialize <Dictionary <string, object> >(await response.Content.ReadAsStreamAsync());

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

            case 412:
                var rev = (string)_jsonSerializer.Deserialize <Dictionary <string, object> >(await response.Content.ReadAsStreamAsync())["_rev"];

                throw new VersionCheckViolationException(rev);

            case 404:
                throw new CollectionNotFoundException();

            default:
                throw new ArangoException();
            }

            return(result);
        }
Esempio n. 3
0
        private async Task <AResult <T> > Request <T>(HttpMethod httpMethod, string relativeUri, string mount, Stream zip = null)
        {
            var request = _requestFactory.Create(httpMethod, relativeUri);

            request.QueryString.Add("mount", mount);
            request.Body = zip;

            var response = await _connection.Send(request);

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

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

            return(result);
        }
Esempio n. 4
0
        private async Task <AResult <T> > Request <T>(HttpMethod httpMethod, string relativeUri)
        {
            var request = _requestFactory.Create(httpMethod, relativeUri);

            if (_body != null)
            {
                request.SetBody(_body);
            }

            var response = await _connection.Send(request);

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

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

            return(result);
        }
Esempio n. 5
0
        internal async Task <AResult <T> > RequestQuery <T>(Request request, string uri = "")
        {
            using var response = await Request(request, uri);

            var result = new AResult <T>()
            {
                StatusCode = (int)response.StatusCode,
                Success    = response.IsSuccessStatusCode
            };

            if (response.IsSuccessStatusCode)
            {
                result.Value = _jsonSerializer.Deserialize <T>(await response.Content.ReadAsStreamAsync());
            }

            return(result);
        }
Esempio n. 6
0
        /// <summary>
        /// Checks for existence of specified document.
        /// </summary>
        /// <exception cref="ArgumentException">Specified 'id' value has invalid format.</exception>
        public async Task<AResult<T>> ById(string id)
        {
            if (!Helpers.IsID(id))
            {
                throw new ArgumentException("Specified 'id' value (" + id + ") has invalid format.");
            }
            
            var request = _requestFactory.Create(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);
            
            using var response = await _collection.Request(request);
            
            var result = new AResult<T>()
            {
                StatusCode = (int) response.StatusCode,
                Success = response.IsSuccessStatusCode
            };

            switch (result.StatusCode)
            {
                case 200:
                case 304:
                    var body = _jsonSerializer.Deserialize<T>(await response.Content.ReadAsStreamAsync());
                    
                    result.Success = (body != null);
                    result.Value = body;
                    break;
                case 412:
                    var rev = (string) _jsonSerializer.Deserialize<Dictionary<string, object>>(await response.Content.ReadAsStreamAsync())["_rev"];
                    
                    throw new VersionCheckViolationException(rev);
                case 404:
                    throw new CollectionNotFoundException();
                default:
                    throw new ArangoException();
            }
            
            return result;
        }
Esempio n. 7
0
        /// <summary>
        /// Checks for existence of specified document.
        /// </summary>
        /// <exception cref="ArgumentException">Specified 'id' value has invalid format.</exception>
        public async Task <AResult <string> > ById(string id)
        {
            if (!Helpers.IsID(id))
            {
                throw new ArgumentException("Specified 'id' value (" + id + ") has invalid format.");
            }

            var request = _requestFactory.Create(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);

            using var response = await _collection.Request(request);

            var result = new AResult <string>()
            {
                StatusCode = (int)response.StatusCode,
                Success    = response.IsSuccessStatusCode
            };

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

            case 412:
                throw new VersionCheckViolationException(response.Headers.ETag?.Tag.Replace("\"", ""));

            default:
                throw new ArangoException();
            }

            return(result);
        }
Esempio n. 8
0
        /// <summary>
        /// Retrieves information about collections in current database connection.
        /// </summary>
        public async Task <AResult <List <Dictionary <string, object> > > > GetAllCollections()
        {
            var request = _requestFactory.Create(HttpMethod.Get, ApiBaseUri.Collection, "");

            var response = await Send(request);

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

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

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

            default:
                throw new ArangoException(response.Body);
            }

            return(result);
        }
Esempio n. 9
0
        /// <summary>
        /// Retrieves list of accessible databases which current user can access without specifying a different username or password.
        /// </summary>
        public async Task <AResult <List <string> > > GetAccessibleDatabases()
        {
            var request = _requestFactory.Create(HttpMethod.Get, ApiBaseUri.Database, "/user");

            var response = await Send(request);

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

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

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

            default:
                throw new ArangoException(response.Body);
            }

            return(result);
        }
Esempio n. 10
0
        /// <summary>
        /// Creates new or replaces existing AQL user function with specified name and code.
        /// </summary>
        public async Task <AResult <bool> > Register(string name, string code)
        {
            var request  = _requestFactory.Create(HttpMethod.Post, ApiBaseUri.AqlFunction, "");
            var document = new Dictionary <string, object>();

            // required
            document.Add(ParameterName.Name, name);
            // required
            document.Add(ParameterName.Code, code);
            // optional
            if (_isDeterministic.HasValue)
            {
                document.Add(ParameterName.IsDeterministic, _isDeterministic.Value.ToString().ToLower());
            }

            request.SetBody(document);

            var response = await _connection.Send(request);

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

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

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

            return(result);
        }
Esempio n. 11
0
        /// <summary>
        /// Completely replaces existing document identified by its handle with new document data.
        /// </summary>
        /// <exception cref="ArgumentException">Specified id value has invalid format.</exception>
        public async Task <AResult <List <T> > > Documents(IEnumerable <T> document)
        {
            // TODO validate

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

            var request = _requestFactory.Create(HttpMethod.Patch, ApiBaseUri.Document, "/" + _collection.Name);

            if (_waitForSync.HasValue)
            {
                request.QueryString.Add(ParameterName.WaitForSync, _waitForSync.Value.ToString().ToLower());
            }
            if (_keepNull.HasValue)
            {
                request.QueryString.Add(ParameterName.KeepNull, _keepNull.Value.ToString().ToLower());
            }
            if (_mergeObjects.HasValue)
            {
                request.QueryString.Add(ParameterName.MergeObjects, _mergeObjects.Value.ToString().ToLower());
            }
            if (_ignoreRevs.HasValue)
            {
                request.QueryString.Add(ParameterName.IgnoreRevs, _ignoreRevs.Value.ToString().ToLower());
            }
            if (_returnNew.HasValue)
            {
                request.QueryString.Add(ParameterName.ReturnNew, _returnNew.Value.ToString().ToLower());
            }
            if (_returnOld.HasValue)
            {
                request.QueryString.Add(ParameterName.ReturnOld, _returnOld.Value.ToString().ToLower());
            }
            if (!String.IsNullOrEmpty(_ifMatch))
            {
                request.Headers.Add(ParameterName.IfMatch, _ifMatch);
            }

            request.SetBody(document);

            var response = await _collection.Send(request);

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

            switch (response.StatusCode)
            {
            case 201:
            case 202:
                response.Headers.TryGetValues("X-Arango-Error-Codes", out var values);
                if (values != null && values.Any())
                {
                    throw new MultipleException();
                }

                List <T> body;
                if (_returnNew.HasValue && _returnNew.Value)
                {
                    body = response.ParseBody <List <DocumentCreateResponse <T> > >().Select(e => e.New).ToList();
                }
                else if (_returnOld.HasValue && _returnOld.Value)
                {
                    body = response.ParseBody <List <DocumentCreateResponse <T> > >().Select(e => e.Old).ToList();
                }
                else
                {
                    body = response.ParseBody <List <T> >();
                }

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

            case 404:
                throw new CollectionNotFoundException();

            default:
                throw new ArangoException(response.Body);
            }

            return(result);
        }
Esempio n. 12
0
        /// <summary>
        /// Updates existing document identified by its handle with new document data.
        /// </summary>
        public async Task <AResult <T> > DocumentById(string id, T document)
        {
            if (!Helpers.IsID(id))
            {
                throw new ArgumentException("Specified 'id' value (" + id + ") has invalid format.");
            }

            var request = _requestFactory.Create(HttpMethod.Patch, ApiBaseUri.Document, "/" + id);

            if (_waitForSync.HasValue)
            {
                request.QueryString.Add(ParameterName.WaitForSync, _waitForSync.Value.ToString().ToLower());
            }
            if (_keepNull.HasValue)
            {
                request.QueryString.Add(ParameterName.KeepNull, _keepNull.Value.ToString().ToLower());
            }
            if (_mergeObjects.HasValue)
            {
                request.QueryString.Add(ParameterName.MergeObjects, _mergeObjects.Value.ToString().ToLower());
            }
            if (_ignoreRevs.HasValue)
            {
                request.QueryString.Add(ParameterName.IgnoreRevs, _ignoreRevs.Value.ToString().ToLower());
            }
            if (_returnNew.HasValue)
            {
                request.QueryString.Add(ParameterName.ReturnNew, _returnNew.Value.ToString().ToLower());
            }
            if (_returnOld.HasValue)
            {
                request.QueryString.Add(ParameterName.ReturnOld, _returnOld.Value.ToString().ToLower());
            }
            if (!String.IsNullOrEmpty(_ifMatch))
            {
                request.Headers.Add(ParameterName.IfMatch, _ifMatch);
            }

            request.SetBody(document);

            var response = await _collection.Send(request);

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

            switch (response.StatusCode)
            {
            case 201:
            case 202:
                T body;
                if (_returnNew.HasValue && _returnNew.Value)
                {
                    body = response.ParseBody <DocumentCreateResponse <T> >()?.New;
                }
                else if (_returnOld.HasValue && _returnOld.Value)
                {
                    body = response.ParseBody <DocumentCreateResponse <T> >()?.Old;
                }
                else
                {
                    body = response.ParseBody <T>();
                }

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

            case 412:
                var rev = (string)response.ParseBody <Dictionary <string, object> >()["_rev"];

                throw new VersionCheckViolationException(rev);

            case 404:
                throw new CollectionNotFoundException();

            default:
                throw new ArangoException(response.Body);
            }

            return(result);
        }
Esempio n. 13
0
        /// <summary>
        /// Creates multiple new document within specified collection in current database context.
        /// </summary>
        public async Task <AResult <List <T> > > Documents(IEnumerable <T> document)
        {
            List <T> documents = document.ToList();

            if (documents.Count == 1)
            {
                AResult <T> resultSingle;
                try
                {
                    resultSingle = await Document(documents.First());
                }
                catch (UniqueConstraintViolationException)
                {
                    throw new MultipleException();
                }

                return(new AResult <List <T> >()
                {
                    StatusCode = resultSingle.StatusCode,
                    Success = resultSingle.Success,
                    Extra = resultSingle.Extra,
                    Value = new List <T>()
                    {
                        resultSingle.Value
                    }
                });
            }

            var request = _requestFactory.Create(HttpMethod.Post, ApiBaseUri.Document, "/" + _collection.Name);

            if (_waitForSync.HasValue)
            {
                request.QueryString.Add(ParameterName.WaitForSync, _waitForSync.Value.ToString().ToLower());
            }
            if (_returnNew.HasValue)
            {
                request.QueryString.Add(ParameterName.ReturnNew, _returnNew.Value.ToString().ToLower());
            }
            if (_returnOld.HasValue)
            {
                request.QueryString.Add(ParameterName.ReturnOld, _returnOld.Value.ToString().ToLower());
            }
            if (_overwriteMode.HasValue)
            {
                request.QueryString.Add(ParameterName.Overwrite, "true");
                request.QueryString.Add(ParameterName.OverwriteMode, _overwriteMode.Value.ToString().ToLower());
            }

            request.SetBody(documents);

            using var response = await _collection.Request(request);

            var result = new AResult <List <T> >()
            {
                StatusCode = (int)response.StatusCode,
                Success    = response.IsSuccessStatusCode
            };

            switch (result.StatusCode)
            {
            case 201:
            case 202:
                response.Headers.TryGetValues("X-Arango-Error-Codes", out var errors);
                if (errors != null && errors.Any())
                {
                    throw new MultipleException();
                }

                var values = _jsonSerializer.Deserialize <List <DocumentCreateResponse <T> > >(await response.Content.ReadAsStreamAsync());

                if (_returnNew.HasValue && _returnNew.Value)
                {
                    result.Value = values.Select(e => e.New).ToList();
                }
                else if (_returnOld.HasValue && _returnOld.Value)
                {
                    result.Value = values.Select(e => e.Old).ToList();
                }
                break;

            case 404:
                throw new CollectionNotFoundException();

            default:
                throw new ArangoException();
            }

            return(result);
        }