/** * Send a request to the Recombee API * @param Requests\Request $request Request that will be sent * @throws Exceptions\ResponseException ResponseException if the request fails (a status different from 200 or 201 is returned) * @throws Exceptions\ApiTimeoutException ApiTimeoutException if the request takes too long */ public function send(Requests\Request $request) { $this->request = $request; $path = Util::sliceDbName($request->getPath()); $request_url = $path . $this->paramsToUrl($request->getQueryParameters()); $signed_url = $this->signUrl($request_url); $protocol = $request->getEnsureHttps() ? 'https' : $this->protocol; $uri = $protocol . '://' . $this->base_uri . $signed_url; $timeout = $request->getTimeout() / 1000; $result = null; try { switch ($request->getMethod()) { case Requests\Request::HTTP_GET: $result = $this->get($uri, $timeout); break; case Requests\Request::HTTP_PUT: $result = $this->put($uri, $timeout); break; case Requests\Request::HTTP_DELETE: $result = $this->delete($uri, $timeout); break; case Requests\Request::HTTP_POST: $json = json_encode($request->getBodyParameters(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $result = $this->post($uri, $timeout, $json); break; } } catch (\Requests_Exception $e) { if (strpos($e->getMessage(), 'cURL error 28') !== false) { throw new ApiTimeoutException($request); } throw $e; } return $result; }
protected function requestToBatchArray($req) { $path = '/' . Util::sliceDbName($req->getPath()); $bh = ['method' => $req->getMethod(), 'path' => $path]; $params = $req->getQueryParameters(); if ($req->getBodyParameters()) { $params = array_merge($params, $req->getBodyParameters()); } if ($params) { $bh['params'] = $params; } return $bh; }