Exemplo n.º 1
0
 /**
  * Performs a upload request
  *
  * @param  Request $request
  * @return An array attactment objects
  */
 public function upload(Request $request)
 {
     $this->emit('before.request', [$request, &$this->headers]);
     $files = $request->getFiles();
     if ($files->count() == 0) {
         throw new \UnexpectedValueException("Upload request must have at least one file.");
     }
     $options = ['headers' => $this->headers, 'cookies' => true, 'verify' => false];
     if (!empty($this->_authToken)) {
         $options['cookies'] = array('ZM_AUTH_TOKEN' => $this->_authToken);
     }
     $httpRequest = $this->_httpClient->createRequest('POST', $this->_location, $options);
     $postBody = $httpRequest->getBody();
     $postBody->setField('requestId', $request->getRequestId());
     foreach ($files as $file) {
         $postBody->addFile(new PostFile(basename($file), fopen($file, 'r')));
     }
     $httpRequest->setQuery(['fmt' => 'raw,extended']);
     try {
         $response = $this->_httpClient->send($httpRequest);
         $this->emit('after.request', [$response, $response->getHeaders()]);
     } catch (BadResponseException $ex) {
         if ($ex->hasResponse()) {
             $response = $ex->getResponse();
             $this->emit('after.request', [$response, $response->getHeaders()]);
         }
         throw $ex;
     }
     return $this->_parseResponse($response);
 }
Exemplo n.º 2
0
 public function testRequest()
 {
     $requestId = $this->faker->word;
     $file1 = $this->faker->word;
     $file2 = $this->faker->word;
     $request = new Request($requestId, [$file1]);
     $this->assertSame($requestId, $request->getRequestId());
     $this->assertSame([$file1], $request->getFiles()->all());
     $request = new Request('');
     $request->setRequestId($requestId)->setFiles([$file1])->addFile($file2);
     $this->assertSame($requestId, $request->getRequestId());
     $this->assertSame([$file1, $file2], $request->getFiles()->all());
 }