/// <summary> /// Parses a batch of jobs /// </summary> /// <param name="apiClient">The API client to use to parse the files</param> /// <param name="parseOptions">Any parsing/indexing options</param> /// <param name="rules"> /// The rules that should be applied to whatever files are found prior to parsing. /// This is important to reduce the number of invalid parse API calls and reduce parsing costs. /// </param> /// <param name="directory">The directory containing the files to be parsed</param> /// <param name="searchOption"></param> /// <param name="successCallback">A callback for when a file is parsed successfully</param> /// <param name="partialSuccessCallback">A callback for when some error happened during/after parsing, but there is still usable data in the response</param> /// <param name="errorCallback">A callback for when an error occurred when parsing the file, and there is no usable data</param> /// <param name="generateDocumentIdFn">A callback so you can specify a DocumentId for each file that is parsed</param> /// <exception cref="SovrenInvalidBatchException">Thrown when the directory provided does not meet the <see cref="BatchParsingRules"/></exception> public static async Task ParseJobs( SovrenClient apiClient, ParseOptions parseOptions, BatchParsingRules rules, string directory, SearchOption searchOption, Func <JobBatchSuccessResult, Task> successCallback, Func <JobBatchPartialSuccessResult, Task> partialSuccessCallback, Func <BatchErrorResult, Task> errorCallback, Func <string, string> generateDocumentIdFn = null) { if (apiClient == null) { throw new ArgumentNullException(nameof(apiClient)); } IEnumerable <string> files = GetFiles(rules, directory, searchOption); //process the batch serially, since multi-threading could cause the customer to violate the AUP accidentally foreach (string file in files) { Document doc = new Document(file); string docId = generateDocumentIdFn == null?Guid.NewGuid().ToString() : generateDocumentIdFn(file); try { //set document id if we plan to index these documents if (parseOptions?.IndexingOptions != null) { parseOptions.IndexingOptions.DocumentId = docId; } ParseRequest request = new ParseRequest(doc, parseOptions); ParseJobResponse response = await apiClient.ParseJob(request); if (successCallback != null) { await successCallback(new JobBatchSuccessResult(file, docId, response)); } } catch (SovrenUsableJobException e) { //this happens when something wasn't 100% successful, but there still might be usable data if (partialSuccessCallback != null) { await partialSuccessCallback(new JobBatchPartialSuccessResult(file, docId, e)); } } catch (SovrenException e) { if (errorCallback != null) { //this happens where there is no usable data await errorCallback(new BatchErrorResult(file, docId, e)); } } } }
internal JobBatchSuccessResult(string file, string docId, ParseJobResponse response) : base(file, docId) { Response = response; }
internal SovrenProfessionNormalizationJobException(RestResponse response, ApiResponseInfoLite errorInfo, string transactionId, ParseJobResponse parseResponse) : base(response, errorInfo, transactionId, parseResponse) { }
internal SovrenIndexJobException(RestResponse response, ApiResponseInfoLite errorInfo, string transactionId, ParseJobResponse parseResponse) : base(response, errorInfo, transactionId, parseResponse) { }
internal SovrenUsableJobException(RestResponse response, ApiResponseInfoLite errorInfo, string transactionId, ParseJobResponse parseResponse) : base(null, response, errorInfo, transactionId) { Response = parseResponse; }