Exemple #1
0
 public HttpFacade(
     IPushLogger logger, HttpClient client, FaultTolerantExecutor executor)
 {
     _logger   = logger;
     _client   = client;
     _executor = executor;
 }
Exemple #2
0
        public void Initialize(AcumaticaCredentials credentials)
        {
            _credentials = credentials;

            BaseAddress  = new Uri(credentials.InstanceUrl);
            _instanceUrl = credentials.InstanceUrl;

            _executor = new FaultTolerantExecutor()
            {
                MaxNumberOfAttempts = _settings.MaxAttempts,
                ThrottlingKey       = credentials.InstanceUrl,
                Logger = _logger,
            };

            _httpClient
                = new HttpClient(
                      new HttpClientHandler
            {
                UseCookies      = true,
                CookieContainer = new CookieContainer(),
            })
                {
                BaseAddress           = BaseAddress,
                DefaultRequestHeaders =
                {
                    Accept =
                    {
                        MediaTypeWithQualityHeaderValue.Parse("text/json")
                    }
                }
                };

            var headerValue = $"{credentials.Username}:{credentials.Password}";
            var byteArray   = Encoding.ASCII.GetBytes(headerValue);

            _httpClient.DefaultRequestHeaders.Authorization
                = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));


            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;
            ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); };

            // Spawn the WebRequest
            _httpClient.Timeout = new TimeSpan(0, 0, 0, _settings.Timeout);
        }
Exemple #3
0
        public void Initialize(IShopifyCredentials credentials, int?shopifyDelayMsOverride = null)
        {
            BaseAddress = new Uri(credentials.Domain.BaseUrl);

            var throttlingDelay = shopifyDelayMsOverride ?? _httpConfig.ThrottlingDelay;

            _executor = new FaultTolerantExecutor()
            {
                MaxNumberOfAttempts = _httpConfig.MaxAttempts,
                ThrottlingKey       = credentials.Domain.BaseUrl,
                ThrottlingDelay     = throttlingDelay,
                Logger = _logger,
            };

            _httpClient
                = new HttpClient(
                      new HttpClientHandler
            {
                UseCookies      = true,
                CookieContainer = new CookieContainer()
            })
                {
                BaseAddress           = this.BaseAddress,
                DefaultRequestHeaders =
                {
                    Accept =
                    {
                        MediaTypeWithQualityHeaderValue.Parse("application/json")
                    }
                }
                };

            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;
            ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); };

            // Spawn the WebRequest
            //
            _httpClient.Timeout = new TimeSpan(0, 0, 0, _httpConfig.Timeout);

            // Key and Secret authentication, for purpose of retrieving OAuth access token
            //
            if (credentials is ApiKeyAndSecret)
            {
                var oAuthKeyAndSecret = credentials as ApiKeyAndSecret;

                var basicCreds = $"{oAuthKeyAndSecret.ApiKey}:{oAuthKeyAndSecret.ApiSecret}";

                _httpClient.DefaultRequestHeaders.Authorization
                    = new AuthenticationHeaderValue("Basic", basicCreds);
            }

            // Authentication using OAuth access token
            //
            if (credentials is OAuthAccessToken)
            {
                var accessTokenCred = credentials as OAuthAccessToken;

                _httpClient.DefaultRequestHeaders.Add("X-Shopify-Access-Token", accessTokenCred.AccessToken);
            }

            // Authentication using Key Credentials i.e. Shopify private app
            //
            if (credentials is PrivateAppCredentials)
            {
                var privateAppCreds = credentials as PrivateAppCredentials;

                var headerValue = $"{privateAppCreds.ApiKey}:{privateAppCreds.ApiPassword}";
                var byteArray   = Encoding.ASCII.GetBytes(headerValue);

                _httpClient.DefaultRequestHeaders.Authorization
                    = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
            }
        }