Пример #1
0
        public void TestHttpClientDefaults()
        {
            var http = HttpClientHelpers.Create();

            Assert.AreEqual(HttpClientHelpers.DefaultTimeout, http.Timeout);
            Assert.AreEqual(HttpClientHelpers.UserAgentString(), String.Join(" ", http.DefaultRequestHeaders.UserAgent.Select(x => x.ToString())));
        }
Пример #2
0
 internal async Task GetRequestAsync(string requestUri)
 {
     using (var httpClient = HttpClientHelpers.CreateHttpClient(Token))
         using (var response = await httpClient.GetAsync(requestUri))
             using (var streamReader = new StreamReader(await response.Content.ReadAsStreamAsync()))
                 CheckResponse <Object>(response, streamReader);
 }
        public async Task InitAsync()
        {
            if (this._initialized)
            {
                return;
            }
            using (HttpClient client = new HttpClient((HttpMessageHandler)HttpClientHelpers.GetClientHandler(true)))
            {
                HttpResponseMessage async = await client.GetAsync(this._descriptorUrl);

                if (async.IsSuccessStatusCode)
                {
                    this._descriptor = (ConfigurationDescriptor)JsonConvert.DeserializeObject <ConfigurationDescriptor>(await async.Content.ReadAsStringAsync());
                    switch (ConfigurationRepository.EnvironmentSetting.Environment)
                    {
                    case Ekreta.Mobile.Core.Models.Environments.Environments.PROD:
                        ConfigurationRepository.EnvironmentSetting.GlobalMobileApiUrl = "https://kretaglobalmobileapi2.ekreta.hu";
                        break;

                    case Ekreta.Mobile.Core.Models.Environments.Environments.UAT:
                        ConfigurationRepository.EnvironmentSetting.GlobalMobileApiUrl = this._descriptor.GlobalMobileApiUrlUAT;
                        break;

                    case Ekreta.Mobile.Core.Models.Environments.Environments.TEST:
                        ConfigurationRepository.EnvironmentSetting.GlobalMobileApiUrl = this._descriptor.GlobalMobileApiUrlTEST;
                        break;

                    case Ekreta.Mobile.Core.Models.Environments.Environments.DEV:
                        ConfigurationRepository.EnvironmentSetting.GlobalMobileApiUrl = this._descriptor.GlobalMobileApiUrlDEV;
                        break;
                    }
                    this._initialized = true;
                }
            }
        }
Пример #4
0
        public async Task ExternalWebContentSource_TestAllServers()
        {
            var urls = await ExternalWebContentSource.LoadInternalServerListAsync();

            var failedUrls = new List <Tuple <Uri, object> >();

            foreach (var url in urls)
            {
                // Make 3 attempts to successfully connect to each server.
                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        var hc     = HttpClientHelpers.Create(userAgent: UnitTestUserAgent());
                        var result = await hc.GetByteArrayAsync(url);

                        break;
                    }
                    catch (Exception ex)
                    {
                        await Task.Delay(1000);

                        if (i == 2)
                        {
                            failedUrls.Add(Tuple.Create(url, (object)ex));
                        }
                    }
                }
            }

            if (failedUrls.Any())
            {
                throw new Exception("Urls failed to GET: " + String.Join("\n", failedUrls.Select(x => x.Item1.ToString() + " - " + x.Item2.ToString())));
            }
        }
