public async Task <KeyValuePair <bool, string> > ReadString(string sourceHttpUrl) { Uri sourceUri = new Uri(sourceHttpUrl); _logger.LogInformation("[ReadString] HttpClientHelper > " + sourceUri.Host + " ~ " + sourceHttpUrl); // allow whitelist and https only if (!_allowedDomains.Contains(sourceUri.Host) || sourceUri.Scheme != "https") { return (new KeyValuePair <bool, string>(false, string.Empty)); } try { using (HttpResponseMessage response = await _httpProvider.GetAsync(sourceHttpUrl)) using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync()) { var reader = new StreamReader(streamToReadFrom, Encoding.UTF8); var result = await reader.ReadToEndAsync(); return(new KeyValuePair <bool, string>(response.StatusCode == HttpStatusCode.OK, result)); } } catch (HttpRequestException exception) { return(new KeyValuePair <bool, string>(false, exception.Message)); } }
public async Task <ApiResponse <UserActivityDto> > GetUserActivities(ReportGeneratorRequest request) { try { var nw = new NameValueCollection() { { nameof(ReportGeneratorRequest.ProjectId), request.ProjectId.ToString() }, { nameof(ReportGeneratorRequest.UserId), request.UserId.ToString() }, }; _provider.SetBearerAuthorization(await _httpContextAccessor.HttpContext.GetTokenAsync("access_token")); var response = await _provider.GetAsync(_settings.Url + Routes.UserActivities + nw.ToQueryString()); response.EnsureSuccessStatusCode(); var activities = await response.Content.ReadAsAsync <ApiResponse <UserActivityDto> >(); return(activities); } catch (Exception e) { _logger.LogError(e, "An error occured while fetching user {0} activities {1}", request.UserId, request.ProjectId); return(ApiResponse <UserActivityDto> .InternalError()); } }
public async Task <ExchangeRateResponseData> GetValutesAsync() { string exchangeRateUrl = _configuration["ExchangeRateUrl"]; var exchangeRate = await _provider.GetAsync(exchangeRateUrl); var exchangeRateResponseData = JsonConvert.DeserializeObject <ExchangeRateResponseData>(exchangeRate); return(exchangeRateResponseData); }
public async Task <List <Category> > LoadCategs() { var response = await _httpProvider.GetAsync("data/app_categs.php?version=1.8"); var responseData = await response.Content.ReadAsStringAsync(); var categs = JsonConvert.DeserializeObject <List <Category> >(responseData); return(categs); }
public async Task Should_GetScoreboardAsync_ForValidInput() { //arrange int leagueId = 555555; int year = 2018; IHttpProvider httpProvider = A.Fake <IHttpProvider>(); IUrlConfigurationProvider urlConfigurationProvider = A.Fake <IUrlConfigurationProvider>(); IFantasyFootballService fantasyFootballService = new EspnApiFantasyFootballService(httpProvider, urlConfigurationProvider); //act LeagueScoreboard leagueScoreboard = await fantasyFootballService.GetScoreboardAsync(leagueId, year); //assert A.CallTo(() => urlConfigurationProvider.GetScoreboardEndpoint(leagueId, year)).MustHaveHappened(); A.CallTo(() => httpProvider.GetAsync <LeagueScoreboard>(null)).WithAnyArguments().MustHaveHappened(); }
public async Task Should_GetRecentActivityAsync_ForValidInput() { //arrange int leagueId = 555555; int year = 2018; IHttpProvider httpProvider = A.Fake <IHttpProvider>(); IUrlConfigurationProvider urlConfigurationProvider = A.Fake <IUrlConfigurationProvider>(); INflPlayerService nflPlayerService = A.Fake <INflPlayerService>(); IFantasyFootballService fantasyFootballService = new EspnApiFantasyFootballService(httpProvider, urlConfigurationProvider); //act RecentActivity recentActivity = await fantasyFootballService.GetRecentActivity(leagueId, year); //assert A.CallTo(() => urlConfigurationProvider.GetRecentActivityEndpoint(leagueId, year)).MustHaveHappened(); A.CallTo(() => httpProvider.GetAsync <RecentActivity>(null)).WithAnyArguments().MustHaveHappened(); }
public async Task <Quote> GetQuote() { if (!_marketDataProvider.Market.IsOpen) { return(null); } var pairs = _marketDataProvider.Pairs; var query = pairs.Aggregate("select * from yahoo.finance.xchange where pair in ('", (current, pair) => current + (pair.Symbol + ",")); query = query.Substring(0, query.Length - 1); query += "')"; var url = BaseUrl + WebUtility.UrlEncode(query) + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; return(await _httpProvider.GetAsync(url, _formatter)); }
private async Task <IReadOnlyList <TorrentComment> > GetCommentsAsync(int torrentId) { var commentsUrl = $"/comments.php?torrentid={torrentId}"; var comments = await _httpProvider.GetAsync <TorrentComment>(commentsUrl); if (comments == null) { return(new List <TorrentComment>()); } return(comments .Select(comment => new TorrentComment( comment.CommentId, comment.Username, comment.Comment, comment.Posted, comment.Avatar.StartsWith("/") ? $"{Configuration.BaseUrl}{comment.Avatar}" : comment.Avatar) ).ToList()); }
/// <inheritdoc /> public async Task <FhrResponse <T> > GetAsync <T>(string requestUri) where T : EntityBase { // Add the version header var headers = new Dictionary <string, string> { { HttpConstants.FhrApiVersionHeaderKey, HttpConstants.FhrApiVersionHeaderValue } }; // Get the http response var httpResponse = await _httpProvider.GetAsync(requestUri, headers); var contentJson = await httpResponse.Content.ReadAsStringAsync(); try { // Return our deserialized object var response = JsonConvert.DeserializeObject <T>(contentJson); return(new FhrResponse <T> { Response = response, Message = "Success" }); } catch (Exception ex) { return(new FhrResponse <T> { Response = null, Message = $"The following error occured: {ex.Message}" }); } }
public async Task <League> GetLeagueAsync(int leagueId, int year) { string url = _urlConfigurationProvider.GetLeagueEndpointUrl(leagueId, year); return(await _httpProvider.GetAsync <League>(url)); }