Exemplo n.º 1
0
 /**
  * Create the OVH signature
  * @param Request $request
  * @param int     $time
  *
  * @return string
  */
 protected function getSignature(Request $request, $time)
 {
     $method = $request->getMethod();
     $url = $request->getUrl();
     $body = $request->getBody();
     $schema = $this->applicationSecret . '+' . $this->consumerKey . '+' . $method . '+' . $url . '+' . $body . '+' . $time;
     return '$1$' . sha1($schema);
 }
Exemplo n.º 2
0
 public function testConstructorInitializesMessage()
 {
     $r = new Request('PUT', '/test', ['test' => '123'], Stream::factory('foo'));
     $this->assertEquals('PUT', $r->getMethod());
     $this->assertEquals('/test', $r->getUrl());
     $this->assertEquals('123', $r->getHeader('test'));
     $this->assertEquals('foo', $r->getBody());
 }
 /**
  * Send asynchronous guzzle request
  *
  * @param Psr7Request $request
  * @param \Tebru\Retrofit\Http\Callback $callback
  * @return null
  */
 public function sendAsync(Psr7Request $request, Callback $callback)
 {
     $request = new Request($request->getMethod(), (string) $request->getUri(), $request->getHeaders(), $request->getBody(), ['future' => true]);
     /** @var FutureInterface $response */
     $response = $this->client->send($request);
     $this->promises[] = $response->then(function (ResponseInterface $response) {
         return new Psr7Response($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase());
     })->then($callback->success(), $callback->failure());
 }
Exemplo n.º 4
0
 public function testCanRemoveBodyBySettingToNullAndRemovesCommonBodyHeaders()
 {
     $m = new Request('GET', 'http://foo.com');
     $m->setBody(Stream::factory('foo'));
     $m->setHeader('Content-Length', 3)->setHeader('Transfer-Encoding', 'chunked');
     $m->setBody(null);
     $this->assertNull($m->getBody());
     $this->assertFalse($m->hasHeader('Content-Length'));
     $this->assertFalse($m->hasHeader('Transfer-Encoding'));
 }
 public function testProcess()
 {
     $client = $this->getClient();
     $data = 'foo';
     // Test data *only* uploads.
     $request = new Request('POST', 'http://www.example.com');
     $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', $data, false);
     $this->assertEquals($data, (string) $request->getBody());
     // Test resumable (meta data) - we want to send the metadata, not the app data.
     $request = new Request('POST', 'http://www.example.com');
     $reqData = json_encode("hello");
     $request->setBody(Stream::factory($reqData));
     $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', $data, true);
     $this->assertEquals(json_decode($reqData), (string) $request->getBody());
     // Test multipart - we are sending encoded meta data and post data
     $request = new Request('POST', 'http://www.example.com');
     $reqData = json_encode("hello");
     $request->setBody(Stream::factory($reqData));
     $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', $data, false);
     $this->assertContains($reqData, (string) $request->getBody());
     $this->assertContains(base64_encode($data), (string) $request->getBody());
 }