Пример #5
0
        public async Task <SearchDataAndResults> ExecuteSearchAsync(string searchArea, CancellationToken cancellationToken)
        {
            try
            {
                var searchDataAndResults = new SearchDataAndResults {
                    CookieContainer = new CookieContainer()
                };
                var handler = HttpClientHelpers.CreateHttpClientHandler(_systemConfig, _configuration, searchDataAndResults.CookieContainer);

                await _logger.LogInformationAsync($"Beginning searches for {searchArea.ToUpper()}...", cancellationToken);

                using (var client = new HttpClientWrapper(_configuration.BaseUri, handler, _logger, _systemConfig))
                {
                    await LogSearchInputsAsync(cancellationToken);

                    var searchPageResponse = await client.GetAsync(_configuration.SearchRoute, new CancellationToken());

                    var searchDates = await DateChunker.SplitDateRange(_searchConfig.StartDate, _searchConfig.EndDate, _configuration.ChunkSizeDays);

                    searchDataAndResults.SearchResultsPages = new List <HttpResponseMessage>();
                    client.DefaultRequestHeaders.Add("Referer", $"{_configuration.BaseUri}{_configuration.SearchRoute}");
                    searchDataAndResults.SearchResultsPages = new List <HttpResponseMessage>();

                    foreach (KeyValuePair <string, string> range in searchDates)
                    {
                        async Task <HttpRequestMessage> SearchRequestBuilder() => await BuildPostFormUrlEncodedRequestAsync(searchPageResponse, range, cancellationToken);

                        var searchPostResponse = await client.PostAsync(SearchRequestBuilder, new CancellationToken());

                        await _logger.LogInformationAsync($"Post search response status: {searchPostResponse.StatusCode}", cancellationToken);

                        var searchResults = await client.GetAsync(_configuration.SearchResultsRoute, new CancellationToken());

                        async Task <HttpRequestMessage> SearchResultsRequestBuilder() => await BuildPostAllFormUrlEncodedRequestAsync(searchResults, cancellationToken);

                        var searchPostResponseAll = await client.PostAsync(SearchResultsRequestBuilder, new CancellationToken());

                        HttpResponseMessage searchResultsAll;
                        if (searchPostResponseAll.StatusCode == HttpStatusCode.Found)
                        {
                            searchResultsAll = await client.GetAsync(_configuration.SearchResultsRoute, new CancellationToken());
                        }
                        else
                        {
                            searchResultsAll = searchPostResponseAll;
                        }

                        searchDataAndResults.SearchResultsPages.Add(searchResultsAll);
                    }

                    return(searchDataAndResults);
                }
            }
            catch (Exception ex)
            {
                await _logger.LogExceptionAsync($"Search failed!", ex, cancellationToken);

                throw new SearchFailedException(ex.Message, ex.InnerException);
            }
        }
Пример #6
0
            public async Task <byte[]> ResetAndRun()
            {
                // TODO: timeout.
                var hash = SHA256.Create();
                var hc   = HttpClientHelpers.Create(userAgent: UserAgent);
                var sw   = Stopwatch.StartNew();

                try
                {
                    var responseBytes = await hc.GetByteArrayAsync(Url);

                    sw.Stop();
                    Log.Trace("GET from '{0}' in {1:N2}ms, received {2:N0} bytes", Url, sw.Elapsed.TotalMilliseconds, responseBytes.Length);
                    var result = hash.ComputeHash(
                        responseBytes
                        .Concat(BitConverter.GetBytes(sw.ElapsedTicks))
                        .Concat(StaticEntropy)
                        .ToArray()
                        );
                    return(result);
                }
                catch (Exception ex)
                {
                    Log.WarnException("Exception when trying to GET from {0}", ex, Url);
                    return(null);
                }
            }
Пример #7
0
 private static async Task <Tuple <TResult, Pages> > DeserializeRequestAsync <TResult>(OAuthToken oauthToken, string requestUri)
 {
     using (var httpClient = HttpClientHelpers.CreateHttpClient(oauthToken))
         using (var response = await httpClient.GetAsync(requestUri))
             using (var streamReader = new StreamReader(await response.Content.ReadAsStreamAsync()))
                 return(ParseResponse <TResult>(response, streamReader));
 }
        public async Task <IList <TransactionEntity> > GetTransactions()
        {
            var transactions = await HttpClientHelpers.GetAsync <IList <TransactionEntity> >(appConfigSettings.TransactionApiUrl)
                               .ConfigureAwait(false);

            return(transactions);
        }
 internal RandomOrgExternalRandomSource(bool useDiskSourceForUnitTests, Guid apiKey)
     : base(TimeSpan.Zero, TimeSpan.Zero, TimeSpan.Zero)
 {
     this._UserAgent = HttpClientHelpers.UserAgentString();
     this._UseDiskSourceForUnitTests = useDiskSourceForUnitTests;
     this._ApiKey = apiKey;
 }
