Differential D12680 Diff 30538 src/infrastructure/daemon/bot/adapter/PhabricatorBotBaseStreamingProtocolAdapter.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/daemon/bot/adapter/PhabricatorBotBaseStreamingProtocolAdapter.php
| Show First 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | foreach ($this->readBuffers as $url => &$buffer) { | ||||
| $until = strpos($buffer, "}\r"); | $until = strpos($buffer, "}\r"); | ||||
| if ($until == false) { | if ($until == false) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| $message = substr($buffer, 0, $until + 1); | $message = substr($buffer, 0, $until + 1); | ||||
| $buffer = substr($buffer, $until + 2); | $buffer = substr($buffer, $until + 2); | ||||
| $m_obj = json_decode($message, true); | $m_obj = phutil_json_decode($message); | ||||
| if ($message = $this->processMessage($m_obj)) { | if ($message = $this->processMessage($m_obj)) { | ||||
| return $message; | return $message; | ||||
| } | } | ||||
| } | } | ||||
| // If we're here, there's nothing to process | // If we're here, there's nothing to process | ||||
| return false; | return false; | ||||
| } | } | ||||
| protected function performPost($endpoint, $data = null) { | protected function performPost($endpoint, $data = null) { | ||||
| $uri = new PhutilURI($this->server); | $uri = new PhutilURI($this->server); | ||||
| $uri->setPath($endpoint); | $uri->setPath($endpoint); | ||||
| $payload = json_encode($data); | $payload = json_encode($data); | ||||
| list($output) = id(new HTTPSFuture($uri)) | list($output) = id(new HTTPSFuture($uri)) | ||||
| ->setMethod('POST') | ->setMethod('POST') | ||||
| ->addHeader('Content-Type', 'application/json') | ->addHeader('Content-Type', 'application/json') | ||||
| ->addHeader('Authorization', $this->getAuthorizationHeader()) | ->addHeader('Authorization', $this->getAuthorizationHeader()) | ||||
| ->setData($payload) | ->setData($payload) | ||||
| ->resolvex(); | ->resolvex(); | ||||
| $output = trim($output); | $output = trim($output); | ||||
| if (strlen($output)) { | if (strlen($output)) { | ||||
| return json_decode($output, true); | return phutil_json_decode($output); | ||||
| } | } | ||||
| return true; | return true; | ||||
| } | } | ||||
| protected function getAuthorizationHeader() { | protected function getAuthorizationHeader() { | ||||
| return 'Basic '.$this->getEncodedAuthToken(); | return 'Basic '.$this->getEncodedAuthToken(); | ||||
| } | } | ||||
| protected function getEncodedAuthToken() { | protected function getEncodedAuthToken() { | ||||
| return base64_encode($this->authtoken.':x'); | return base64_encode($this->authtoken.':x'); | ||||
| } | } | ||||
| abstract protected function buildStreamingUrl($channel); | abstract protected function buildStreamingUrl($channel); | ||||
| abstract protected function processMessage($raw_object); | abstract protected function processMessage(array $raw_object); | ||||
| } | } | ||||