Exemplo n.º 6
0
 /**
  * Making request using Guzzle
  *
  * @param string $method Type of request, either POST, GET, PUT or DELETE
  * @param string $endpoint The operation / task for API
  * @param array $data The parameter need to be passed
  * @param array $options The options like header, body, etc
  *
  * @return EntityBodyInterface|string
  * @throws \Exception
  */
 private function sendRequest($method, $endpoint, array $data = array(), array $options = array())
 {
     $uri = $this->buildUri($endpoint);
     if ($method === "GET" || $method === "PUT") {
         $uri = $this->buildUri($endpoint, $data);
     } elseif (count($data)) {
         $options['body'] = $data;
     }
     $this->request = $this->client->createRequest($method, $uri, $options);
     $this->response = $this->client->send($this->request);
     if ($this->response->getStatusCode() >= 400) {
         $bt = debug_backtrace();
         $caller = $bt[2];
         if (isset($caller['class']) && $caller['class'] === get_class(new StacklaModel())) {
             $json = (string) $this->response->getBody();
             if (JsonValidator::validate($json, true)) {
                 $content = json_decode($json, true);
                 if (isset($content['errors'])) {
                     $caller['object']->fromArray($content);
                 }
             }
         }
         if ($this->logger) {
             $this->logger->addError('-> REQUEST [' . $this->request->getMethod() . ' - ' . $this->request->getUrl() . "]", array($this->request->getMethod() !== "GET" ? (string) $this->request->getBody() : ""));
             $this->logger->addError('<- RESPONSE [' . $this->response->getStatusCode() . ':' . $this->response->getReasonPhrase() . "]", array((string) $this->response->getBody()));
         }
     } else {
         if ($this->logger) {
             $this->logger->addInfo('-> REQUEST [' . $this->request->getMethod() . ' - ' . $this->request->getUrl() . "]", array($this->request->getMethod() !== "GET" ? (string) $this->request->getBody() : ""));
             $this->logger->addInfo('<- RESPONSE [' . $this->response->getStatusCode() . ':' . $this->response->getReasonPhrase() . "]", array($this->response->json()));
         }
     }
     $statusCode = $this->response->getStatusCode();
     switch ($statusCode) {
         case 200:
             return (string) $this->response->getBody();
         case 400:
             throw ApiException::create(sprintf("Server return %s error code. Bad request: The request could not be understood. %s", $this->response->getStatusCode(), (string) $this->response->getBody()), $statusCode, (string) $this->response->getBody());
         case 401:
             throw ApiException::create(sprintf("Server return %s error code. Unauthorized: Authentication credentials invalid or not authorised to access resource", $this->response->getStatusCode()), $statusCode, (string) $this->response->getBody());
         case 403:
             throw ApiException::create(sprintf("\n                  Server return %s error code. Rate limit exceeded: Too many requests in the current time window", $this->response->getStatusCode()), $statusCode, (string) $this->response->getBody());
         case 404:
             throw ApiException::create(sprintf("Server return %s error code. Invalid resource: Invalid resource specified or resource not found", $this->response->getStatusCode()), $statusCode, (string) $this->response->getBody());
         default:
             throw ApiException::create(sprintf("Server return %s error code.Server error: An error on the server prohibited a successful response; please contact support. %s", $this->response->getStatusCode(), (string) $this->response->getBody()), $statusCode, (string) $this->response->getBody());
     }
 }
 /**
  * Announces HTTP request to Wildfire channel
  *
  * @param GuzzleHttp\Message\Request
  * @param GuzzleHttp\Message\Response   when dealing with error events, response may not be populated
  * @param float $elapsed
  */
 public function publishRequest(Request $request, Response $response = null, $elapsed = 0)
 {
     $request_body = $request->getBody();
     $request_preview = '';
     if ($request_body) {
         $request_body->seek(0);
         // rewind the cursor in case a read was already done
         $request_preview = $request_body->read($this->_preview_length);
         $request_body->seek(0);
         // rewind the cursor, so subsequent reads are not affected
     }
     // Ensure response is populated before extracting body from it
     $response_body = $response ? $response->getBody() : '';
     $response_preview = '';
     if ($response_body) {
         $response_body->seek(0);
         // rewind the cursor, in case a read was already done
         $response_preview = $response_body->read($this->_preview_length);
         $response_body->seek(0);
         // rewind the cursor, so subsequent reads are not affected
     }
     $phrase = $response ? $response->getReasonPhrase() : self::ERROR_NO_RESPONSE;
     $table = [];
     $table[] = ['Key', 'Value'];
     $table[] = ['Phrase', $phrase];
     $table[] = ['Host', $request->getHost()];
     $table[] = ['Protocol', $request->getScheme()];
     $table[] = ['User Agent', $request->getHeader('User-Agent')];
     $table[] = ['Request', $request_preview];
     $table[] = ['Response', $response_preview];
     if ($response && $response->getEffectiveUrl() != $request->getUrl()) {
         $table[] = ['Effective URL', $response->getEffectiveUrl()];
     }
     $elapsed = number_format($elapsed, 4);
     $status = $response ? $response->getStatusCode() : 0;
     $message = sprintf("%s <%s> (%d) %s (%f)s", $this->_remote_prefix, $request->getMethod(), $status, $request->getUrl(), $elapsed);
     $this->_client->table($message, $table);
 }
Exemplo n.º 8
0
 public function testProperBodyReading()
 {
     $request = new Request('PUT', 'example.local', [], Stream::factory('foo=bar&hello=world'));
     $request->getBody()->getContents();
     $curl = $this->curlFormatter->format($request);
     $this->assertContains("-d 'foo=bar&hello=world'", $curl);
     $this->assertContains("-X PUT", $curl);
 }
Exemplo n.º 9
0
 /**
  * {@inheritDoc}
  */
 public function getBody()
 {
     return $this->request instanceof MessageInterface ? $this->request->getBody() : '';
 }