protected function getProxiedFuture() { if (!$this->future) { $params = $this->params; if (!$this->action) { throw new Exception(pht('You must %s!', 'setRawGitHubQuery()')); } if (!$this->accessToken) { throw new Exception(pht('You must %s!', 'setAccessToken()')); } $uri = new PhutilURI('https://api.github.com/'); $uri->setPath('/' . ltrim($this->action, '/')); $future = new HTTPSFuture($uri); $future->setData($this->params); $future->addHeader('Authorization', 'token ' . $this->accessToken); // NOTE: GitHub requires a 'User-Agent' header. $future->addHeader('User-Agent', __CLASS__); $future->setMethod($this->method); foreach ($this->headers as $header) { list($key, $value) = $header; $future->addHeader($key, $value); } $this->future = $future; } return $this->future; }
protected function loadOAuthAccountData() { $uri = new PhutilURI('https://disqus.com/api/3.0/users/details.json'); $uri->setQueryParam('api_key', $this->getClientID()); $uri->setQueryParam('access_token', $this->getAccessToken()); $uri = (string) $uri; $future = new HTTPSFuture($uri); $future->setMethod('GET'); list($body) = $future->resolvex(); $data = json_decode($body, true); if (!is_array($data)) { throw new Exception("Expected valid JSON response from Disqus account data request, " . "got: " . $body); } return $data['response']; }
protected function loadOAuthAccountData() { $uri = new PhutilURI('https://disqus.com/api/3.0/users/details.json'); $uri->setQueryParam('api_key', $this->getClientID()); $uri->setQueryParam('access_token', $this->getAccessToken()); $uri = (string) $uri; $future = new HTTPSFuture($uri); $future->setMethod('GET'); list($body) = $future->resolvex(); try { $data = phutil_json_decode($body); return $data['response']; } catch (PhutilJSONParserException $ex) { throw new PhutilProxyException(pht('Expected valid JSON response from Disqus account data request.'), $ex); } }
protected function getProxiedFuture() { if (!$this->future) { $params = $this->params; if (!$this->action) { throw new Exception(pht('You must %s!', 'setRawAsanaQuery()')); } if (!$this->accessToken) { throw new Exception(pht('You must %s!', 'setAccessToken()')); } $uri = new PhutilURI('https://app.asana.com/'); $uri->setPath('/api/1.0/' . ltrim($this->action, '/')); $future = new HTTPSFuture($uri); $future->setData($this->params); $future->addHeader('Authorization', 'Bearer ' . $this->accessToken); $future->setMethod($this->method); $this->future = $future; } return $this->future; }
protected function getProxiedFuture() { if (!$this->future) { $params = $this->params; if (!$this->action) { throw new Exception(pht('You must %s!', 'setRawSlackQuery()')); } if (!$this->accessToken) { throw new Exception(pht('You must %s!', 'setAccessToken()')); } $uri = new PhutilURI('https://slack.com/'); $uri->setPath('/api/' . $this->action); $uri->setQueryParam('token', $this->accessToken); $future = new HTTPSFuture($uri); $future->setData($this->params); $future->setMethod($this->method); $this->future = $future; } return $this->future; }
protected function getProxiedFuture() { if (!$this->future) { $params = $this->params; if (!$this->action) { throw new Exception("You must setRawWordPressQuery()!"); } if (!$this->accessToken) { throw new Exception("You must setAccessToken()!"); } $uri = new PhutilURI('https://public-api.wordpress.com/'); $uri->setPath('/rest/v1/' . ltrim($this->action, '/')); $future = new HTTPSFuture($uri); $future->setData($this->params); $future->setMethod($this->method); // NOTE: This is how WordPress.com REST API authenticates $future->addHeader('Authorization', 'Bearer ' . $this->accessToken); $this->future = $future; } return $this->future; }
public function callMethod($method, array $params) { $meta = array(); if ($this->sessionKey) { $meta['sessionKey'] = $this->sessionKey; } if ($this->connectionID) { $meta['connectionID'] = $this->connectionID; } if ($method == 'conduit.connect') { $certificate = idx($params, 'certificate'); if ($certificate) { $token = time(); $params['authToken'] = $token; $params['authSignature'] = sha1($token . $certificate); } unset($params['certificate']); } if ($meta) { $params['__conduit__'] = $meta; } $uri = id(clone $this->uri)->setPath('/api/' . $method); $data = array('params' => json_encode($params), 'output' => 'json', '__conduit__' => true); // Always use the cURL-based HTTPSFuture, for proxy support and other // protocol edge cases that HTTPFuture does not support. $core_future = new HTTPSFuture($uri, $data); $core_future->setMethod('POST'); $core_future->setTimeout($this->timeout); if ($this->username !== null) { $core_future->setHTTPBasicAuthCredentials($this->username, $this->password); } $conduit_future = new ConduitFuture($core_future); $conduit_future->setClient($this, $method); $conduit_future->beginProfile($data); $conduit_future->isReady(); return $conduit_future; }
protected function getProxiedFuture() { if (!$this->future) { $params = $this->params; if (!$this->action) { throw new Exception('You must setRawTwitchQuery()!'); } if (!$this->accessToken) { throw new Exception('You must setAccessToken()!'); } $uri = new PhutilURI('https://api.twitch.tv/'); $uri->setPath('/kraken/' . ltrim($this->action, '/')); $uri->setQueryParam('oauth_token', $this->accessToken); $future = new HTTPSFuture($uri); $future->setData($this->params); $future->setMethod($this->method); // NOTE: This is how the Twitch API is versioned. $future->addHeader('Accept', 'application/vnd.twitchtv.2+json'); // NOTE: This is required to avoid rate limiting. $future->addHeader('Client-ID', $this->clientID); $this->future = $future; } return $this->future; }
private function executeRequest($path, array $data, $is_write = false) { $uri = new PhutilURI($this->uri); $data = json_encode($data); $uri->setPath($path); $future = new HTTPSFuture($uri, $data); if ($is_write) { $future->setMethod('PUT'); } if ($this->getTimeout()) { $future->setTimeout($this->getTimeout()); } list($body) = $future->resolvex(); if ($is_write) { return null; } $body = json_decode($body, true); if (!is_array($body)) { throw new Exception("elasticsearch server returned invalid JSON!"); } return $body; }
public function send() { $user = PhabricatorEnv::getEnvConfig('sendgrid.api-user'); $key = PhabricatorEnv::getEnvConfig('sendgrid.api-key'); if (!$user || !$key) { throw new Exception("Configure 'sendgrid.api-user' and 'sendgrid.api-key' to use " . "SendGrid for mail delivery."); } $params = array(); $ii = 0; foreach (idx($this->params, 'tos', array()) as $to) { $params['to[' . $ii++ . ']'] = $to; } $params['subject'] = idx($this->params, 'subject'); $params['text'] = idx($this->params, 'body'); if (idx($this->params, 'html-body')) { $params['html'] = idx($this->params, 'html-body'); } $params['from'] = idx($this->params, 'from'); if (idx($this->params, 'from-name')) { $params['fromname'] = $this->params['from-name']; } if (idx($this->params, 'reply-to')) { $replyto = $this->params['reply-to']; // Pick off the email part, no support for the name part in this API. $params['replyto'] = $replyto[0]['email']; } foreach (idx($this->params, 'files', array()) as $name => $data) { $params['files[' . $name . ']'] = $data; } $headers = idx($this->params, 'headers', array()); // See SendGrid Support Ticket #29390; there's no explicit REST API support // for CC right now but it works if you add a generic "Cc" header. // // SendGrid said this is supported: // "You can use CC as you are trying to do there [by adding a generic // header]. It is supported despite our limited documentation to this // effect, I am glad you were able to figure it out regardless. ..." if (idx($this->params, 'ccs')) { $headers[] = array('Cc', implode(', ', $this->params['ccs'])); } if ($headers) { // Convert to dictionary. $headers = ipull($headers, 1, 0); $headers = json_encode($headers); $params['headers'] = $headers; } $params['api_user'] = $user; $params['api_key'] = $key; $future = new HTTPSFuture('https://sendgrid.com/api/mail.send.json', $params); $future->setMethod('POST'); list($body) = $future->resolvex(); $response = json_decode($body, true); if (!is_array($response)) { throw new Exception("Failed to JSON decode response: {$body}"); } if ($response['message'] !== 'success') { $errors = implode(';', $response['errors']); throw new Exception("Request failed with errors: {$errors}."); } return true; }
private function makeTokenRequest(array $params) { $uri = $this->getTokenBaseURI(); $query_data = array('client_id' => $this->getClientID(), 'client_secret' => $this->getClientSecret()->openEnvelope(), 'redirect_uri' => $this->getRedirectURI()) + $params; $future = new HTTPSFuture($uri, $query_data); $future->setMethod('POST'); list($body) = $future->resolvex(); $data = $this->readAccessTokenResponse($body); if (isset($data['expires_in'])) { $data['expires_epoch'] = $data['expires_in']; } else { if (isset($data['expires'])) { $data['expires_epoch'] = $data['expires']; } } // If we got some "expires" value back, interpret it as an epoch timestamp // if it's after the year 2010 and as a relative number of seconds // otherwise. if (isset($data['expires_epoch'])) { if ($data['expires_epoch'] < 60 * 60 * 24 * 365 * 40) { $data['expires_epoch'] += time(); } } if (isset($data['error'])) { throw new Exception('Access token error: ' . $data['error']); } return $data; }
private function executeRequest($path, array $data, $method = 'GET') { $uri = new PhutilURI($this->uri); $uri->setPath($this->index); $uri->appendPath($path); $data = json_encode($data); $future = new HTTPSFuture($uri, $data); if ($method != 'GET') { $future->setMethod($method); } if ($this->getTimeout()) { $future->setTimeout($this->getTimeout()); } list($body) = $future->resolvex(); if ($method != 'GET') { return null; } try { return phutil_json_decode($body); } catch (PhutilJSONParserException $ex) { throw new PhutilProxyException(pht('ElasticSearch server returned invalid JSON!'), $ex); } }
public function send() { $key = PhabricatorEnv::getEnvConfig('mailgun.api-key'); $domain = PhabricatorEnv::getEnvConfig('mailgun.domain'); $params = array(); $params['to'] = implode(', ', idx($this->params, 'tos', array())); $params['subject'] = idx($this->params, 'subject'); $params['text'] = idx($this->params, 'body'); if (idx($this->params, 'html-body')) { $params['html'] = idx($this->params, 'html-body'); } $from = idx($this->params, 'from'); if (idx($this->params, 'from-name')) { $params['from'] = "{$this->params['from-name']} <{$from}>"; } else { $params['from'] = $from; } if (idx($this->params, 'reply-to')) { $replyto = $this->params['reply-to']; $params['h:reply-to'] = implode(', ', $replyto); } if (idx($this->params, 'ccs')) { $params['cc'] = implode(', ', $this->params['ccs']); } foreach (idx($this->params, 'headers', array()) as $header) { list($name, $value) = $header; $params['h:' . $name] = $value; } $future = new HTTPSFuture("https://*****:*****@api.mailgun.net/v2/{$domain}/messages", $params); $future->setMethod('POST'); foreach ($this->attachments as $attachment) { $future->attachFileData('attachment', $attachment['data'], $attachment['name'], $attachment['type']); } list($body) = $future->resolvex(); $response = json_decode($body, true); if (!is_array($response)) { throw new Exception("Failed to JSON decode response: {$body}"); } if (!idx($response, 'id')) { $message = $response['message']; throw new Exception("Request failed with errors: {$message}."); } return true; }
#!/usr/bin/env php <?php $root = dirname(dirname(dirname(__FILE__))); require_once $root . '/scripts/__init_script__.php'; $args = new PhutilArgumentParser($argv); $args->parseStandardArguments(); $args->parse(array(array('name' => 'attach', 'param' => 'file', 'help' => pht('Attach a file to the request.')), array('name' => 'url', 'wildcard' => true))); $uri = $args->getArg('url'); if (count($uri) !== 1) { throw new PhutilArgumentUsageException("Specify exactly one URL to retrieve."); } $uri = head($uri); $method = 'GET'; $data = ''; $timeout = 30; $future = new HTTPSFuture($uri, $data); $future->setMethod($method); $future->setTimeout($timeout); $attach_file = $args->getArg('attach'); if ($attach_file !== null) { $future->attachFileData('file', Filesystem::readFile($attach_file), basename($attach_file), Filesystem::getMimeType($attach_file)); } print_r($future->resolve());
private function retrieveAccessToken(PhabricatorOAuthProvider $provider) { $request = $this->getRequest(); $token = $request->getStr('confirm_token'); if ($token) { $this->tokenExpires = $request->getInt('expires'); $this->accessToken = $token; $this->oauthState = $request->getStr('state'); return null; } $client_id = $provider->getClientID(); $client_secret = $provider->getClientSecret(); $redirect_uri = $provider->getRedirectURI(); $auth_uri = $provider->getTokenURI(); $code = $request->getStr('code'); $query_data = array('client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri, 'code' => $code) + $provider->getExtraTokenParameters(); $future = new HTTPSFuture($auth_uri, $query_data); $future->setMethod('POST'); try { list($response) = $future->resolvex(); } catch (Exception $ex) { return $this->buildErrorResponse(new PhabricatorOAuthFailureView()); } $data = $provider->decodeTokenResponse($response); $token = idx($data, 'access_token'); if (!$token) { return $this->buildErrorResponse(new PhabricatorOAuthFailureView()); } $this->tokenExpires = $provider->getTokenExpiryFromArray($data); $this->accessToken = $token; $this->oauthState = $request->getStr('state'); return null; }