Example #1
0
 /**
  * Builds an returns local file path from current request
  *
  * Path may be a combination of the location and the method
  * location may be the full file path and method may then be empty
  *
  * @param ServiceRequestInterface $request
  * @return string
  * @throws CouldNotConnectException
  */
 protected function makeFilePathFromRequest(ServiceRequestInterface $request)
 {
     $location = $request->getLocation();
     $method = $request->getMethod();
     if (!empty($location) && !empty($method)) {
         return rtrim($location(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $method();
     }
     if (!empty($location)) {
         return $location;
     }
     if (!empty($method)) {
         return $method;
     }
     throw new CouldNotConnectException('No path given for FileService. Set a location and/or method to build a valid path.');
 }
 /**
  * Prepares and returns guzzle options array for next call
  *
  * @param ServiceRequestInterface $request
  * @param null|string             $httpMethod
  * @return array
  */
 protected function prepareGuzzleOptions(ServiceRequestInterface $request, $httpMethod = null)
 {
     if ($this->isMultipartFormRequest()) {
         $this->enableMultipart();
     } else {
         $this->disableMultipart();
     }
     $headers = $request->getHeaders() ?: [];
     if ($this->debugUser) {
         $headers = array_merge($headers, ['debug-user' => $this->debugUser]);
     } else {
         if (array_key_exists('debug-user', $headers)) {
             unset($headers['debug-user']);
         }
     }
     $request->setHeaders($headers);
     return parent::prepareGuzzleOptions($request, $httpMethod);
 }
Example #3
0
 /**
  * Performs raw REST call
  *
  * @param ServiceRequestInterface $request
  * @return mixed
  * @throws CouldNotConnectException
  */
 protected function callRaw(ServiceRequestInterface $request)
 {
     $url = rtrim($request->getLocation(), '/') . '/' . $request->getMethod();
     $curl = curl_init();
     if ($curl === false) {
         throw new CouldNotConnectException('cURL could not be initialized');
     }
     $credentials = $request->getCredentials();
     if ($this->basicAuth && !empty($credentials['name']) && !empty($credentials['password'])) {
         curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
         curl_setopt($curl, CURLOPT_USERPWD, $credentials['name'] . ":" . $credentials['password']);
     }
     $headers = $request->getHeaders();
     switch ($this->method) {
         case RestService::METHOD_PATCH:
         case RestService::METHOD_POST:
         case RestService::METHOD_PUT:
             curl_setopt($curl, CURLOPT_POST, true);
             curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($request->getBody() ?: []));
             $parameters = $request->getParameters();
             if (!empty($parameters)) {
                 $url .= '?' . http_build_query($request->getParameters());
             }
             break;
         case RestService::METHOD_GET:
             $parameters = $request->getbody();
             $url .= '?' . http_build_query($parameters ?: []);
             break;
             // default omitted on purpose
     }
     if (count($headers)) {
         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     }
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_USERAGENT, self::USER_AGENT);
     curl_setopt($curl, CURLOPT_URL, $url);
     $response = curl_exec($curl);
     if ($response === false) {
         throw new CouldNotConnectException(curl_error($curl), curl_errno($curl));
     }
     $this->responseInformation->setStatusCode(curl_getinfo($curl, CURLINFO_HTTP_CODE));
     curl_close($curl);
     event(new RestCallCompleted($url, isset($parameters) ? $parameters : [], $this->sendResponseToEvent ? $response : null));
     return $response;
 }
Example #4
0
 /**
  * Takes the current request and supplements it with the service's defaults
  * to merge them into a complete request.
  */
 protected function supplementRequestWithDefaults()
 {
     // properties to set only if they are empty
     if (empty($this->request->getLocation())) {
         $this->request->setLocation($this->defaults->getLocation());
     }
     if (empty($this->request->getMethod())) {
         $this->request->setMethod($this->defaults->getMethod());
     }
     if (empty($this->request->getParameters())) {
         $this->request->setParameters($this->defaults->getParameters());
     }
     if (empty($this->request->getBody())) {
         $this->request->setBody($this->defaults->getBody());
     }
     if ($this->credentialsAreEmpty($this->request->getCredentials()) && !$this->credentialsAreEmpty($this->defaults->getCredentials())) {
         $this->request->setCredentials($this->defaults->getCredentials()['name'], $this->defaults->getCredentials()['password'], $this->defaults->getCredentials()['domain']);
     }
     // properties to expand
     if (!empty($this->defaults->getHeaders())) {
         $this->request->setHeaders(array_merge($this->request->getHeaders(), $this->defaults->getHeaders()));
     }
 }
Example #5
0
 /**
  * Returns HTTP method to use based on request & default
  *
  * @param ServiceRequestInterface|ServiceRestRequest $request
  * @return string
  */
 protected function determineHttpMethod(ServiceRequestInterface $request)
 {
     // use method set in request, or fall back to default
     return $request->getHttpMethod() ?: $this->httpMethod;
 }