1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-22 14:52:41 +01:00

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
This commit is contained in:
epriestley 2019-01-01 19:04:30 -08:00
parent 3963c86ad5
commit 7e87d254ab
2 changed files with 87 additions and 0 deletions

View file

@ -4567,6 +4567,7 @@ phutil_register_library_map(array(
'PhabricatorTriggerClockTestCase' => 'infrastructure/daemon/workers/clock/__tests__/PhabricatorTriggerClockTestCase.php', 'PhabricatorTriggerClockTestCase' => 'infrastructure/daemon/workers/clock/__tests__/PhabricatorTriggerClockTestCase.php',
'PhabricatorTriggerDaemon' => 'infrastructure/daemon/workers/PhabricatorTriggerDaemon.php', 'PhabricatorTriggerDaemon' => 'infrastructure/daemon/workers/PhabricatorTriggerDaemon.php',
'PhabricatorTrivialTestCase' => 'infrastructure/testing/__tests__/PhabricatorTrivialTestCase.php', 'PhabricatorTrivialTestCase' => 'infrastructure/testing/__tests__/PhabricatorTrivialTestCase.php',
'PhabricatorTwilioFuture' => 'applications/metamta/future/PhabricatorTwilioFuture.php',
'PhabricatorTwitchAuthProvider' => 'applications/auth/provider/PhabricatorTwitchAuthProvider.php', 'PhabricatorTwitchAuthProvider' => 'applications/auth/provider/PhabricatorTwitchAuthProvider.php',
'PhabricatorTwitterAuthProvider' => 'applications/auth/provider/PhabricatorTwitterAuthProvider.php', 'PhabricatorTwitterAuthProvider' => 'applications/auth/provider/PhabricatorTwitterAuthProvider.php',
'PhabricatorTypeaheadApplication' => 'applications/typeahead/application/PhabricatorTypeaheadApplication.php', 'PhabricatorTypeaheadApplication' => 'applications/typeahead/application/PhabricatorTypeaheadApplication.php',
@ -10608,6 +10609,7 @@ phutil_register_library_map(array(
'PhabricatorTriggerClockTestCase' => 'PhabricatorTestCase', 'PhabricatorTriggerClockTestCase' => 'PhabricatorTestCase',
'PhabricatorTriggerDaemon' => 'PhabricatorDaemon', 'PhabricatorTriggerDaemon' => 'PhabricatorDaemon',
'PhabricatorTrivialTestCase' => 'PhabricatorTestCase', 'PhabricatorTrivialTestCase' => 'PhabricatorTestCase',
'PhabricatorTwilioFuture' => 'FutureProxy',
'PhabricatorTwitchAuthProvider' => 'PhabricatorOAuth2AuthProvider', 'PhabricatorTwitchAuthProvider' => 'PhabricatorOAuth2AuthProvider',
'PhabricatorTwitterAuthProvider' => 'PhabricatorOAuth1AuthProvider', 'PhabricatorTwitterAuthProvider' => 'PhabricatorOAuth1AuthProvider',
'PhabricatorTypeaheadApplication' => 'PhabricatorApplication', 'PhabricatorTypeaheadApplication' => 'PhabricatorApplication',

View file

@ -0,0 +1,85 @@
<?php
final class PhabricatorTwilioFuture extends FutureProxy {
private $future;
private $accountSID;
private $authToken;
private $method;
private $parameters;
public function __construct() {
parent::__construct(null);
}
public function setAccountSID($account_sid) {
$this->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;
}
}