public Dictionary <int, RegulationResource> MapRegulationResources(JsonElement regulationListFromTextService) { var dictionary = new Dictionary <int, RegulationResource>(); using var document = JsonDocument.Parse(regulationListFromTextService.ToString()); var arrayEnumerator = document.RootElement.EnumerateArray(); var counter = 1; while (arrayEnumerator.MoveNext()) { var i = arrayEnumerator.Current; i.TryGetProperty("referenceId", out var referenceId); i.TryGetProperty("title", out var title); i.TryGetProperty("url", out var url); i.TryGetProperty("language", out var language); var item = new RegulationResource { ReferenceId = referenceId.GetString(), Title = title.GetString(), Url = new System.Uri(url.GetString()), Language = language.GetString() }; dictionary.Add(counter, item); counter++; } return(dictionary); }
public void TestReadFullResponse() { var twilioRestClient = Substitute.For <ITwilioRestClient>(); twilioRestClient.AccountSid.Returns("ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); twilioRestClient.Request(Arg.Any <Request>()) .Returns(new Response( System.Net.HttpStatusCode.OK, "{\"results\": [{\"sid\": \"RNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"friendly_name\": \"Australia: Local - Individual\",\"iso_country\": \"AU\",\"number_type\": \"local\",\"end_user_type\": \"individual\",\"requirements\": {\"end_user\": [{\"name\": \"Individual\",\"type\": \"individual\",\"url\": \"https://numbers.twilio.com/v2/RegulatoryCompliance/Regulations/individual\",\"fields\": [\"first_name\",\"last_name\"]}],\"supporting_document\": [[{\"name\": \"Address\",\"type\": \"document\",\"description\": \"The physical location of the individual or business. Must be within locality or region covered by the phone numbers prefix; a PO Box is not acceptable where a local address is required.\",\"accepted_documents\": [{\"name\": \"Address Validation\",\"type\": \"address\",\"url\": \"https://numbers.twilio.com/v2/RegulatoryCompliance/DocumentTypes/address\",\"fields\": []}]}]]},\"url\": \"https://numbers.twilio.com/v2/RegulatoryCompliance/Regulations/RNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"}],\"meta\": {\"page\": 0,\"page_size\": 50,\"first_page_url\": \"https://numbers.twilio.com/v2/RegulatoryCompliance/Regulations?PageSize=50&Page=0\",\"previous_page_url\": null,\"url\": \"https://numbers.twilio.com/v2/RegulatoryCompliance/Regulations?PageSize=50&Page=0\",\"next_page_url\": null,\"key\": \"results\"}}" )); var response = RegulationResource.Read(client: twilioRestClient); Assert.NotNull(response); }
public void TestReadEmptyResponse() { var twilioRestClient = Substitute.For <ITwilioRestClient>(); twilioRestClient.AccountSid.Returns("ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); twilioRestClient.Request(Arg.Any <Request>()) .Returns(new Response( System.Net.HttpStatusCode.OK, "{\"results\": [],\"meta\": {\"page\": 0,\"page_size\": 50,\"first_page_url\": \"https://numbers.twilio.com/v2/RegulatoryCompliance/Regulations?IsoCountry=US&EndUserType=business&NumberType=mobile&PageSize=50&Page=0\",\"previous_page_url\": null,\"url\": \"https://numbers.twilio.com/v2/RegulatoryCompliance/Regulations?IsoCountry=US&EndUserType=business&NumberType=mobile&PageSize=50&Page=0\",\"next_page_url\": null,\"key\": \"results\"}}" )); var response = RegulationResource.Read(client: twilioRestClient); Assert.NotNull(response); }
public void TestFetchRequest() { var twilioRestClient = Substitute.For <ITwilioRestClient>(); var request = new Request( HttpMethod.Get, Twilio.Rest.Domain.Numbers, "/v2/RegulatoryCompliance/Regulations/RNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "" ); twilioRestClient.Request(request).Throws(new ApiException("Server Error, no content")); try { RegulationResource.Fetch("RNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", client: twilioRestClient); Assert.Fail("Expected TwilioException to be thrown for 500"); } catch (ApiException) {} twilioRestClient.Received().Request(request); }
private async ValueTask NlpBackgroundTask( Uri requestedTextServiceRegulationIri, CancellationToken stoppingToken) { if (stoppingToken.IsCancellationRequested) { _logger.LogInformation($"{Environment.NewLine}Stopped because of CancellationToken.{Environment.NewLine}"); return; } _logger.LogInformation($"{Environment.NewLine}Queued Background Task (starting): {requestedTextServiceRegulationIri}{Environment.NewLine}"); // // Text Service // var regulationResource = new RegulationResource { Url = requestedTextServiceRegulationIri }; _logger.LogInformation($"{Environment.NewLine}Asking Text Service for regulation {regulationResource.RegulationYear}-{regulationResource.RegulationMonth}-{regulationResource.RegulationDay}-{regulationResource.RegulationNumber}.{Environment.NewLine}"); var regulationFromTextService = await _textServiceApi.GetRegulation( _textServiceSettings.ApiBaseUrl, Convert.ToInt32(regulationResource.RegulationYear), Convert.ToInt32(regulationResource.RegulationMonth), Convert.ToInt32(regulationResource.RegulationDay), Convert.ToInt32(regulationResource.RegulationNumber)); var chapterList = _textServiceHelper.SplitRegulationResponseIntoChapterList(regulationFromTextService); _logger.LogInformation($"{Environment.NewLine}{chapterList.Count} chapters loaded successfully.{Environment.NewLine}"); // // NLP Service // // Load NLP options if (_nlpServiceSettings.RunAsTest) { // Just load test data NlpResourceDictionary = _nlpServiceHelper.GetNlpResourceTestDictionary(); } else // Send request to NLP Service API { var nlpResourceListFromNlpService = await _nlpServiceApi.GetNlpResourceList( _nlpServiceSettings.ApiBaseUrl); NlpResourceDictionary = _nlpServiceHelper.MapNlpResources( _nlpServiceSettings.ApiBaseUrl, nlpResourceListFromNlpService); } foreach (var nlpResourceDictionaryItem in NlpResourceDictionary) { var selectedNlpResourceDictionary = nlpResourceDictionaryItem.Value; _logger.LogInformation($"{Environment.NewLine}Asking NLP Service to identify information about {selectedNlpResourceDictionary.Title} ({selectedNlpResourceDictionary.Language}) in regulation {regulationResource.RegulationYear}-{regulationResource.RegulationMonth}-{regulationResource.RegulationDay}-{regulationResource.RegulationNumber}.{Environment.NewLine}"); for (var i = 0; i < chapterList.Count; i++) { var requestNumber = i + 1; _logger.LogInformation($"{Environment.NewLine}Processing chapter {requestNumber} of {chapterList.Count}:{Environment.NewLine}"); JsonElement identifiedInformationInChapterTextData; if (_nlpServiceSettings.RunAsTest) { identifiedInformationInChapterTextData = _nlpServiceHelper.GetTestDataForIdentifyInformationInChapterTextData(); } else // Send request to NLP Service API { identifiedInformationInChapterTextData = await _nlpServiceApi.IdentifyInformationInChapterTextData( chapterList[i], selectedNlpResourceDictionary.Url); } var itemCountOfNlpServiceResponse = _nlpServiceHelper.CountItemsInNlpServiceApiResponse( identifiedInformationInChapterTextData); if (itemCountOfNlpServiceResponse > 0) { _logger.LogInformation($"{Environment.NewLine}{itemCountOfNlpServiceResponse} detections in this chapter.{Environment.NewLine}"); IdentifiedInformationInChapterTextDataList.Add(identifiedInformationInChapterTextData); } else { _logger.LogInformation($"{Environment.NewLine}No detections in this chapter.{Environment.NewLine}"); } } } // // Transformer Service // if (_transformerServiceSettings.RunAsTest) { _logger.LogInformation($"{Environment.NewLine}RUN AS TEST -> Asking Transformer Service to transform information into knowledge.{Environment.NewLine}"); var transformerCounter = 0; foreach (var identifiedInformationInChapterTextData in IdentifiedInformationInChapterTextDataList) { var transformedRdfKnowledge = _transformerServiceHelper.GetTestDataForTransformNlpInformationToRdfKnowledge( identifiedInformationInChapterTextData); TransformedRdfKnowledgeList.Add(transformedRdfKnowledge); transformerCounter++; _logger.LogInformation($"{Environment.NewLine}Transformation {transformerCounter}. Success!{Environment.NewLine}"); } } else // send request to Transformer Service API { _logger.LogInformation($"{Environment.NewLine}Asking Transformer Service to transform information into knowledge.{Environment.NewLine}"); var transformerCounter = 0; foreach (var identifiedInformationInChapterTextData in IdentifiedInformationInChapterTextDataList) { var transformedRdfKnowledge = await _transformerServiceApi.TransformNlpInformationToRdfKnowledge( _transformerServiceSettings.ApiBaseUrl, identifiedInformationInChapterTextData); TransformedRdfKnowledgeList.Add(transformedRdfKnowledge); transformerCounter++; _logger.LogInformation($"{Environment.NewLine}Transformation {transformerCounter}. Success!{Environment.NewLine}"); } } // // Authentication Service // try { _logger.LogInformation($"{Environment.NewLine}Asking Authentication Service for access token.{Environment.NewLine}"); TopBraidEdgOAuthAccessToken = await _authenticationApi.GetAuthenticationToken( _authenticationServiceSettings.ApiBaseUrl, _authenticationServiceSettings.ClientId, _authenticationServiceSettings.ClientSecret, _authenticationServiceSettings.Scope); _logger.LogInformation($"{Environment.NewLine}Successfully loaded access token.{Environment.NewLine}"); } catch (Exception e) { _logger.LogInformation($"{Environment.NewLine}Oh no.. :-O Something went wrong. Here is an error message:{Environment.NewLine}"); _logger.LogInformation(e.ToString()); _logger.LogInformation($"{Environment.NewLine}Service Controller application ended.{Environment.NewLine}"); return; } // // Knowledge Service // foreach (var transformedRdfKnowledge in TransformedRdfKnowledgeList) { _logger.LogInformation($"{Environment.NewLine}Asking Knowledge Service to construct SPARQL INSERT query.{Environment.NewLine}"); var topBraidEdgSparqlInsertBuilder = new Entities.KnowledgeService.TopBraidEdgSparqlInsertBuilder( _knowledgeServiceSettings.TopBraidEdgOntologyId, _knowledgeServiceSettings.TopBraidEdgWorkflowId, _knowledgeServiceSettings.TopBraidEdgUserId, transformedRdfKnowledge ); _logger.LogInformation($"{Environment.NewLine}Successfully parsed {topBraidEdgSparqlInsertBuilder.Graph.Nodes.Count()} triples from Transformer Service.{Environment.NewLine}"); var sparqlInsertQueryString = topBraidEdgSparqlInsertBuilder.BuildSparqlInsertQueryString(); _logger.LogInformation($"{Environment.NewLine}Successfully constructed SPARQL INSERT query.{Environment.NewLine}"); var topBraidEdgGraphUrn = topBraidEdgSparqlInsertBuilder.BuildTopBraidEdgGraphUrn(); _logger.LogInformation($"{Environment.NewLine}Loading knowledge into TopBraid EDG graph: {topBraidEdgGraphUrn}{Environment.NewLine}"); try { await _topBraidEdgApi.TestInsert( _knowledgeServiceSettings.ApiBaseUrl, TopBraidEdgOAuthAccessToken, sparqlInsertQueryString, topBraidEdgGraphUrn); } catch (Exception e) { _logger.LogInformation($"{Environment.NewLine}Oh no.. :-O Something went wrong. Here is an error message:{Environment.NewLine}"); _logger.LogInformation(e.ToString()); _logger.LogInformation($"{Environment.NewLine}Service Controller application ended.{Environment.NewLine}"); return; } _logger.LogInformation($"{Environment.NewLine}Successfully loaded knowledge.{Environment.NewLine}"); } _logger.LogInformation($"{Environment.NewLine}Queued Background Task (completed): {requestedTextServiceRegulationIri}{Environment.NewLine}"); }