public async Task <KeywordSearchModel> BuildAsync() { if (string.IsNullOrWhiteSpace(_term)) { return(null); } var apiUri = _uriHelper.GetFullUri(Endpoint, true); var url = apiUri + "/" + _term; var response = await _httpClient.GetAsync(url, _tokenResponse, new CancellationToken(false)); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); var model = new KeywordSearchModel { Term = _term, Keywords = JsonConvert.DeserializeObject <List <KeywordModel> >(result) }; return(model); } return(null); }
protected async Task <ITokenResponse> GetAccessToken(bool tryRefreshIfExpired) { if (_memoryCache.TryGetValue(TokenResponseCacheKey, out ITokenResponse tokenResponse) && TryGetJwtSecurityToken(tokenResponse, out var jwtSecurityToken)) { if (CheckIfJwtSecurityTokenIsValid(jwtSecurityToken)) { return(tokenResponse); } if (tryRefreshIfExpired) { var refreshTokenUrl = _uriHelper.GetFullUri(_tokenOptions.BaseUrl, _tokenOptions.TokenRefreshEndpoint).ToString(); tokenResponse = await CustomHttpClientWrapper.Instance.ResfreshTokenAsync(refreshTokenUrl, tokenResponse.RefreshToken); } } //We didnt succeed to refresh if (tokenResponse == null) { var tokenUrl = _uriHelper.GetFullUri(_tokenOptions.BaseUrl, _tokenOptions.TokenEndpoint).ToString(); tokenResponse = await CustomHttpClientWrapper.Instance.GetAccessTokenAsync(tokenUrl, _tokenOptions.AccessId, _tokenOptions.AccessSecret); } if (tokenResponse == null) { return(null); } var cacheEntryOptions = new MemoryCacheEntryOptions() // Keep in cache for this time, reset time if accessed. .SetSlidingExpiration(TimeSpan.FromMinutes(30)); // Save data in cache. _memoryCache.Set(TokenResponseCacheKey, tokenResponse, cacheEntryOptions); return(tokenResponse); }
public async Task<SkillSearchModel> BuildAsync() { if (string.IsNullOrWhiteSpace(_term)) return null; var content = new {term = _term}; var url = _uriHelper.GetFullUri(Endpoint, true); var response = await _httpClient.PostAsync(url.ToString(), _tokenResponse, content); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); var serachSkillResultModel = JsonConvert.DeserializeObject<SearchSkillResultModel>(result); var model = new SkillSearchModel() { Term = _term, CompanyUsers =serachSkillResultModel.Hits?.ToList() }; return model; } return null; }