Ejemplo n.º 1
0
 public bool IsSafe(NetworkProviderContext context)
 {
     lock (_lock)
     {
         _hits.Add(DateTime.UtcNow);
         var expired = DateTime.UtcNow.AddMinutes(-_perMinutes);
         _hits.RemoveAll(x => x <= expired);
         return(_hits.Count < _requests);
     }
 }
Ejemplo n.º 2
0
        public static void EnterRate(INetworkProvider provider, NetworkProviderContext context)
        {
            var limiter = provider.RateLimiter;

            if (limiter.IsSafe(context))
            {
                return;
            }

            limiter.Limit();
        }
Ejemplo n.º 3
0
        public static async Task <ApiResponse <T> > WrapExceptionAsync <T>(Func <Task <T> > t, string name, INetworkProvider provider, NetworkProviderContext context, Action <T> afterSuccess = null)
        {
            if (t == null)
            {
                return(new ApiResponse <T>("Not implemented"));
            }

            try
            {
                EnterRate(provider, context);
                var sw = new Stopwatch();
                sw.Start();
                context.L.Trace("Api: " + provider.Title + " " + name);
                var response = await t.Invoke().ConfigureAwait(false);

                context.L.Trace("Api finished @ " + sw.ToElapsed() + " : " + provider.Title + " " + name);
                if (response != null)
                {
                    afterSuccess?.Invoke(response);
                }
                return(new ApiResponse <T>(response));
            }
            catch (ApiResponseException ae)
            {
                context.L.Error("Api Error: " + provider.Title + " " + name + ": " + ae.Message);
                return(new ApiResponse <T>(ae.Message));
            }
            catch (Exception e)
            {
                context.L.Error("Api Error: " + provider.Title + " " + name + ": " + e.Message);
                return(new ApiResponse <T>(e));
            }
        }
Ejemplo n.º 4
0
 public static Task <ApiResponse <List <AssetInfo> > > GetCoinInformationAsync(ICoinInformationProvider provider, NetworkProviderContext context = null)
 {
     context = context ?? new NetworkProviderContext();
     return(ApiHelpers.WrapExceptionAsync(() => provider.GetCoinInformationAsync(context), nameof(GetCoinInformation), provider, context));
 }
Ejemplo n.º 5
0
 public static Task <ApiResponse <TransferSuspensions> > GetTransferSuspensionsAsync(IDepositProvider provider, NetworkProviderContext context = null)
 {
     context = context ?? new NetworkProviderContext();
     return(ApiHelpers.WrapExceptionAsync(() => provider.GetTransferSuspensionsAsync(context), nameof(GetTransferSuspensions), provider, context));
 }
Ejemplo n.º 6
0
        public static Task <ApiResponse <AssetPairs> > GetAssetPairsAsync(IAssetPairsProvider provider, NetworkProviderContext context = null)
        {
            context = context ?? new NetworkProviderContext();

            return(AssetPairCache.I.TryAsync(provider, () => ApiHelpers.WrapExceptionAsync(() => provider.GetAssetPairsAsync(context), nameof(GetAssetPairs), provider, context)));
        }
Ejemplo n.º 7
0
 public static ApiResponse <List <AssetInfo> > GetCoinInformation(ICoinInformationProvider provider, NetworkProviderContext context = null)
 {
     return(AsyncContext.Run(() => GetCoinInformationAsync(provider, context)));
 }
Ejemplo n.º 8
0
 public static ApiResponse <TransferSuspensions> GetTransferSuspensions(IDepositProvider provider, NetworkProviderContext context = null)
 {
     return(AsyncContext.Run(() => GetTransferSuspensionsAsync(provider, context)));
 }
Ejemplo n.º 9
0
 public static ApiResponse <AssetPairs> GetAssetPairs(IAssetPairsProvider provider, NetworkProviderContext context = null)
 {
     return(AsyncContext.Run(() => GetAssetPairsAsync(provider, context)));
 }