/** * 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.'); }
/** * 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; }
/** * 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())); } }
/** * Performs raw REST call * * @param ServiceRequestInterface $request * @return mixed * @throws CouldNotConnectException * @throws Exception */ protected function callRaw(ServiceRequestInterface $request) { $url = rtrim($request->getLocation(), '/') . '/' . $request->getMethod(); $httpMethod = $this->determineHttpMethod($request); $options = $this->prepareGuzzleOptions($request, $httpMethod); $this->beforeGuzzleCall($options); try { $response = $this->client->request($httpMethod, $url, $options); } catch (Exception $e) { // throw as CouldNotConnect if it is a guzzle error // or rethrow if unexpected if ($this->isGuzzleException($e)) { throw new CouldNotConnectException($e->getMessage(), $e->getCode(), $e); } throw $e; } $this->afterGuzzleCall($response); $this->responseInformation->setStatusCode($response->getStatusCode()); $this->responseInformation->setMessage($response->getReasonPhrase()); $this->responseInformation->setHeaders($response->getHeaders()); $responseBody = $response->getBody()->getContents(); event(new RestCallCompleted($url, isset($options['form_params']) ? $options['form_params'] : (isset($options['query']) ? $options['query'] : []), $this->sendResponseToEvent ? $responseBody : null)); return $responseBody; }