/** * Check if the account has reached the maximum number of API calls it can make globally (not API specific) * * @return boolean True if they're within their limits, false if not */ protected static function check_global_limits() { $account_data = \V1\Model\Account::get_account(); $total_calls = \V1\Usage::get_usage(false); // If they've used up all of their calls, and they aren't unlimited, then they've hit their global limits. if ($total_calls === $account_data['max_calls'] && $account_data['max_calls'] != 0) { return false; } return true; }
/** * Set the account free (free tier) * * @return boolean True if successful, false if it fails */ public static function be_free_my_brother() { // Get the cached row $account_data = static::get_account(); // No data if (empty($account_data)) { return false; } // Pull the row so that we have an object to use for Orm manipulation. $account_row = static::find($account_data['id']); $account_row->free_account_on = 0; $account_row->access_level = 1; $account_row->max_calls = \Config::get('tiers.free.max_calls', 10000); $saved = $account_row->save(); \V1\Usage::delete_usage(); return $saved; }
/** * Check the speed limits of an API to make sure that the account isn't calling the API more than it's * allowed to in the specified time frame. * * @return mixed True if the call is within the speed limits, or the error array if it isn't */ private static function check_usage_limits() { $api_data = \V1\Model\APIs::get_api(\V1\APIRequest::get('api')); $account_data = \V1\Model\Account::get_account(); $package_limits = \V1\RAML::parse_package_limits(); /* * Are there any limits on the API for our package level? API limits are imposed * by Bit API Hub, not the API provider. It helps to limit the amount of bandwidth * we allot for each call. The heavier the call, the less calls people can make. */ if (is_array($package_limits) && array_key_exists('level' . $account_data['access_level'], $package_limits) && !empty($usage = \V1\Usage::get_usage(\V1\APIRequest::get('api')))) { // Loop the allotments of API calls per time period foreach ($package_limits['level' . $account_data['access_level']] as $time_period => $allotment) { // If we have a valid log for the time period, and it's already maxed out, fail the check. if (isset($usage[$time_period]) && $usage[$time_period]['count'] == $allotment && \Utility::subtract_seconds($time_period) <= $usage[$time_period]['ts'] && $usage[$time_period]['ts'] <= time()) { return \Utility::format_error(429, \V1\Err::TOO_MANY_REQUESTS, \Lang::get('v1::errors.too_many_requests', array('allotment' => number_format($allotment), 'period' => $time_period))); } } } // We're not breaking the speed limit, so shush. return true; }
/** * Configure a static call * * @return mixed The \V1\APICall object or an error array if the call wasn't found */ public static function configure_call() { if (is_array($static_calls = \V1\RAML::parse_static_calls())) { $api_data = \V1\Model\APIs::get_api(); $account_data = \V1\Model\Account::get_account(); foreach ($static_calls as $call => $call_data) { // The static call data matches the request. if ('/' . \V1\APIRequest::get('static-call') === $call) { // If we can't run the inactive call, then error out. if (isset($call_data['stability']) && $call_data['stability'] === 0 && ($account_data['can_run_inactive'] === 0 || $account_data['id'] !== $api_data['account_id'])) { return \Utility::format_error(403, \V1\Err::DISABLED_STATIC, \Lang::get('v1::errors.disabled_static')); } // Do we have a cached entry? if (is_array($cached_data = \V1\Call\StaticCall::get_call_cache())) { // Set their usage stats. \V1\Usage::set_usage(); // Set the API provider stats \V1\Usage::set_api_stats($cached_data['response']['status']); // Return the response-formatted data from the cached entry. return $cached_data; } // Try to get an APICall object if (($apicall = static::apicall_object($call)) !== false) { return $apicall; } } } // The static call wasn't found. return \Utility::format_error(404, \V1\Err::BAD_STATIC, \Lang::get('v1::errors.bad_static')); } else { // If they've requested a static call when there aren't any for the requested API, give 'em errors! return \Utility::format_error(404, \V1\Err::NO_STATIC, \Lang::get('v1::errors.no_static')); } }
/** * Prepare the response to show the customer. We also handle caching and usage. * * @param array $response The response array containing data from the remote server * @param string $api The name of the API we're preparing the response for * * @return array The response formatted string */ public static function prepare_response(array $response, $api_request = null, $is_static = null) { $api_request = empty($api_request) ? \V1\APIRequest::get() : $api_request; $api = $api_request['api']; $is_static = $is_static === null ? \V1\APIRequest::is_static() : $is_static; $api_data = \V1\Model\APIs::get_api($api); if ($is_static === true && $api_data['account_id'] === 0) { $response['headers'] = array(); } else { $response['headers'] = static::sanitize_headers($response['headers']); } $response['body'] = static::convert_body($response['headers'], $response['body']); $response_array = \Utility::format_response(200, $response); $internal_call = \Utility::is_internal_call(); /* * Cache the static call response if it the remote server didn't report an error. * To allow for proper testing, we don't cache calls from the account area or other internal * locations. */ if ($is_static === true && $response['status'] < 300 && $internal_call === false) { $api_call = $is_static === true ? $api_request['static-call'] : null; \V1\Call\StaticCall::set_call_cache($response_array, $api, $api_call); } /* * Log the usage stats if we aren't running a call from the internal API testing system. (Ex. The * account area) */ if ($internal_call === false) { // Set the account usage stats \V1\Usage::set_usage($api); // Set the API provider stats \V1\Usage::set_api_stats($response['status'], $api_request, $is_static); } return $response_array; }