示例#1
0
 /**
  * @param RequestInterface|EntityEnclosingRequestInterface $request
  * @param Credentials $credentials
  */
 public function signRequest($request, $credentials)
 {
     $request->setHeader('X-HMB-Signature-Method', self::DEFAULT_METHOD);
     $request->setHeader('X-HMB-Signature-Version', self::DEFAULT_SIGN_VERSION);
     $request->setHeader('X-HMB-TimeStamp', time());
     $contentMd5 = $request instanceof EntityEnclosingRequestInterface ? md5($request->getBody()) : '';
     if ($contentMd5) {
         $request->setHeader('Content-MD5', $contentMd5);
     }
     $sign = array();
     $sign[] = strtoupper($request->getMethod());
     $sign[] = $request->getHost();
     if ($request->getHeader('Content-MD5')) {
         $sign[] = $request->getHeader('Content-MD5');
     }
     if ($request->getHeader('Content-Type')) {
         $sign[] = $request->getHeader('Content-Type');
     }
     $sign[] = $request->getHeader('X-HMB-Signature-Method');
     $sign[] = $request->getHeader('X-HMB-Signature-Version');
     $sign[] = $request->getHeader('X-HMB-TimeStamp');
     if ($request->getHeader('X-HMB-User-Session-Token')) {
         $sign[] = $request->getHeader('X-HMB-User-Session-Token');
     }
     $sign[] = $request->getQuery(true) ? $request->getPath() . '?' . $request->getQuery(true) : $request->getPath();
     $signature = base64_encode(hash_hmac(strtolower($request->getHeader('X-HMB-Signature-Method')), implode("\n", $sign), $credentials->getSecret()));
     $request->setHeader('Authorization', sprintf('%s %s:%s', self::AUTHORIZATION_SCHME, $credentials->getKey(), $signature));
 }
 /**
  * Helper method to extract the items from a request object for a BatchWriteItem operation
  *
  * @param EntityEnclosingRequestInterface $request
  *
  * @return array
  */
 private function extractItemsFromRequestObject(EntityEnclosingRequestInterface $request)
 {
     $items = json_decode((string) $request->getBody(), true);
     return $this->convertResultsToUnprocessedRequests($items['RequestItems'] ?: array());
 }
 /**
  * Handles exceptions caused by the request being too large (over 1 MB). The
  * response will have a status code of 413. In this case the batch should be
  * split up into smaller batches and retried.
  *
  * @param EntityEnclosingRequestInterface   $request             The failed request
  * @param UnprocessedWriteRequestsException $unprocessedRequests Collection of unprocessed items
  */
 protected function retryLargeRequest(EntityEnclosingRequestInterface $request, UnprocessedWriteRequestsException $unprocessedRequests)
 {
     // Collect the items out from the request object
     $items = json_decode($request->getBody(true), true);
     $items = $this->convertResultsToUnprocessedRequests($items['RequestItems']);
     // Divide batch into smaller batches and transfer them via recursion
     // NOTE: Dividing the batch into 3 (instead of 2) batches resulted in less recursion during testing
     if ($items) {
         $newBatches = array_chunk($items, ceil(count($items) / 3));
         foreach ($newBatches as $newBatch) {
             $this->performTransfer($newBatch, $unprocessedRequests);
         }
     }
 }