void FilterRequests(IHttpRequest request, IHttpResponse response, object requestObject)
        {
            string userName = request.Headers.Get("Username");
            string apiKey   = request.Headers.Get("ApiKey");

            if (String.IsNullOrEmpty(userName))
            {
                throw new Exception("Please suply username in 'Username' header");
            }

            if (String.IsNullOrEmpty(apiKey))
            {
                throw new Exception("Please suply API key in 'ApiKey' header");
            }

            if (!QuotaProvider.AuthenticateUser(userName, apiKey))
            {
                throw new Exception("Invalid user or API key");
            }

            int quotaAmount = QuotaProvider.GetHourlyQuota(userName);

            if (quotaAmount >= 0)
            {
                int currentUsage = UsageStorer.GetCurrentUsage(userName);

                //Increment for each API call
                currentUsage++;

                if (currentUsage > quotaAmount)
                {
                    throw new Exception("You have exceded your quota!");
                }

                UsageStorer.StoreNewUsage(userName, currentUsage);
            }
        }
Пример #2
0
 public void GetInvalidUsageTest()
 {
     Assert.AreEqual(0, storer.GetCurrentUsage("foo"));
 }