/** * Does a HTTP request backed up by fallback servers */ protected function requestWithFallback($method, $path, $headers = [], $params = [], $attempt = 0) { try { if ($attempt == 0) { // using default host $server = ($this->options->tls ? 'https://' : 'http://') . $this->options->restHost; } else { // using a fallback host Log::d('Connection failed, attempting with fallback server #' . $attempt); // attempt 1 uses fallback host with index 0 $server = ($this->options->tls ? 'https://' : 'http://') . $this->options->fallbackHosts[$attempt - 1]; } return $this->http->request($method, $server . $path, $headers, $params); } catch (AblyRequestException $e) { if ($e->getCode() >= 50000) { if ($attempt < min($this->options->httpMaxRetryCount, count($this->options->fallbackHosts))) { return $this->requestWithFallback($method, $path, $headers, $params, $attempt + 1); } else { Log::e('Failed to connect to server and all of the fallback servers.'); throw $e; } } throw $e; // other error code than timeout, rethrow exception } }
public function request($method, $url, $headers = [], $params = []) { $this->lastHeaders = $headers; $this->lastParams = $params; $this->lastResponse = parent::request($method, $url, $headers, $params); return $this->lastResponse; }
/** * Test library interoperability by sending messages from messages-encoding.json * via the library, fetching them using raw HTTP and comparing */ public function testEncodingInteroperabilityAblyToRaw() { $fixture = json_decode(file_get_contents(__DIR__ . '/../ably-common/test-resources/messages-encoding.json')); $defaultOpts = new \Ably\Models\ClientOptions(self::$defaultOptions); $http = new \Ably\Http($defaultOpts); // initialize http class for raw requests with default timeouts $server = 'https://' . $defaultOpts->restHost; $messages = []; foreach ($fixture->messages as $i => $testMsgData) { $msg = new Message(); $msg->data = $testMsgData->data; $msg->encoding = $testMsgData->encoding; $messages[] = $msg; } self::$ably->channel("interopTest2")->publish($messages); $res = $http->request('GET', $server . '/channels/interopTest2/messages?direction=forwards', ['Accept: application/json', 'Authorization: Basic ' . base64_encode(self::$testApp->getAppKeyDefault()->string)]); $history = $res['body']; foreach ($fixture->messages as $i => $testMsgData) { $msg = $history[$i]; $this->assertEquals($testMsgData->data, $msg->data); $this->assertEquals($testMsgData->encoding, $msg->encoding); } }