Skip to content
This repository was archived by the owner on Apr 5, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/main/php/webservices/rest/RestClient.class.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php namespace webservices\rest;

use io\IOException;
use lang\Object;
use peer\URL;
use util\log\Traceable;
use peer\http\HttpConnection;
use peer\http\HttpRequest;
use lang\IllegalStateException;
use lang\IllegalArgumentException;
use lang\XPClass;

/**
* REST client
Expand All @@ -14,7 +15,7 @@
* @test xp://webservices.rest.unittest.RestClientTest
* @test xp://webservices.rest.unittest.RestClientSendTest
*/
class RestClient extends \lang\Object implements Traceable {
class RestClient extends Object implements Traceable {
protected $connection= null;
protected $cat= null;
protected $serializers= [];
Expand Down Expand Up @@ -190,16 +191,19 @@ public function serializerFor($contentType) {
* @param webservices.rest.RestRequest $request
* @return webservices.rest.RestResponse
* @throws lang.IllegalStateException if no connection is set
* @throws webservices.rest.RestException
*/
public function execute(RestRequest $request) {
if (null === $this->connection) {
throw new IllegalStateException('No connection set');
}

$targetUrl= $request->targetUrl(new URL());

$send= $this->connection->create(new HttpRequest());
$send->addHeaders($request->headerList());
$send->setMethod($request->getMethod());
$send->setTarget($request->getTarget($this->connection->getUrl()->getPath('/')));
$send->setTarget($targetUrl->getPath('/'));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause problems and BC breaks.

// Compose body
// * Serialize payloads using the serializer for the given mimetype
Expand All @@ -212,18 +216,20 @@ public function execute(RestRequest $request) {
));
} else if ($request->hasBody()) {
$send->setParameters($request->getBody());
} else {
$send->setParameters($request->getParameters());
} else if ($targetUrl->hasParams()) {
$send->setParameters($targetUrl->getParams());
}

try {
$this->cat && $this->cat->debug('>>>', $send->getRequestString());
$response= $this->connection->send($send);
} catch (\io\IOException $e) {
} catch (IOException $e) {
throw new RestException('Cannot send request', $e);
}

$reader= new ResponseReader($this->deserializerFor($response->header('Content-Type')[0]), $this->marshalling);
$reader= new ResponseReader(
$this->deserializerFor($response->header('Content-Type')[0]), $this->marshalling
);
$result= new RestResponse($response, $reader);

$this->cat && $this->cat->debug('<<<', $response->toString(), $result->contentCopy());
Expand Down