Checking content
public CongreeCheckresult Check(string text, string ruleSet) { text = text.Replace("\"", "'"); var client = new RestClient(this.serverUrl + this.documentCheckhUrl + ruleSet); var request = new RestRequest(Method.POST); request.AddHeader("content-type", "application/json"); request.AddHeader("authorization", "Bearer " + this.token); request.AddParameter("application/json", "{\n\t\"Xml\": \"" + text + "\",\n\t\"options\": {\n\t\t\"includeReporting\": true,\n\t\t\"extractTermCandidates\": true,\n\t\t\"includeContextMapping\": false\n\t}\n}", ParameterType.RequestBody); IRestResponse response = client.Execute(request); CongreeCheckresult checkResult = JsonConvert.DeserializeObject<CongreeCheckresult>(response.Content); return checkResult; }
The entire class looks like shown below:
using Newtonsoft.Json; using RestSharp; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Congree { public class CongreeHttpClient { private string authUrl = "/congreeidentityserver/connect/token"; private string documentCheckhUrl = "/congree/api/Linguistic/v1/Check?ruleSet="; private string documentSettingsUrl = "/congree/api/Settings/v1/Document/"; private string serverUrl = ""; private string token = ""; public CongreeHttpClient(string serverUrl, string username, string password) { this.serverUrl = serverUrl; this.token = GetToken(username, password); } public string GetToken(string username, string password) { var client = new RestClient(this.serverUrl + this.authUrl); var request = new RestRequest(Method.POST); request.AddHeader("content-type", "application/x-www-form-urlencoded"); request.AddHeader("authorization", "Bearer undefined"); request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&username=" + username + "&password=" + password + "&scope=webApi&client_id=CongreeWebAPI", ParameterType.RequestBody); IRestResponse response = client.Execute(request); AccessToken accessToken = new AccessToken(); accessToken = JsonConvert.DeserializeObject<AccessToken>(response.Content); this.token = accessToken.access_token; return this.token; } public CongreeCheckresult Check(string text, string ruleSet) { var client = new RestClient(this.serverUrl + this.documentCheckhUrl + ruleSet); var request = new RestRequest(Method.POST); request.AddHeader("content-type", "application/json"); request.AddHeader("authorization", "Bearer " + this.token); CongreeCheckRequest checkRequest = new CongreeCheckRequest(); checkRequest.Xml = text; JsonSerializerSettings settings = new JsonSerializerSettings(); settings.TypeNameHandling = TypeNameHandling.Auto; string serializedRequestJSON = JsonConvert.SerializeObject(checkRequest, settings); request.AddParameter("application/json", serializedRequestJSON, ParameterType.RequestBody); IRestResponse response = client.Execute(request); CongreeCheckresult checkResult = JsonConvert.DeserializeObject<CongreeCheckresult>(response.Content); return checkResult; } public string[] GetDocumentRules() { string[] rules; var client = new RestClient(this.serverUrl + this.documentSettingsUrl); var request = new RestRequest(Method.GET); request.AddHeader("content-type", "application/json"); request.AddHeader("authorization", "Bearer " + this.token); IRestResponse response = client.Execute(request); rules = JsonConvert.DeserializeObject<string[]>(response.Content); return rules; } } }
The code above uses the class CongreeCheckRequest to allow for a creation of a JSON compliant object that is being passed to Congree's REST API
using System; using System.Collections.Generic; using System.Text; namespace Congree { class CongreeCheckRequest { // Input for the linguistic checker.Must be valid XML. public string Xml { get; set; } // Check options public CongreeCheckOptions options { get; set; } public CongreeCheckRequest() { this.options = new CongreeCheckOptions(); } } }
In this class we are using another helper class to configure the different check and response options.
using System; using System.Collections.Generic; using System.Text; namespace Congree { public class CongreeCheckOptions { // Specifies whether valid terms should be included in result public bool includeValidTerms { get; set; } // Specifies whether linguistic report should be generated , public bool includeReporting { get; set; } // Specifies whether context bounds(<?cngr-ctx-b { ContextId}?> ... <? cngr-ctx-e {ContextId}?> blocks) should be included in output XML, public bool includeContextMapping { get; set; } // Specifies whether to perform terminology candidates extraction public bool extractTermCandidates { get; set; } public CongreeCheckOptions() { this.includeValidTerms = true; this.includeReporting = true; this.includeContextMapping = true; this.extractTermCandidates = true; } } }
The Check function uses the token obtain during the authentication. The API returns a JSON object that is being deserialized. The class CongreeCheckresult is shown below.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CongreeProofReader.Congree { public class CongreeCheckresult { public string ResultXml { get; set; } public IList<Error> Errors { get; set; } public IList<TermCandidate> TermCandidates { get; set; } public Reporting Reporting { get; set; } } public class Error { public string Id { get; set; } public string ContextId { get; set; } public string Type { get; set; } public IList<ErrorDescription> Descriptions { get; set; } public IList<Proposal> Proposals { get; set; } } public class ErrorDescription { public string Code { get; set; } public Description Description { get; set; } public IList<ErrorExplanation> Explanation { get; set; } public IList<ErrorExplanationRewrite> ExplanationRewrite { get; set; } } public class Description { public string Header { get; set; } public string Instruction { get; set; } public string Explanation { get; set; } } public class ErrorExplanation { public string Type { get; set; } public string Text { get; set; } } public class ErrorExplanationRewrite { public string Type { get; set; } public string Text { get; set; } } public class Proposal { public string Text { get; set; } public string AdditionalInfo { get; set; } public string BaseTermForm { get; set; } public string ConceptId { get; set; } } public class TermCandidate { public string Id { get; set; } public int ContextId { get; set; } public string Text { get; set; } public string Context { get; set; } public bool IsExists { get; set; } } public class Reporting { public string TotalCheckedWords { get; set; } public string ReleaseLevel { get; set; } public string RelativeReleaseLevel { get; set; } public string SafeReleaseLevel { get; set; } public string SafeReleaseLevelTitle { get; set; } public string AcceptableReleaseLevel { get; set; } public string UnsafeReleaseLevel { get; set; } public string UnsafeReleaseLevelTitle { get; set; } public IList<ReportingByTypes> ReportingByTypes { get; set; } } public class ReportingByTypes { public string Type { get; set; } public string ErrorCount { get; set; } public string Severity { get; set; } public string RelativeSeverity { get; set; } public string Terminology { get; set; } public string Spelling { get; set; } public string Grammar { get; set; } public string Style { get; set; } public string Abbreviation { get; set; } } }