private async Task <List <ItemsResponseModel> > GetDomains() { var httpRequest = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = new Uri(ApiUrls.GetDomainUri()) }; var httpResponse = await _connectionProvider.HttpClient.SendAsync(httpRequest); var jsonDomainsModel = new JsonDomainResponseModel(); try { httpResponse?.EnsureSuccessStatusCode(); if (httpResponse?.Content != null) { var httpResponseAsString = await httpResponse.Content?.ReadAsStringAsync(); jsonDomainsModel = JsonConvert.DeserializeObject <JsonDomainResponseModel>(httpResponseAsString); } } finally { httpResponse?.Dispose(); } return(jsonDomainsModel.Items); }
private IList <ISearchResult> MapResponseValues(IRestResponse response, JsonDomainResponseModel domainResponseModel) { var termsList = new List <ISearchResult>(); var jObject = JObject.Parse(response.Content); var itemTokens = (JArray)jObject.SelectToken("items"); if (itemTokens != null) { foreach (var item in itemTokens) { var itemId = item.SelectToken("id").ToString(); var domainModel = domainResponseModel.Items.Where(i => i.Id == itemId).FirstOrDefault(); var domain = SetTermDomain(domainModel); SetTermSubdomains(domainModel); _id++; // get language childrens (source + target languages) var languageTokens = item.SelectToken("language").Children().ToList(); if (languageTokens.Any()) { // foreach language token get the terms foreach (JProperty languageToken in languageTokens) { // Latin translations are automatically returned by IATE API response->"la" code // Ignore the "la" translations if (!languageToken.Name.Equals("la")) { var termEntry = languageToken.FirstOrDefault().SelectToken("term_entries").Last; var termValue = termEntry.SelectToken("term_value").ToString(); var termType = GetTermTypeByCode(termEntry.SelectToken("type").ToString()); var langTwoLetters = languageToken.Name; var definition = languageToken.Children().FirstOrDefault() != null ? languageToken.Children().FirstOrDefault().SelectToken("definition") : null; var languageModel = new LanguageModel { Name = new Language(langTwoLetters).DisplayName, Locale = new Language(langTwoLetters).CultureInfo }; var termResult = new SearchResultModel { Text = termValue, Id = _id, Score = 100, Language = languageModel, Definition = definition != null?definition.ToString() : string.Empty, Domain = domain, Subdomain = FormatSubdomain(), TermType = termType }; termsList.Add(termResult); } } } } } return(termsList); }
/// <summary> /// Map the terms values returned from the IATE API response with the SearchResultModel /// </summary> /// <param name="response">IATE API response</param> /// <param name="domainResponseModel">domains response model</param> /// <returns>list of terms</returns> private List <ISearchResult> MapResponseValues(string response, JsonDomainResponseModel domainResponseModel) { var termsList = new List <ISearchResult>(); if (!string.IsNullOrEmpty(response)) { var jObject = JObject.Parse(response); var itemTokens = (JArray)jObject.SelectToken("items"); if (itemTokens != null) { foreach (var item in itemTokens) { _termIndexId++; var itemId = item.SelectToken("id").ToString(); var domainModel = domainResponseModel.Items.FirstOrDefault(i => i.Id == itemId); var domain = SetTermDomain(domainModel); SetTermSubdomains(domainModel); var searchResultItems = new List <SearchResultModel>(); // get language children (source + target languages) var languageTokens = item.SelectToken("language").Children().ToList(); if (languageTokens.Any()) { // foreach language token get the terms foreach (var jToken in languageTokens) { var languageToken = (JProperty)jToken; // Multilingual and Latin translations are automatically returned by IATE API response->"la" code // Ignore the "mul" and "la" translations if (languageToken.Name.Equals("la") || languageToken.Name.Equals("mul")) { continue; } var termEntries = languageToken.FirstOrDefault()?.SelectToken("term_entries"); if (termEntries == null) { continue; } foreach (var termEntry in termEntries) { var termValue = termEntry.SelectToken("term_value").ToString(); var termType = GetTermTypeByCode(termEntry.SelectToken("type").ToString()); var langTwoLetters = languageToken.Name; var definition = languageToken.Children().FirstOrDefault() != null ? languageToken.Children().FirstOrDefault()?.SelectToken("definition") : null; var displayOrder = -1; var evaluation = -1; var metaData = termEntry.SelectToken("metadata"); if (metaData != null) { var displayOrderObject = metaData.SelectToken("display_order"); if (displayOrderObject != null) { displayOrder = displayOrderObject.Value <int>(); } var evaluationObject = metaData.SelectToken("evaluation"); if (evaluationObject != null) { evaluation = evaluationObject.Value <int>(); } } try { var languageModel = new LanguageModel { Name = new Language(langTwoLetters).DisplayName, Locale = new Language(langTwoLetters).CultureInfo }; var termResult = new SearchResultModel { Text = termValue, Id = _termIndexId, ItemId = itemId, Score = 100, Language = languageModel, Definition = definition?.ToString() ?? string.Empty, Domain = domain, Subdomain = FormatSubdomain(), TermType = termType, DisplayOrder = displayOrder, Evaluation = evaluation }; searchResultItems.Add(termResult); } catch (Exception e) { _logger.Error($"{e.Message}\n{e.StackTrace}"); } } } } termsList.AddRange(searchResultItems); } } } return(termsList); }