diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -4567,6 +4567,7 @@ 'PhabricatorTriggerClockTestCase' => 'infrastructure/daemon/workers/clock/__tests__/PhabricatorTriggerClockTestCase.php', 'PhabricatorTriggerDaemon' => 'infrastructure/daemon/workers/PhabricatorTriggerDaemon.php', 'PhabricatorTrivialTestCase' => 'infrastructure/testing/__tests__/PhabricatorTrivialTestCase.php', + 'PhabricatorTwilioFuture' => 'applications/metamta/future/PhabricatorTwilioFuture.php', 'PhabricatorTwitchAuthProvider' => 'applications/auth/provider/PhabricatorTwitchAuthProvider.php', 'PhabricatorTwitterAuthProvider' => 'applications/auth/provider/PhabricatorTwitterAuthProvider.php', 'PhabricatorTypeaheadApplication' => 'applications/typeahead/application/PhabricatorTypeaheadApplication.php', @@ -10608,6 +10609,7 @@ 'PhabricatorTriggerClockTestCase' => 'PhabricatorTestCase', 'PhabricatorTriggerDaemon' => 'PhabricatorDaemon', 'PhabricatorTrivialTestCase' => 'PhabricatorTestCase', + 'PhabricatorTwilioFuture' => 'FutureProxy', 'PhabricatorTwitchAuthProvider' => 'PhabricatorOAuth2AuthProvider', 'PhabricatorTwitterAuthProvider' => 'PhabricatorOAuth1AuthProvider', 'PhabricatorTypeaheadApplication' => 'PhabricatorApplication', diff --git a/src/applications/metamta/future/PhabricatorTwilioFuture.php b/src/applications/metamta/future/PhabricatorTwilioFuture.php new file mode 100644 --- /dev/null +++ b/src/applications/metamta/future/PhabricatorTwilioFuture.php @@ -0,0 +1,85 @@ +accountSID = $account_sid; + return $this; + } + + public function setAuthToken(PhutilOpaqueEnvelope $token) { + $this->authToken = $token; + return $this; + } + + public function setMethod($method, array $parameters) { + $this->method = $method; + $this->parameters = $parameters; + return $this; + } + + protected function getProxiedFuture() { + if (!$this->future) { + if ($this->accountSID === null) { + throw new PhutilInvalidStateException('setAccountSID'); + } + + if ($this->authToken === null) { + throw new PhutilInvalidStateException('setAuthToken'); + } + + if ($this->method === null || $this->parameters === null) { + throw new PhutilInvalidStateException('setMethod'); + } + + $path = urisprintf( + '/%s/Accounts/%s/%s', + '2010-04-01', + $this->accountSID, + $this->method); + + $uri = id(new PhutilURI('https://api.twilio.com/2010-04-01/accounts/')) + ->setPath($path); + + $data = $this->parameters; + + $future = id(new HTTPSFuture($uri, $data)) + ->setHTTPBasicAuthCredentials($this->accountSID, $this->authToken) + ->setMethod('POST') + ->addHeader('Accept', 'application/json'); + + $this->future = $future; + } + + return $this->future; + } + + protected function didReceiveResult($result) { + list($status, $body, $headers) = $result; + + if ($status->isError()) { + throw $status; + } + + try { + $data = phutil_json_decode($body); + } catch (PhutilJSONParserException $ex) { + throw new PhutilProxyException( + pht('Expected JSON response from Twilio.'), + $ex); + } + + return $data; + } + +}