/** * @return Request */ protected function convertRequest(RequestInterface $request, array $options) { $artaxRequest = new Request(); $artaxRequest->setProtocol($request->getProtocolVersion()); $artaxRequest->setMethod($request->getMethod()); $artaxRequest->setUri((string) $request->getUri()); $artaxRequest->setAllHeaders($request->getHeaders()); $body = $request->getBody(); if ($body->getSize() === null || $body->getSize() > 0) { $body->rewind(); $artaxRequest->setBody(new PsrStreamIterator($body)); } return $artaxRequest; }
/** * @param $gauges Gauge[] * @param $counters Counter[] */ function send($gauges, $counters) { $client = new ArtaxClient(); $client->setAllOptions([ArtaxClient::OP_MS_CONNECT_TIMEOUT => 2000, ArtaxClient::OP_MS_TRANSFER_TIMEOUT => 3000]); $request = new Request(); $request->setUri("https://metrics-api.librato.com/v1/metrics"); $request->setProtocol('1.1'); $request->setMethod('POST'); $request->setHeader("Content-Type", "application/json"); $auth = base64_encode($this->libratoConfig->getLibratoUsername() . ':' . $this->libratoConfig->getLibratoKey()); $request->setHeader("Authorization", "Basic {$auth}"); $data = []; //working //"{"gauges":[{"name":"Queue.ImagickTaskQueue","value":0,"source":"test.phpimagick.com"}]}" if (count($gauges)) { $gaugeEntries = []; foreach ($gauges as $gauge) { $arrayed = $gauge->convertToArray(); $gaugeEntries = array_merge($gaugeEntries, $arrayed); } $data["gauges"] = $gaugeEntries; } if (count($counters)) { $counterEntries = []; foreach ($counters as $counter) { $arrayed = $counter->convertToArray(); if (is_array($arrayed) == true) { $counterEntries = array_merge($counterEntries, $arrayed); } else { $counterEntries[] = $arrayed; } } $data["counters"] = $counterEntries; } $body = json_encode($data); $request->setBody($body); var_dump($body); try { $promise = $client->request($request); /** @var $response \Amp\Artax\Response */ $response = \Amp\wait($promise); echo "Status " . $response->getStatus() . "\n"; echo $response->getBody(); } catch (SocketException $se) { echo "Artax\\SocketExeption" . $se->getMessage(); } }
private function normalizeRequestMethod(Request $request) { if (!($method = $request->getMethod())) { $request->setMethod('GET'); } }
/** * Helper method to facilitate json rpc requests using the Amp\Artax client * * @param string $method The rpc method to call * @param array $params Associative array of rpc method arguments to send in the header (not auth arguments) * * @throws HTTPException When something goes wrong with the HTTP call * * @return Response The HTTP response containing headers / body ready for validation / parsing */ private function performRPCRequest($method, array $params) { $client = $this->client; $request = new Request(); if (empty($this->sessionCookie)) { $request->setUri(sprintf('%s:%s/json', $this->connectionArgs['host'], $this->connectionArgs['port'])); $request->setMethod('POST'); $request->setAllHeaders(array('Content-Type' => 'application/json; charset=utf-8')); $request->setBody(json_encode(array('method' => self::METHOD_AUTH, 'params' => array($this->connectionArgs['password']), 'id' => rand()))); $promise = $client->request($request); /** @var Amp\Artax\Response $response */ $response = Amp\wait($promise); if ($response->hasHeader('Set-Cookie')) { $cookieHeader = $response->getHeader('Set-Cookie'); preg_match_all('/_session_id=(.*?);/', $cookieHeader[0], $matches); $this->sessionCookie = isset($matches[0][0]) ? $matches[0][0] : ''; } else { throw new HTTPException("Response from torrent client did not return a Set-Cookie header"); } } $request = new Request(); $request->setUri(sprintf('%s:%s/json', $this->connectionArgs['host'], $this->connectionArgs['port'])); $request->setMethod('POST'); $request->setAllHeaders(array('Content-Type' => 'application/json; charset=utf-8', 'Cookie' => $this->sessionCookie)); $body = array('method' => $method, 'params' => $params, 'id' => rand()); $request->setBody(json_encode($body)); $promise = $client->request($request); /** @var Amp\Artax\Response $response */ $response = Amp\wait($promise); if ($response->getStatus() === 200) { $body = $response->getBody(); $isJson = function () use($body) { json_decode($body); return json_last_error() === JSON_ERROR_NONE; }; if ($isJson()) { return $response; } else { throw new HTTPException(sprintf('"%s" did not get back a JSON response body, got "%s" instead', $method, print_r($response->getBody(), true))); } } else { throw new HTTPException(sprintf('"%s" expected 200 response, got "%s" instead, reason: "%s"', $method, $response->getStatus(), $response->getReason())); } }
public function testGetAndSetMethod() { $request = new Request(); $request->setMethod('GET'); $this->assertEquals('GET', $request->getMethod()); }