@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add a parameterized Future for Twilio API calls

Summary: Ref T920. We currently embed the Twilio PHP API, but can replace it with about 100 lines of code and get a future-oriented interface as a bonus. Add a Future so we can move toward a simpler calling convention for the API.

Test Plan: Used this future to send SMS messages via the Twilio API.

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T920

Differential Revision: https://secure.phabricator.com/D19937

+87
+2
src/__phutil_library_map__.php
··· 4567 4567 'PhabricatorTriggerClockTestCase' => 'infrastructure/daemon/workers/clock/__tests__/PhabricatorTriggerClockTestCase.php', 4568 4568 'PhabricatorTriggerDaemon' => 'infrastructure/daemon/workers/PhabricatorTriggerDaemon.php', 4569 4569 'PhabricatorTrivialTestCase' => 'infrastructure/testing/__tests__/PhabricatorTrivialTestCase.php', 4570 + 'PhabricatorTwilioFuture' => 'applications/metamta/future/PhabricatorTwilioFuture.php', 4570 4571 'PhabricatorTwitchAuthProvider' => 'applications/auth/provider/PhabricatorTwitchAuthProvider.php', 4571 4572 'PhabricatorTwitterAuthProvider' => 'applications/auth/provider/PhabricatorTwitterAuthProvider.php', 4572 4573 'PhabricatorTypeaheadApplication' => 'applications/typeahead/application/PhabricatorTypeaheadApplication.php', ··· 10608 10609 'PhabricatorTriggerClockTestCase' => 'PhabricatorTestCase', 10609 10610 'PhabricatorTriggerDaemon' => 'PhabricatorDaemon', 10610 10611 'PhabricatorTrivialTestCase' => 'PhabricatorTestCase', 10612 + 'PhabricatorTwilioFuture' => 'FutureProxy', 10611 10613 'PhabricatorTwitchAuthProvider' => 'PhabricatorOAuth2AuthProvider', 10612 10614 'PhabricatorTwitterAuthProvider' => 'PhabricatorOAuth1AuthProvider', 10613 10615 'PhabricatorTypeaheadApplication' => 'PhabricatorApplication',
+85
src/applications/metamta/future/PhabricatorTwilioFuture.php
··· 1 + <?php 2 + 3 + final class PhabricatorTwilioFuture extends FutureProxy { 4 + 5 + private $future; 6 + private $accountSID; 7 + private $authToken; 8 + private $method; 9 + private $parameters; 10 + 11 + public function __construct() { 12 + parent::__construct(null); 13 + } 14 + 15 + public function setAccountSID($account_sid) { 16 + $this->accountSID = $account_sid; 17 + return $this; 18 + } 19 + 20 + public function setAuthToken(PhutilOpaqueEnvelope $token) { 21 + $this->authToken = $token; 22 + return $this; 23 + } 24 + 25 + public function setMethod($method, array $parameters) { 26 + $this->method = $method; 27 + $this->parameters = $parameters; 28 + return $this; 29 + } 30 + 31 + protected function getProxiedFuture() { 32 + if (!$this->future) { 33 + if ($this->accountSID === null) { 34 + throw new PhutilInvalidStateException('setAccountSID'); 35 + } 36 + 37 + if ($this->authToken === null) { 38 + throw new PhutilInvalidStateException('setAuthToken'); 39 + } 40 + 41 + if ($this->method === null || $this->parameters === null) { 42 + throw new PhutilInvalidStateException('setMethod'); 43 + } 44 + 45 + $path = urisprintf( 46 + '/%s/Accounts/%s/%s', 47 + '2010-04-01', 48 + $this->accountSID, 49 + $this->method); 50 + 51 + $uri = id(new PhutilURI('https://api.twilio.com/2010-04-01/accounts/')) 52 + ->setPath($path); 53 + 54 + $data = $this->parameters; 55 + 56 + $future = id(new HTTPSFuture($uri, $data)) 57 + ->setHTTPBasicAuthCredentials($this->accountSID, $this->authToken) 58 + ->setMethod('POST') 59 + ->addHeader('Accept', 'application/json'); 60 + 61 + $this->future = $future; 62 + } 63 + 64 + return $this->future; 65 + } 66 + 67 + protected function didReceiveResult($result) { 68 + list($status, $body, $headers) = $result; 69 + 70 + if ($status->isError()) { 71 + throw $status; 72 + } 73 + 74 + try { 75 + $data = phutil_json_decode($body); 76 + } catch (PhutilJSONParserException $ex) { 77 + throw new PhutilProxyException( 78 + pht('Expected JSON response from Twilio.'), 79 + $ex); 80 + } 81 + 82 + return $data; 83 + } 84 + 85 + }