/// <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); } }
/// <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); } }
/// <summary> /// Retrieve a document source from the specified index. /// </summary> /// <param name="name">Name of the index.</param> /// <param name="docId">Document ID.</param> /// <param name="token">Cancellation token used to cancel the request.</param> /// <returns>Komodo object.</returns> public async Task <DocumentData> GetSourceDocument(string name, string docId, CancellationToken token = default) { if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } if (String.IsNullOrEmpty(docId)) { throw new ArgumentNullException(nameof(docId)); } try { string url = name + "/" + docId; 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) { return(new DocumentData(resp.ContentType, resp.ContentLength, resp.Data)); } return(null); } catch (TaskCanceledException) { return(null); } catch (OperationCanceledException) { return(null); } }
/// <summary> /// Deletes 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> public async Task DeleteDocument(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; RestRequest req = new RestRequest( _Endpoint + url, HttpMethod.DELETE, _AuthHeaders, "application/json"); req.IgnoreCertificateErrors = AcceptInvalidCertificates; RestResponse resp = await req.SendAsync(token).ConfigureAwait(false); KomodoException e = KomodoException.FromRestResponse(resp); if (e != null) { throw e; } } catch (TaskCanceledException) { return; } catch (OperationCanceledException) { return; } }
/// <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; } }
/// <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>()); } }
/// <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); } }