Пример #10
0
        public string finace()
        {
            string         fin = HttpClientHelpers.Send("get", "/api/WangLuChao/ShowFinances", null);
            List <Finance> ss  = JsonConvert.DeserializeObject <List <Finance> >(fin);

            return(JsonConvert.SerializeObject(ss));
        }
Пример #11
0
        /// <summary>
        /// 下拉框
        /// </summary>
        /// <returns></returns>
        ///
        public string HHer()
        {
            string            result = HttpClientHelpers.Send("get", "/api/WangLuChao/ShowDepartMent", null);
            List <DepartMent> jias   = JsonConvert.DeserializeObject <List <DepartMent> >(result);

            return(JsonConvert.SerializeObject(jias));
        }
Пример #12
0
        public async Task <string> GenerateToken(string email, string password)
        {
            var client = new HttpClient {
                BaseAddress = new Uri(this._iconfiguration["Identity:Authority"])
            };

            var parameters = new Dictionary <string, string>
            {
                { "grant_type", "password" },
                { "scope", "openid api1" },
                { "client_id", "rel.angular" },
                { "client_secret", "secret" },
                { "username", string.Empty + email + string.Empty },
                { "password", string.Empty + password + string.Empty }
            };

            var response = await client.PostAsync("/connect/token", HttpClientHelpers.GetPostBody(parameters));

            response.EnsureSuccessStatusCode();
            var responseBody = await response.Content.ReadAsStringAsync();

            var token = JsonConvert.DeserializeObject <TokenDetail>(responseBody);

            return(token.access_token);
        }
Пример #13
0
        public async Task <IList <RateEntity> > GetRates()
        {
            var rates = await HttpClientHelpers.GetAsync <IList <RateEntity> >(appConfigSettings.RatesApiUrl)
                        .ConfigureAwait(false);

            return(rates);
        }
Пример #14
0
        public async Task <GetAccessTokenFromSecretKeyResponse> GetAccessTokenFromSecretKey(string secretKey, string clientId)
        {
            var client = new HttpClient()
            {
                BaseAddress = new Uri(_authUri)
            };

            client.DefaultRequestHeaders.Authorization = BasicAuthHeader.GetHeader(clientId, secretKey);

            var parameters = new Dictionary <string, string>()
            {
                { "grant_type", "client_credentials" },
                { "scope", "api1" }
            };

            var clientResponse = await client.PostAsync("connect/token", HttpClientHelpers.GetPostBody(parameters));

            var response = await QuickResponse <GetAccessTokenFromSecretKeyResponse> .FromMessage(clientResponse);

            if (response.Data?.AccessToken == null)
            {
                throw new Exception($"Could not get access token. Error: {response.ResponseBody}");
            }

            return(response.Data);
        }
Пример #15
0
 protected async Task <QuickResponse <T, TOptions> > PostHttp <T, TOptions>(string url, object data)
 {
     return(await SendHttp <T, TOptions>(() => new HttpRequestMessage(HttpMethod.Post, Options.BaseUri + url)
     {
         Content = HttpClientHelpers.GetJsonBody(data)
     }));
 }
Пример #16
0
 protected async Task <QuickResponse <T> > PostHttp <T>(string url, Dictionary <string, string> parameters)
 {
     return(await SendHttp <T>(() => new HttpRequestMessage(HttpMethod.Post, Options.BaseUri + url)
     {
         Content = HttpClientHelpers.GetPostBody(parameters)
     }));
 }
Пример #17
0
 protected async Task <ApiResponse <T> > PostHttp <T>(string url, object data, bool requiresAuthentication = false)
 {
     return(await SendHttp <T>(() => new HttpRequestMessage(HttpMethod.Post, $"{BaseUri}/{url}")
     {
         Content = HttpClientHelpers.GetJsonBody(data)
     }, requiresAuthentication));
 }
Пример #18
0
        public string Show()
        {
            string          result = HttpClientHelpers.Send("get", "/api/WangLuChao/ShowEmployee", null);
            List <Employee> jias   = JsonConvert.DeserializeObject <List <Employee> >(result);

            return(JsonConvert.SerializeObject(jias));
        }
        public void GetTestableHttpRequestMessage_ReturnsHttpRequestMessage()
        {
            var message = HttpClientHelpers.GetTestableHttpRequestMessage(HttpMethod.Get);

            message.Should().NotBeNull();
            message.Should().BeOfType <HttpRequestMessage>();
            message.Content.Should().BeNull();
        }
