Наследование: implements Psr\Http\Message\StreamInterface, use trait GuzzleHttp\Psr7\StreamDecoratorTrait
Пример #1
0
 public function testCanCreateEmptyStream()
 {
     $b = new MultipartStream();
     $boundary = $b->getBoundary();
     $this->assertSame("--{$boundary}--\r\n", $b->getContents());
     $this->assertSame(strlen($boundary) + 6, $b->getSize());
 }
Пример #2
0
 /**
  * @param string $method
  * @param string $uri
  * @param array  $body
  * @param array  $files
  * @param array  $headers
  *
  * @return \stdClass
  *
  * @throws GenericHTTPError
  * @throws InvalidCredentials
  * @throws MissingEndpoint
  * @throws MissingRequiredParameters
  */
 protected function send($method, $uri, $body = null, $files = [], array $headers = [])
 {
     $headers['User-Agent'] = Api::SDK_USER_AGENT . '/' . Api::SDK_VERSION;
     $headers['Authorization'] = 'Basic ' . base64_encode(sprintf('%s:%s', Api::API_USER, $this->apiKey));
     if (!empty($files)) {
         $body = new MultipartStream($files);
         $headers['Content-Type'] = 'multipart/form-data; boundary=' . $body->getBoundary();
     }
     $request = new Request($method, $this->getApiUrl($uri), $headers, $body);
     $response = $this->getHttpClient()->sendRequest($request);
     return $this->responseHandler($response);
 }
Пример #3
0
 /**
  * @param string $operation
  * @param DataPacket $data
  * @param array $files
  *
  * @return string
  */
 public function send($operation, DataPacket $data, $files = [], $decode = true)
 {
     if (!$this->ready()) {
         throw new \RuntimeException('Invalid parameters');
     }
     $data->addRequiredPair('Type', $operation);
     $data->addRequiredPair('terminalID', $this->terminal);
     $data->addRequiredPair('TerminalID', $this->terminal);
     if (!@$data->getRequiredItem('Password')) {
         $data->addRequiredPair('Password', $this->password);
     }
     $data->addRequiredPair('TimePacket', time());
     $data = $data->serializeToString();
     $data = base64_encode($data);
     $data = str_replace(['+', '/'], ['-', '_'], $data);
     $send = [['name' => 'destination', 'contents' => $data], ['name' => 'Hash', 'contents' => md5($data)]];
     if (!empty($files)) {
         foreach ($files as $name => &$file) {
             $send[] = ['name' => $name, 'contents' => $handle = fopen($file, 'r')];
             $file = [$handle, $file];
         }
     }
     $send = new MultipartStream($send);
     $request = new Request('POST', $this->host, ['Content-Type' => 'multipart/form-data; boundary=' . $send->getBoundary()], $send);
     $return = $this->getResponse($request, $decode);
     foreach ($files as $name => $file) {
         fclose($file[0]);
         unlink($file[1]);
     }
     return $return;
 }
 /**
  * Triggers the upload process.
  *
  * @return array
  */
 public function upload()
 {
     $multipartStream = new Psr7\MultipartStream([['name' => 'metadata', 'headers' => ['Content-Type' => 'application/json; charset=UTF-8'], 'contents' => json_encode($this->metadata)], ['name' => 'data', 'headers' => ['Content-Type' => $this->contentType], 'contents' => $this->data]], 'boundary');
     $headers = ['Content-Type' => 'multipart/related; boundary=boundary', 'Content-Length' => $multipartStream->getSize()];
     return json_decode($this->requestWrapper->send(new Request('POST', $this->uri, $headers, $multipartStream), $this->requestOptions)->getBody(), true);
 }
Пример #5
0
 /**
  * @param MethodDescriptionInterface $methodDescription
  * @param array $options
  *
  * @return $this|\GuzzleHttp\Psr7\MessageTrait|Request
  */
 public function createRequest(MethodDescriptionInterface $methodDescription, array $options = [])
 {
     $headers = [];
     $body = null;
     $method = $methodDescription->getMethod();
     if (in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE'])) {
         if ($data = $methodDescription->getRequestData($options)) {
             $format = $methodDescription->getRequestDataFormat();
             switch ($format) {
                 case MethodDescriptionInterface::FORMAT_JSON:
                     $serializer = $this->clientDescription->getSerializer();
                     $body = \GuzzleHttp\Psr7\stream_for($serializer($data, $format, $methodDescription->getRequestDataContext()));
                     $headers['Content-Type'] = 'application/json';
                     break;
                 case MethodDescriptionInterface::FORMAT_XML:
                     $serializer = $this->clientDescription->getSerializer();
                     $body = \GuzzleHttp\Psr7\stream_for($serializer($data, $format, $methodDescription->getRequestDataContext()));
                     $headers['Content-Type'] = 'application/xml';
                     break;
                 case MethodDescriptionInterface::FORMAT_MULTIPART_FORM_DATA:
                     $elements = [];
                     foreach ($data as $name => $value) {
                         if ($value instanceof File) {
                             $value = fopen($value->getRealPath(), 'r');
                         }
                         $elements[] = ['name' => $name, 'contents' => $value];
                     }
                     $body = new MultipartStream($elements);
                     $headers['Content-Type'] = 'multipart/form-data; boundary=' . $body->getBoundary();
                     break;
             }
         }
     }
     $request = new Request($methodDescription->getMethod(), $this->resolveUri($methodDescription->getResource(), $methodDescription->getQuery($options), $options), $headers, $body);
     return $request;
 }