Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 9 additions & 4 deletions src/main/php/com/amazon/aws/ServiceEndpoint.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,18 @@ public function sign($target, int $expires= 3600, $time= null): string {
*
* @throws io.OperationFailed
*/
public function open(string $method, $target, array $headers, $hash= null, $time= null): Transfer {
public function open(string $method, $target, array $headers, $hash= null, $time= null, $timeouts= [null, null]): Transfer {
$signature= new SignatureV4($this->credentials());
list($path, $encoded, $params)= $this->target($signature, $target);

$host= $this->domain();
$conn= ($this->connections)('https://'.$host.$encoded);
$conn->setTrace($this->cat);

// Use request timeouts if supplied, otherwise use those of the connection
isset($timeouts[0]) && $conn->setTimeout($timeouts[0]);
isset($timeouts[1]) && $conn->setConnectTimeout($timeouts[1]);

// Create and sign request
$request= $conn->create(new HttpRequest());
$request->setMethod($method);
Expand Down Expand Up @@ -265,16 +269,17 @@ public function open(string $method, $target, array $headers, $hash= null, $time
*
* @throws io.OperationFailed
*/
public function request(string $method, $target, array $headers= [], $payload= null, $time= null): Response {
public function request(string $method, $target, array $headers= [], $payload= null, $time= null, $timeouts= [null, null]): Response {
if (null === $payload) {
$transfer= $this->open($method, $target, $headers + ['Content-Length' => 0], SignatureV4::NO_PAYLOAD, $time);
$transfer= $this->open($method, $target, $headers + ['Content-Length' => 0], SignatureV4::NO_PAYLOAD, $time, $timeouts);
} else {
$transfer= $this->open(
$method,
$target,
$headers + ['Content-Length' => strlen($payload)],
hash(SignatureV4::HASH, $payload),
$time
$time,
$timeouts
);
$transfer->write($payload);
}
Expand Down
26 changes: 24 additions & 2 deletions src/main/php/com/amazon/aws/api/Resource.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class Resource {
private $endpoint, $marshalling;
private $headers= [];
public $timeouts= [null, null];
public $target;

/**
Expand Down Expand Up @@ -48,6 +49,18 @@ public function __construct($endpoint, $path, $segments= [], $marshalling= null)
}
}

/**
* Sets timeouts for reading and connecting
*
* @param ?float $read
* @param ?float $connect
* @return self
*/
public function waiting($read= null, $connect= null) {
$this->timeouts= [$read, $connect];
return $this;
}

/**
* Adds headers
*
Expand Down Expand Up @@ -92,7 +105,9 @@ public function transmit($payload, $type= 'application/json', $method= 'POST') {
$method,
$this->target,
['Content-Type' => $type] + $this->headers,
$this->serialize($this->marshalling->marshal($payload), $type)
$this->serialize($this->marshalling->marshal($payload), $type),
null,
$this->timeouts
);
}

Expand All @@ -104,6 +119,13 @@ public function transmit($payload, $type= 'application/json', $method= 'POST') {
* @return com.amazon.aws.api.Transfer
*/
public function open(string $method, array $headers= []) {
return $this->endpoint->open($method, $this->target, $headers + $this->headers);
return $this->endpoint->open(
$method,
$this->target,
$headers + $this->headers,
null, // hash
null, // time
$this->timeouts
);
}
}
10 changes: 10 additions & 0 deletions src/test/php/com/amazon/aws/unittest/ResourceTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ public function serialize_rfc1738($payload, $expected) {
public function serialize_unknown() {
(new Resource($this->endpoint, '/'))->serialize(null, 'unknown/mime');
}

#[Test]
public function default_timeouts() {
Assert::equals([null, null], (new Resource($this->endpoint, '/'))->timeouts);
}

#[Test]
public function override_timeouts() {
Assert::equals([30, 4], (new Resource($this->endpoint, '/'))->waiting(30, 4)->timeouts);
}
}
Loading