/**
  * 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'));
     }
 }
 /**
  * Call the remote API server
  * 
  * @param \V1\APICall $apicall_obj The APICall object we're using to make calls
  * @return array The array of data ready for display (Response array or error array)
  */
 public function make_the_call(\V1\APICall $apicall_obj)
 {
     /**
      * RUNCLE RICK'S RAD RUN CALL :)
      */
     $api = \V1\Model\APIs::get_api();
     $account = \V1\Model\Account::get_account();
     /*
      * When we make a call from the local server, we'll get the localhost IP. If that's the case,
      * we'll set our public IP. DO NOT use X-Forwarded-For in the request headers to us. It's unreliable.
      * We'll still set our X-Forwarded-For in case the API provider wishes to use it.
      */
     $forwarded_for = \Input::real_ip('0.0.0.0', true);
     if ($internal_call = \Utility::is_internal_call()) {
         $forwarded_for = \Config::get('engine.call_test_ip');
     }
     /*
      * Add our own headers to allow for authenticating our server and customers. We overwrite any
      * of these headers that were specified by the API Provider through RAML, or by the developer
      * through thier configuration.
      */
     $headers = static::get_headers($apicall_obj->get_headers());
     $call = array('url' => $apicall_obj->get_url(), 'method' => $apicall_obj->get_method(), 'headers' => $headers, 'body' => $apicall_obj->get_method_params(), 'body-type' => $apicall_obj->get_body_type());
     if (\Fuel::$env !== 'production' && \Config::get('engine.dev_disable_live_calls', false) === true) {
         /**
          * In dev mode we can disable calls to the remote server. Feel free to change the
          * dummy response to whatever you'd like to.
          */
         $response = array('status' => 200, 'headers' => array('X-Dev-Mode' => 'Dummy header'), 'body' => array('dummy_key' => 'dummy_val'));
         return \Utility::format_response(200, $response);
     } else {
         /*
          * We'll see if anyone got a cached entry into our system while we were configuring stuff.
          * That way we'll save time.
          */
         if (\V1\APIRequest::is_static() && is_array($cached_data = \V1\Call\StaticCall::get_call_cache())) {
             // Return the response-formatted data from the cached entry.
             return $cached_data;
         }
         $queued = \V1\Socket::forge()->queue_call(\V1\APIRequest::get('api'), $call, $apicall_obj);
     }
     // Non-Data Calls grab the request right away.
     if (\Session::get('data_call', false) === false) {
         if ($queued === false) {
             // Server unavailable
             return \Utility::format_error(503, \Err::SERVER_ERROR, \Lang::get('v1::errors.remote_unavailable'));
         }
         // Pull the results.
         $result = \V1\Socket::forge()->get_results();
         if (is_array($result)) {
             // We only have one call.
             return $result[\V1\APIRequest::get('api')][0];
         } else {
             // If the request failed with false, it means that all streams timed out.
             return \Utility::format_error(500);
         }
     }
     $dc_response = array('status' => 200, 'headers' => array(), 'body' => \V1\Constant::QUEUED_CALL);
     // In Data Call mode we just signify that we've queued the call.
     return \Utility::format_response(200, $dc_response);
 }
 /**
  * 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;
 }