Esempio n. 1
0
        /// <summary>
        /// Enumerate source documents in the specified index.
        /// </summary>
        /// <param name="indexName">Name of the index.</param>
        /// <param name="query">Enumeration query.</param>
        /// <param name="token">Cancellation token used to cancel the request.</param>
        /// <returns>Enumeration result.</returns>
        public async Task <EnumerationResult> Enumerate(string indexName, EnumerationQuery query, CancellationToken token = default)
        {
            if (String.IsNullOrEmpty(indexName))
            {
                throw new ArgumentNullException(nameof(indexName));
            }
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            try
            {
                string url = indexName + "?enumerate";

                RestRequest req = new RestRequest(
                    _Endpoint + url,
                    HttpMethod.PUT,
                    _AuthHeaders,
                    "application/json");

                req.IgnoreCertificateErrors = AcceptInvalidCertificates;

                RestResponse resp = await req.SendAsync(KomodoCommon.SerializeJson(query, true), token).ConfigureAwait(false);

                KomodoException e = KomodoException.FromRestResponse(resp);
                if (e != null)
                {
                    throw e;
                }

                if (resp.Data != null && resp.ContentLength > 0)
                {
                    byte[] respData = KomodoCommon.StreamToBytes(resp.Data);
                    return(KomodoCommon.DeserializeJson <EnumerationResult>(respData));
                }

                return(null);
            }
            catch (TaskCanceledException)
            {
                return(null);
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieve metadata for a document from the specified index.
        /// </summary>
        /// <param name="indexName">Name of the index.</param>
        /// <param name="docId">Document ID.</param>
        /// <param name="token">Cancellation token used to cancel the request.</param>
        /// <returns>DocumentMetadata.</returns>
        public async Task <DocumentMetadata> GetDocumentMetadata(string indexName, string docId, CancellationToken token = default)
        {
            if (String.IsNullOrEmpty(indexName))
            {
                throw new ArgumentNullException(nameof(indexName));
            }
            if (String.IsNullOrEmpty(docId))
            {
                throw new ArgumentNullException(nameof(docId));
            }

            try
            {
                string url = indexName + "/" + docId + "?parsed&pretty";

                RestRequest req = new RestRequest(
                    _Endpoint + url,
                    HttpMethod.GET,
                    _AuthHeaders,
                    "application/json");

                req.IgnoreCertificateErrors = AcceptInvalidCertificates;

                RestResponse resp = await req.SendAsync(token).ConfigureAwait(false);

                KomodoException e = KomodoException.FromRestResponse(resp);
                if (e != null)
                {
                    throw e;
                }

                if (resp.Data != null && resp.ContentLength > 0)
                {
                    byte[] respData = KomodoCommon.StreamToBytes(resp.Data);
                    return(KomodoCommon.DeserializeJson <DocumentMetadata>(respData));
                }

                return(null);
            }
            catch (TaskCanceledException)
            {
                return(null);
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Creates an index.
        /// </summary>
        /// <param name="index">Index.</param>
        /// <param name="token">Cancellation token used to cancel the request.</param>
        public async Task CreateIndex(Index index, CancellationToken token = default)
        {
            if (index == null)
            {
                throw new ArgumentNullException(nameof(index));
            }
            if (String.IsNullOrEmpty(index.Name))
            {
                throw new ArgumentNullException(nameof(index.Name));
            }

            try
            {
                string url = "indices";

                RestRequest req = new RestRequest(
                    _Endpoint + url,
                    HttpMethod.POST,
                    _AuthHeaders,
                    "application/json");

                req.IgnoreCertificateErrors = AcceptInvalidCertificates;

                RestResponse resp = await req.SendAsync(KomodoCommon.SerializeJson(index, true), token).ConfigureAwait(false);

                KomodoException e = KomodoException.FromRestResponse(resp);
                if (e != null)
                {
                    throw e;
                }
            }
            catch (TaskCanceledException)
            {
                return;
            }
            catch (OperationCanceledException)
            {
                return;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Return the list of indices available on the server.
        /// </summary>
        /// <param name="token">Cancellation token used to cancel the request.</param>
        /// <returns>List of index names.</returns>
        public async Task <List <string> > GetIndices(CancellationToken token = default)
        {
            try
            {
                RestRequest req = new RestRequest(
                    _Endpoint + "indices",
                    HttpMethod.GET,
                    _AuthHeaders,
                    "application/json");

                req.IgnoreCertificateErrors = AcceptInvalidCertificates;

                RestResponse resp = await req.SendAsync(token).ConfigureAwait(false);

                KomodoException e = KomodoException.FromRestResponse(resp);
                if (e != null)
                {
                    throw e;
                }

                if (resp.Data != null && resp.ContentLength > 0)
                {
                    byte[] respData = KomodoCommon.StreamToBytes(resp.Data);
                    return(KomodoCommon.DeserializeJson <List <string> >(respData));
                }

                return(new List <string>());
            }
            catch (TaskCanceledException)
            {
                return(new List <string>());
            }
            catch (OperationCanceledException)
            {
                return(new List <string>());
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Store a document in the specified index without parsing and indexing.
        /// </summary>
        /// <param name="indexName">Name of the index.</param>
        /// <param name="docGuid">Document GUID.</param>
        /// <param name="sourceUrl">Source URL for the data (overrides 'data' parameter).</param>
        /// <param name="title">Title for the document.</param>
        /// <param name="tags">Document tags.</param>
        /// <param name="docType">Type of document.</param>
        /// <param name="data">Data from the document.</param>
        /// <param name="token">Cancellation token used to cancel the request.</param>
        /// <returns>Index result.</returns>
        public async Task <IndexResult> StoreDocument(string indexName, string docGuid, string sourceUrl, string title, List <string> tags, DocType docType, byte[] data, CancellationToken token = default)
        {
            if (String.IsNullOrEmpty(indexName))
            {
                throw new ArgumentNullException(nameof(indexName));
            }
            if (String.IsNullOrEmpty(sourceUrl) &&
                (data == null || data.Length < 1))
            {
                throw new ArgumentException("Either sourceUrl or data must be populated.");
            }

            try
            {
                string docTypeStr = DocTypeString(docType);

                string url = indexName + "?type=" + docTypeStr + "&bypass";
                if (!String.IsNullOrEmpty(docGuid))
                {
                    url += "/" + docGuid;
                }
                if (!String.IsNullOrEmpty(sourceUrl))
                {
                    url += "&url=" + WebUtility.UrlEncode(sourceUrl);
                }
                if (!String.IsNullOrEmpty(title))
                {
                    url += "&title=" + WebUtility.UrlEncode(title);
                }

                if (tags != null && tags.Count > 0)
                {
                    url += "&tags=" + WebUtility.UrlEncode(KomodoCommon.StringListToCsv(tags));
                }

                RestRequest req = new RestRequest(
                    _Endpoint + url,
                    HttpMethod.POST,
                    _AuthHeaders,
                    "application/json");

                req.IgnoreCertificateErrors = AcceptInvalidCertificates;

                RestResponse resp = await req.SendAsync(data, token).ConfigureAwait(false);

                KomodoException e = KomodoException.FromRestResponse(resp);
                if (e != null)
                {
                    throw e;
                }

                if (resp.Data != null && resp.ContentLength > 0)
                {
                    byte[] respData = KomodoCommon.StreamToBytes(resp.Data);
                    return(KomodoCommon.DeserializeJson <IndexResult>(respData));
                }

                return(null);
            }
            catch (TaskCanceledException)
            {
                return(null);
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
        }
Esempio n. 6
0
        internal static KomodoException FromRestResponse(RestResponse resp)
        {
            KomodoException e = new KomodoException();

            if (resp == null)
            {
                e.StatusCode   = 0;
                e.ResponseData = null;
                e.Type         = ExceptionType.CannotConnect;
                return(e);
            }

            if (resp.StatusCode >= 500)
            {
                e.StatusCode = resp.StatusCode;
                if (resp.ContentLength > 0)
                {
                    e.ResponseData = KomodoCommon.StreamToBytes(resp.Data);
                }
                else
                {
                    e.ResponseData = null;
                }
                e.Type = ExceptionType.InternalServerError;
                return(e);
            }

            if (resp.StatusCode == 413)
            {
                e.StatusCode = resp.StatusCode;
                if (resp.ContentLength > 0)
                {
                    e.ResponseData = KomodoCommon.StreamToBytes(resp.Data);
                }
                else
                {
                    e.ResponseData = null;
                }
                e.Type = ExceptionType.PayloadTooLarge;
                return(e);
            }

            if (resp.StatusCode == 409)
            {
                e.StatusCode = resp.StatusCode;
                if (resp.ContentLength > 0)
                {
                    e.ResponseData = KomodoCommon.StreamToBytes(resp.Data);
                }
                else
                {
                    e.ResponseData = null;
                }
                e.Type = ExceptionType.Conflict;
                return(e);
            }

            if (resp.StatusCode == 404)
            {
                e.StatusCode = resp.StatusCode;
                if (resp.ContentLength > 0)
                {
                    e.ResponseData = KomodoCommon.StreamToBytes(resp.Data);
                }
                else
                {
                    e.ResponseData = null;
                }
                e.Type = ExceptionType.NotFound;
                return(e);
            }

            if (resp.StatusCode == 401)
            {
                e.StatusCode = resp.StatusCode;
                if (resp.ContentLength > 0)
                {
                    e.ResponseData = KomodoCommon.StreamToBytes(resp.Data);
                }
                else
                {
                    e.ResponseData = null;
                }
                e.Type = ExceptionType.Unauthorized;
                return(e);
            }

            if (resp.StatusCode == 400)
            {
                e.StatusCode = resp.StatusCode;
                if (resp.ContentLength > 0)
                {
                    e.ResponseData = KomodoCommon.StreamToBytes(resp.Data);
                }
                else
                {
                    e.ResponseData = null;
                }
                e.Type = ExceptionType.BadRequest;
                return(e);
            }

            return(null);
        }