示例#1
0
 private static RestClientConfiguration createConfiguration(
     String keyId,
     String secretKey,
     Uri alpacaRestApi,
     Uri polygonRestApi,
     Uri alpacaDataApi,
     Int32 apiVersion,
     Int32 dataApiVersion,
     ThrottleParameters throttleParameters,
     Boolean isStagingEnvironment,
     String oauthKey)
 {
     if (!(String.IsNullOrEmpty(secretKey) ^
           String.IsNullOrEmpty(oauthKey)))
     {
         throw new ArgumentException("Application secret key or OAuth key (but not both) should not be null or empty.");
     }
     return(new RestClientConfiguration
     {
         KeyId = keyId ?? throw new ArgumentException("Application key id should not be null.", nameof(keyId)),
         SecurityId = String.IsNullOrEmpty(secretKey) ? (SecurityKey) new SecretKey(keyId, secretKey) : new OAuthKey(oauthKey),
         TradingApiUrl = alpacaRestApi ?? Environments.Live.AlpacaTradingApi,
         PolygonApiUrl = polygonRestApi ?? Environments.Live.PolygonDataApi,
         DataApiUrl = alpacaDataApi ?? Environments.Live.AlpacaDataApi,
         TradingApiVersion = (ApiVersion)apiVersion,
         DataApiVersion = (ApiVersion)dataApiVersion,
         ThrottleParameters = throttleParameters ?? ThrottleParameters.Default,
         IsStagingEnvironment = isStagingEnvironment
     });
        /// <summary>
        /// Creates new instance of <see cref="RestClient"/> object.
        /// </summary>
        /// <param name="keyId">Application key identifier.</param>
        /// <param name="secretKey">Application secret key.</param>
        /// <param name="alpacaRestApi">Alpaca REST API endpoint URL.</param>
        /// <param name="polygonRestApi">Polygon REST API endpoint URL.</param>
        /// <param name="alpacaDataApi">Alpaca REST data API endpoint URL.</param>
        /// <param name="apiVersion">Version of Alpaca api to call.  Valid values are "1" or "2".</param>
        /// <param name="dataApiVersion">Version of Alpaca data API to call.  The only valid value is currently "1".</param>
        /// <param name="isStagingEnvironment">If <c>true</c> use staging.</param>
        /// <param name="throttleParameters">Parameters for requests throttling.</param>
        public RestClient(
            String keyId,
            String secretKey,
            Uri alpacaRestApi,
            Uri polygonRestApi,
            Uri alpacaDataApi,
            Int32 apiVersion,
            Int32 dataApiVersion,
            Boolean isStagingEnvironment,
            ThrottleParameters throttleParameters)
        {
            keyId     = keyId ?? throw new ArgumentException(nameof(keyId));
            secretKey = secretKey ?? throw new ArgumentException(nameof(secretKey));

            if (!_supportedApiVersions.Contains(apiVersion))
            {
                throw new ArgumentException(nameof(apiVersion));
            }
            if (!_supportedDataApiVersions.Contains(dataApiVersion))
            {
                throw new ArgumentException(nameof(dataApiVersion));
            }

            _alpacaRestApiThrottler = throttleParameters.GetThrottler();

            _alpacaHttpClient.DefaultRequestHeaders.Add(
                "APCA-API-KEY-ID", keyId);
            _alpacaHttpClient.DefaultRequestHeaders.Add(
                "APCA-API-SECRET-KEY", secretKey);
            _alpacaHttpClient.DefaultRequestHeaders.Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _alpacaHttpClient.BaseAddress = addApiVersionNumberSafe(
                alpacaRestApi ?? new Uri("https://api.alpaca.markets"), apiVersion);

            _alpacaDataClient.DefaultRequestHeaders.Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _alpacaDataClient.BaseAddress = addApiVersionNumberSafe(
                alpacaDataApi ?? new Uri("https://data.alpaca.markets"), dataApiVersion);

            _polygonApiKey = keyId;
            _polygonHttpClient.DefaultRequestHeaders.Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _polygonHttpClient.BaseAddress =
                polygonRestApi ?? new Uri("https://api.polygon.io");
            _isPolygonStaging = isStagingEnvironment ||
                                _alpacaHttpClient.BaseAddress.Host.Contains("staging");

#if NET45
            ServicePointManager.SecurityProtocol =
                SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
#endif
        }
示例#3
0
 /// <summary>
 /// Creates new instance of <see cref="RestClient"/> object.
 /// </summary>
 /// <param name="keyId">Application key identifier.</param>
 /// <param name="secretKey">Application secret key.</param>
 /// <param name="alpacaRestApi">Alpaca REST API endpoint URL.</param>
 /// <param name="polygonRestApi">Polygon REST API endpoint URL.</param>
 /// <param name="alpacaDataApi">Alpaca REST data API endpoint URL.</param>
 /// <param name="apiVersion">Version of Alpaca API to call.  Valid values are "1" or "2".</param>
 /// <param name="dataApiVersion">Version of Alpaca data API to call.  The only valid value is currently "1".</param>
 /// <param name="isStagingEnvironment">If <c>true</c> use staging.</param>
 /// <param name="throttleParameters">Parameters for requests throttling.</param>
 /// <param name="oauthKey">Key for alternative authentication via oauth. keyId and secretKey will be ignored if provided.</param>
 public RestClient(
     String keyId,
     String secretKey,
     Uri alpacaRestApi,
     Uri polygonRestApi,
     Uri alpacaDataApi,
     Int32 apiVersion,
     Int32 dataApiVersion,
     ThrottleParameters throttleParameters,
     Boolean isStagingEnvironment,
     String oauthKey)
     : this(createConfiguration(
                keyId, secretKey, alpacaRestApi, polygonRestApi, alpacaDataApi, apiVersion, dataApiVersion, throttleParameters, isStagingEnvironment, oauthKey))
 {
 }
        /// <summary>
        /// Creates new instance of <see cref="RateThrottler"/> object with parameters
        /// specified in <paramref name="throttleParameters"/> parameter.
        /// </summary>
        /// <param name="throttleParameters"></param>
        public RateThrottler(
            ThrottleParameters throttleParameters)
        {
            _timeUnitMilliseconds = (Int32)throttleParameters.TimeUnit.TotalMilliseconds;
            MaxRetryAttempts      = throttleParameters.MaxRetryAttempts;
            _retryHttpStatuses    = new HashSet <Int32>(throttleParameters.RetryHttpStatuses);

            // Create the throttle semaphore, with the number of occurrences as the maximum count.
            _throttleSemaphore = new SemaphoreSlim(throttleParameters.Occurrences, throttleParameters.Occurrences);

            // Create a queue to hold the semaphore exit times.
            _exitTimes = new ConcurrentQueue <Int32>();

            // Create a timer to exit the semaphore. Use the time unit as the original
            // interval length because that's the earliest we will need to exit the semaphore.
            _exitTimer = new Timer(exitTimerCallback, null, _timeUnitMilliseconds, -1);
        }
 /// <summary>
 /// Creates new instance of <see cref="RestClient"/> object.
 /// </summary>
 /// <param name="keyId">Application key identifier.</param>
 /// <param name="secretKey">Application secret key.</param>
 /// <param name="alpacaRestApi">Alpaca REST API endpoint URL.</param>
 /// <param name="polygonRestApi">Polygon REST API endpoint URL.</param>
 /// <param name="alpacaDataApi">Alpaca REST data API endpoint URL.</param>
 /// <param name="apiVersion">Version of Alpaca api to call.  Valid values are "1" or "2".</param>
 /// <param name="isStagingEnvironment">If <c>true</c> use staging.</param>
 /// <param name="throttleParameters">Parameters for requests throttling.</param>
 public RestClient(
     String keyId,
     String secretKey,
     String alpacaRestApi                  = null,
     String polygonRestApi                 = null,
     String alpacaDataApi                  = null,
     Int32?apiVersion                      = null,
     Boolean?isStagingEnvironment          = null,
     ThrottleParameters throttleParameters = null)
     : this(
         keyId,
         secretKey,
         new Uri(alpacaRestApi ?? "https://api.alpaca.markets"),
         new Uri(polygonRestApi ?? "https://api.polygon.io"),
         new Uri(alpacaDataApi ?? "https://data.alpaca.markets"),
         apiVersion ?? DEFAULT_API_VERSION_NUMBER,
         isStagingEnvironment ?? false,
         throttleParameters ?? ThrottleParameters.Default)
 {
 }
示例#6
0
        /// <summary>
        /// Creates new instance of <see cref="RestClient"/> object.
        /// </summary>
        /// <param name="keyId">Application key identifier.</param>
        /// <param name="secretKey">Application secret key.</param>
        /// <param name="alpacaRestApi">Alpaca REST API endpoint URL.</param>
        /// <param name="polygonRestApi">Polygon REST API endpoint URL.</param>
        /// <param name="alpacaDataApi">Alpaca REST data API endpoint URL.</param>
        /// <param name="apiVersion">Version of Alpaca API to call.  Valid values are "1" or "2".</param>
        /// <param name="dataApiVersion">Version of Alpaca data API to call.  The only valid value is currently "1".</param>
        /// <param name="isStagingEnvironment">If <c>true</c> use staging.</param>
        /// <param name="throttleParameters">Parameters for requests throttling.</param>
        /// <param name="oauthKey">Key for alternative authentication via oauth. keyId and secretKey will be ignored if provided.</param>
        public RestClient(
            String keyId,
            String secretKey,
            Uri alpacaRestApi,
            Uri polygonRestApi,
            Uri alpacaDataApi,
            Int32 apiVersion,
            Int32 dataApiVersion,
            Boolean isStagingEnvironment,
            ThrottleParameters throttleParameters,
            String oauthKey)
        {
            keyId = keyId ?? throw new ArgumentException(
                              "Application key id should not be null", nameof(keyId));
            secretKey = secretKey ?? throw new ArgumentException(
                                  "Application secret key id should not be null", nameof(secretKey));

            if (!_supportedApiVersions.Contains(apiVersion))
            {
                throw new ArgumentException(
                          "Supported REST API versions are '1' and '2' only", nameof(apiVersion));
            }
            if (!_supportedDataApiVersions.Contains(dataApiVersion))
            {
                throw new ArgumentException(
                          "Supported Data REST API versions are '1' and '2' only", nameof(dataApiVersion));
            }

            throttleParameters      = throttleParameters ?? ThrottleParameters.Default;
            _alpacaRestApiThrottler = throttleParameters.GetThrottler();

            if (string.IsNullOrEmpty(oauthKey))
            {
                _alpacaHttpClient.DefaultRequestHeaders.Add(
                    "APCA-API-KEY-ID", keyId);
                _alpacaHttpClient.DefaultRequestHeaders.Add(
                    "APCA-API-SECRET-KEY", secretKey);
            }
            else
            {
                _alpacaHttpClient.DefaultRequestHeaders.Add(
                    "Authorization", "Bearer " + oauthKey);
            }
            _alpacaHttpClient.DefaultRequestHeaders.Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _alpacaHttpClient.BaseAddress = addApiVersionNumberSafe(
                alpacaRestApi ?? new Uri("https://api.alpaca.markets"), apiVersion);

            _alpacaDataClient.DefaultRequestHeaders.Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _alpacaDataClient.BaseAddress = addApiVersionNumberSafe(
                alpacaDataApi ?? new Uri("https://data.alpaca.markets"), dataApiVersion);

            _polygonApiKey = keyId;
            _polygonHttpClient.DefaultRequestHeaders.Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _polygonHttpClient.BaseAddress =
                polygonRestApi ?? new Uri("https://api.polygon.io");
            _isPolygonStaging = isStagingEnvironment ||
                                _alpacaHttpClient.BaseAddress.Host.Contains("staging");

#if NET45
            System.Net.ServicePointManager.SecurityProtocol =
#pragma warning disable CA5364 // Do Not Use Deprecated Security Protocols
                System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11;
#pragma warning restore CA5364 // Do Not Use Deprecated Security Protocols
#endif
        }