Skip to content

Commit

Permalink
Get response text and json content.
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Oct 8, 2016
1 parent 1822d1c commit 95a1176
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
20 changes: 14 additions & 6 deletions src/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ public function formData():Promise {
* Returns a promise that resolves with a StdClass object containing JSON data.
*/
public function json():Promise {
// return json_decode($this->body);
$deferredJson = new Deferred();
$promise = $deferredJson->promise();
$this->readRawBodyDeferredArray []= $deferredJson;
$this->readRawBodyDeferredTransformArray []= function($data) {
return json_decode($data);
};

return $promise;
}

/**
Expand All @@ -51,12 +58,13 @@ public function text():Promise {
$deferredText = new Deferred();
$promise = $deferredText->promise();
$this->readRawBodyDeferredArray []= $deferredText;
return $promise;
// $charset = $this->getCharset();
// $toEncoding = "utf-8";
$this->readRawBodyDeferredTransformArray []= function($data) {
$charset = $this->getCharset();
$toEncoding = "utf-8";
return mb_convert_encoding($data, $toEncoding, $charset);
};

// $converted = mb_convert_encoding($this->body, $toEncoding, $charset);
// return $converted;
return $promise;
}

}#
13 changes: 10 additions & 3 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
class Headers {

private $headerArray = [];

public function __construct(array $init = []) {

}
Expand All @@ -19,7 +21,7 @@ public function __construct(array $init = []) {
* adds the header if it does not already exist.
*/
public function append(string $name, string $value) {

$this->headerArray[$name] []= $value;
}

/**
Expand All @@ -36,7 +38,11 @@ public function delete(string $name) {
* returns null.
*/
public function get(string $name) {
if($this->has($name)) {
return $this->headerArray[$name][0];
}

return null;
}

/**
Expand All @@ -52,7 +58,7 @@ public function getAll():array {
* Returns a boolean stating whether a Headers object contains a certain header.
*/
public function has(string $name):bool {

return !empty($this->headerArray[$name]);
}

/**
Expand All @@ -69,7 +75,8 @@ public function has(string $name):bool {
* strings to append to the same header
*/
public function set(string $name, $value) {

$this->headerArray[$name] = [];
$this->append($name, $value);
}

}#
15 changes: 11 additions & 4 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Response {

/** @var React\Promise\Deferred[] */
private $readRawBodyDeferredArray = [];
/** @var callable[] */
private $readRawBodyDeferredTransformArray = [];

/** @var React\Promise\Deferred */
private $deferredResponse;
Expand Down Expand Up @@ -70,10 +72,15 @@ public function stream($curlHandle, string $data):int {
/**
* Called when the underlying curl handle completes. At this point, all of the
* response has arrived, so we can resolve any functions reading the body.
*
* Each deferred reader has its own transform function to manupulate the body
* into the expected format, which is called within the same iteration.
*/
public function complete(int $statusCode) {
foreach($this->readRawBodyDeferredArray as $readRawBodyDeferred) {
$readRawBodyDeferred->resolve($this->rawBody);
foreach($this->readRawBodyDeferredArray as $i => $readRawBodyDeferred) {
$readRawBodyDeferred->resolve(
$this->readRawBodyDeferredTransformArray[$i]($this->rawBody)
);
}
}

Expand Down Expand Up @@ -170,8 +177,8 @@ private function getCharset():string {
$charset = mb_internal_encoding();

if(isset($this->headers)
&& isset($this->headers["Content-Type"])) {
$contentTypeString = $this->headers["Content-Type"];
&& $this->headers->has("Content-Type")) {
$contentTypeString = $this->headers->get("Content-Type");
$charsetEquals = "charset=";

if(!strstr($contentTypeString, $charsetEquals)) {
Expand Down

0 comments on commit 95a1176

Please sign in to comment.