Пример #20
0
 internal async Task <TResult> PostRequestAsync <TResult>(string requestUri, string content)
 {
     using (var httpClient = HttpClientHelpers.CreateHttpClient(Token))
         using (var httpContent = new StringContent(content))
             using (var response = await httpClient.PostAsync(requestUri, httpContent))
                 using (var streamReader = new StreamReader(await response.Content.ReadAsStreamAsync()))
                     return(CheckResponse <TResult>(response, streamReader));
 }
 public CommentAggregator(HttpClient httpClient, IHttpContextAccessor httpContextAccessor, HttpClientHelpers responseProcessor, IConfiguration configs, ILogger <CommentAggregator> logger, UserService userService)
 {
     _httpClient        = httpClient;
     _httpContext       = httpContextAccessor.HttpContext;
     _responseProcessor = responseProcessor;
     _configs           = configs;
     _logger            = logger;
     _userService       = userService;
 }
Пример #22
0
        /// <summary>
        /// Create a user-agent string to use with HTTP requests.
        /// </summary>
        /// <param name="usageIdentifier">An email address, website, or other identifying mark to include.</param>
        /// <exception cref="System.Exception">May throw if the usageIdentifier has invalid characters.</exception>
        public static string UserAgent(string usageIdentifier)
        {
            var id   = (usageIdentifier ?? "unconfigured").Replace("@", ".AT.");
            var ua   = HttpClientHelpers.UserAgentString(id);
            var http = HttpClientHelpers.Create(userAgent: ua);

            http.Dispose();
            return(ua);
        }
Пример #23
0
        public async Task <IEnumerable <PlanningApplication> > ExtractDataAsync(string searchArea, List <HttpResponseMessage> searchResultPages, CookieContainer cookieContainer, CancellationToken cancellationToken)
        {
            try
            {
                _configuration = _configResolver.ResolveConfig(searchArea);

                var currentPage = 0;

                await _logger.LogInformationAsync($"Processing {searchResultPages.Count} search result pages for {searchArea.ToUpper()}", cancellationToken);

                var client = HttpClientHelpers.CreateClient(_configuration.BaseUri, _systemConfig, _configuration, _logger, cookieContainer);

                foreach (var searchResults in searchResultPages)
                {
                    currentPage++;
                    var searchResultsHtml = await searchResults.Content.ReadAsStringAsync();

                    var searchPageResponseDoc = CQ.Create(searchResultsHtml);
                    var appSummaryPaths       = GetAppSummaryPaths(searchPageResponseDoc);

                    await _logger.LogInformationAsync($"Found {appSummaryPaths.Count} planning applications in page {currentPage}...", cancellationToken);

                    var row = 0;
                    foreach (var appSummaryPath in appSummaryPaths)
                    {
                        row++;
                        var planningApplication = new PlanningApplication();
                        await _logger.LogInformationAsync($"Getting application detail for result number {row} application {appSummaryPath}", cancellationToken);

                        await ExtractApplicationSummary(cancellationToken, appSummaryPath, client, planningApplication);

                        var appDetailsPath = await ExtractApplicationDetails(cancellationToken, appSummaryPath, client, planningApplication);

                        await ExtractApplicationContact(cancellationToken, appDetailsPath, client, planningApplication);

                        _planningApplications.Add(planningApplication);

                        if (_configuration.UseProxy)
                        {
                            // refresh client/handler to get a new IP address
                            client = HttpClientHelpers.CreateClient(_configuration.BaseUri, _systemConfig, _configuration, _logger, cookieContainer);
                        }
                    }
                }

                await _logger.LogInformationAsync($"Finished extracting planning data for {searchArea.ToUpper()}...", cancellationToken);

                client.Dispose();

                return(_planningApplications);
            }
            catch (Exception ex)
            {
                throw new ExtractDataFailedException(ex.Message, ex.InnerException);
            }
        }
Пример #24
0
 public CommentAggregator(
     HttpClientHelpers responseProcessor,
     IConfiguration configs,
     UserService userService, IMapper mapper)
 {
     _responseProcessor = responseProcessor;
     _configs           = configs;
     _userService       = userService;
     _mapper            = mapper;
 }
