private AnnotationResult ProccessResponse(ResponseBody response) { var result = new AnnotationResult(); if (response.Error != null) { result.State = AnnotationStates.Failed; } else { result.ServiceId = response.SessionId; result.State = AnnotationStates.Ok; result.Annotations = response.Matches != null?response.Matches.Select(x => new Annotation() { Uri = x }).ToList() : new List <Annotation>(); } return(result); }
public async Task <AnnotationResult> GetAnnotations(ServiceInfo serviceInfo) { if (!initialized) { return(null); } if (string.IsNullOrEmpty(serviceConnectionConfiguration.Uri)) { return(null); } if (serviceInfo == null) { return(null); } WebProxy proxy = null; if ((proxySettings != null) && (!string.IsNullOrEmpty(proxySettings.Address))) { string proxyUri = string.Format("{0}:{1}", proxySettings.Address, proxySettings.Port); proxy = new WebProxy(proxyUri); if (!string.IsNullOrEmpty(proxySettings.UserName) && !string.IsNullOrEmpty(proxySettings.Password)) { proxy.Credentials = new NetworkCredential(proxySettings.UserName, proxySettings.Password); } } using (HttpClientHandler httpClientHandler = new HttpClientHandler() { Proxy = proxy }) { if (serviceConnectionConfiguration.IgnoreCertificate) { httpClientHandler.ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true; } using (var client = new HttpClient(httpClientHandler) { BaseAddress = new Uri(serviceConnectionConfiguration.Uri) }) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaContentType)); var result = new AnnotationResult() { ServiceId = serviceInfo.Id }; var requestBody = new RequestBody() { SessionId = serviceInfo.Id, Lang = serviceInfo.LanguageCode, Limit = 100, Timeout = 3000, Content = ProcessRequest(serviceInfo), Properties = new List <string>() { "rdfs:label", "skos:narrower", "skos:broader", "skos:related", "skos:inScheme", "rdf:type" } }; if (!requestBody.Content.Any()) { result.State = AnnotationStates.EmptyInputReceived; return(result); } try { HttpContent content = new StringContent( JsonConvert.SerializeObject(requestBody), Encoding.UTF8, mediaContentType); var response = await client.PostAsync("", content); if (response.StatusCode != HttpStatusCode.OK) { logger.LogError($"Annotation failed: {response.Content?.ReadAsStringAsync()?.Result ?? string.Empty}"); Console.WriteLine("Annotation failed."); result.State = AnnotationStates.Failed; } else { string text = await response.Content.ReadAsStringAsync(); var responseBody = JsonConvert.DeserializeObject <ResponseBody>(text); result = ProccessResponse(responseBody); } } catch (Exception e) { Console.WriteLine("Annotation failed."); logger.LogError($"Annotation failed: {e.Message}", e); result.State = AnnotationStates.Failed; } return(result); } } }