/**
  * Sends a PUT-Request to google drive and parses the response,
  * setting the appropiate variables from the response()
  *
  * @param Request $httpRequest the Reuqest which will be send
  *
  * @return false|mixed false when the upload is unfinished or the decoded http response
  *
  */
 private function makePutRequest(Request $httpRequest)
 {
     if ($this->client->getClassConfig("Request", "enable_gzip_for_uploads")) {
         $httpRequest->enableGzip();
     } else {
         $httpRequest->disableGzip();
     }
     $response = $this->client->getIo()->makeRequest($httpRequest);
     $response->setExpectedClass($this->request->getExpectedClass());
     $code = $response->getResponseHttpCode();
     $this->httpResultCode = $code;
     if (308 == $code) {
         // Track the amount uploaded.
         $range = explode('-', $response->getResponseHeader('range'));
         $this->progress = $range[1] + 1;
         // Allow for changing upload URLs.
         $location = $response->getResponseHeader('location');
         if ($location) {
             $this->resumeUri = $location;
         }
         // No problems, but upload not complete.
         return false;
     } else {
         return REST::decodeHttpResponse($response, $this->client);
     }
 }
Example #2
0
 /**
  * Decode an HTTP Response.
  * @static
  * @throws Exception
  * @param Request $response The http response to be decoded.
  * @param Client $client
  * @return mixed|null
  */
 public static function decodeHttpResponse($response, Client $client = null)
 {
     $code = $response->getResponseHttpCode();
     $body = $response->getResponseBody();
     $decoded = null;
     if (intVal($code) >= 300) {
         $decoded = json_decode($body, true);
         $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl();
         if (isset($decoded['error']) && isset($decoded['error']['message']) && isset($decoded['error']['code'])) {
             // if we're getting a json encoded error definition, use that instead of the raw response
             // body for improved readability
             $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}";
         } else {
             $err .= ": ({$code}) {$body}";
         }
         $errors = null;
         // Specific check for APIs which don't return error details, such as Blogger.
         if (isset($decoded['error']) && isset($decoded['error']['errors'])) {
             $errors = $decoded['error']['errors'];
         }
         $map = null;
         if ($client) {
             $client->getLogger()->error($err, array('code' => $code, 'errors' => $errors));
             $map = $client->getClassConfig('Exception', 'retry_map');
         }
         throw new Exception($err, $code, null, $errors, $map);
     }
     // Only attempt to decode the response, if the response code wasn't (204) 'no content'
     if ($code != '204') {
         if ($response->getExpectedRaw()) {
             return $body;
         }
         $decoded = json_decode($body, true);
         if ($decoded === null || $decoded === "") {
             $error = "Invalid json in service response: {$body}";
             if ($client) {
                 $client->getLogger()->error($error);
             }
             throw new Exception($error);
         }
         if ($response->getExpectedClass()) {
             $class = $response->getExpectedClass();
             $decoded = new $class($decoded);
         }
     }
     return $decoded;
 }