Пример #25
0
 public TransactionService(HttpClient httpClient, IHttpContextAccessor accessor, HttpClientHelpers responseProcessor, IConfiguration configs, ILogger <TransactionService> logger, UserService userService, ObjectService objectService)
 {
     _httpClient        = httpClient;
     _httpContext       = accessor.HttpContext;
     _responseProcessor = responseProcessor;
     _configs           = configs;
     _logger            = logger;
     _userService       = userService;
     _objectService     = objectService;
 }
        public RandomNumbersInfoExternalRandomSource(string userAgent, int numberOfNumbers, TimeSpan periodNormalPriority, TimeSpan periodHighPriority, TimeSpan periodLowPriority)
            : base(periodNormalPriority, periodHighPriority, periodLowPriority)
        {
            if (numberOfNumbers < 0 || numberOfNumbers > 1000)
            {
                throw new ArgumentOutOfRangeException(nameof(numberOfNumbers), numberOfNumbers, "Between 1 and 1000 numbers are allowed");
            }

            this._UserAgent       = String.IsNullOrWhiteSpace(userAgent) ? HttpClientHelpers.UserAgentString() : userAgent;
            this._NumberOfNumbers = numberOfNumbers;
        }
Пример #27
0
        public Task <string> GetAll()
        {
            TaskCompletionSource <string> _taskComplete = new TaskCompletionSource <string>();

            Task.Run(() => {
                string result = HttpClientHelpers.Instance().GET(URLConstants.PROJECT_GET_ALL);
                _taskComplete.SetResult(result);
            });

            return(_taskComplete.Task);
        }
Пример #28
0
        public QrngEthzChExternalRandomSource(string userAgent, int bytesPerRequest, TimeSpan periodNormalPriority, TimeSpan periodHighPriority, TimeSpan periodLowPriority)
            : base(periodNormalPriority, periodHighPriority, periodLowPriority)
        {
            if (bytesPerRequest < 4 || bytesPerRequest > 2048)      // No published maximum, but we'll be nice.
            {
                throw new ArgumentOutOfRangeException(nameof(bytesPerRequest), bytesPerRequest, "Bytes per request must be between 4 and 2048");
            }

            this._UserAgent       = String.IsNullOrWhiteSpace(userAgent) ? HttpClientHelpers.UserAgentString() : userAgent;
            this._BytesPerRequest = bytesPerRequest;
        }
Пример #29
0
        public HotbitsExternalRandomSource(string userAgent, int bytesPerRequest, string apiKey, TimeSpan periodNormalPriority, TimeSpan periodHighPriority, TimeSpan periodLowPriority)
            : base(periodNormalPriority, periodHighPriority, periodLowPriority)
        {
            if (bytesPerRequest < 4 || bytesPerRequest > 2048)      // Max of 2048 bytes based on Web UI.
            {
                throw new ArgumentOutOfRangeException(nameof(bytesPerRequest), bytesPerRequest, "Bytes per request must be between 4 and 2048");
            }

            this._UserAgent       = String.IsNullOrWhiteSpace(userAgent) ? HttpClientHelpers.UserAgentString() : userAgent;
            this._BytesPerRequest = bytesPerRequest;
            this._ApiKey          = string.IsNullOrWhiteSpace(apiKey) ? null : apiKey;
        }
        public RandomOrgExternalRandomSource(string userAgent, int bytesPerRequest, Guid apiKey, TimeSpan periodNormalPriority, TimeSpan periodHighPriority, TimeSpan periodLowPriority)
            : base(periodNormalPriority, periodHighPriority, periodLowPriority)
        {
            if (bytesPerRequest < 4 || bytesPerRequest > 4096)
            {
                throw new ArgumentOutOfRangeException(nameof(bytesPerRequest), bytesPerRequest, "Bytes per request must be between 4 and 4096");
            }

            this._UserAgent       = String.IsNullOrWhiteSpace(userAgent) ? HttpClientHelpers.UserAgentString() : userAgent;
            this._BytesPerRequest = bytesPerRequest;
            this._ApiKey          = apiKey;
        }