Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove guzzlehttp/guzzle & ramsey/uuid #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"type": "library",
"require": {
"php": ">=5.6.0",
"guzzlehttp/guzzle": "^6.2",
"ramsey/uuid": "^3.5"
"paragonie/random_compat": ">=1 <9.99"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
Expand Down
84 changes: 84 additions & 0 deletions src/Http/HttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace WF\Hypernova\Http;

function getOrNull($array, $key){
return isset($array[$key]) ? $array[$key] : null;
}

class Client {
private $globalConf;

public function __construct($conf) {
$this->globalConf = $conf;
}

public function request($method, $url, $body = null, $queryParams = null, $headers = []) {
$curl = curl_init();

if (!is_null($queryParams)) {
$url .= '?' . http_build_query($queryParams);
}

$conf = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method
);

foreach ($conf as $option => $value) {
curl_setopt($curl, $option, $value);
}

if (!is_null($body)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
}

$tmp = [];
foreach ($headers as $key => $record) {
$tmp[] = $record;
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $tmp);

try {
$rawResponse = curl_exec($curl);
} catch (\ErrorException $ex) {
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
}

$response = new Response($curl, $rawResponse);
curl_close($curl); // close cURL handler

return $response;
}

public function get($url, $conf = []){
$queryParams = getOrNull($conf, 'params');
$headers = getOrNull($conf, 'headers');
return $this->request('GET', $url, null, $queryParams, $headers);
}

public function post($url, $conf = []) {
$body = getOrNull($conf, 'body');
$json = getOrNull($conf, 'json');
$queryParams = getOrNull($conf, 'params');
$headers = getOrNull($conf, 'headers');

if(!is_null($json)){
$body = json_encode($json);
if(is_null($headers)) {
$headers = [];
}
$headers[] = "Content-Type: application/json";
}
return $this->request('POST', $url, $body, $queryParams, $headers);
}
}

?>
43 changes: 43 additions & 0 deletions src/Http/HttpResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace WF\Hypernova\Http;

class Response {

private $rawBody;
private $headers;
private $response;

public function __construct($curl, $response) {
$this->rawBody = $response;
if (empty($response)) {
throw new Exception("Empty Response", 0);
return;
}

$info = curl_getinfo($curl);

if (empty($info['http_code'])) {
throw new Exception("No HTTP code was returned", 0);
return;
}

$this->response = $info;
$this->headers = $info['request_header'];


if($info['http_code'] > 399) {
throw new Exception($response, $info['http_code'] );
}
}

public function getBody() {
return $this->rawBody;
}

public function getHeaders() {
return $this->headers;
}
}

?>
8 changes: 4 additions & 4 deletions src/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ protected function doRequest($jobs)
}

/**
* @return \GuzzleHttp\Client
* @return \WF\Hypernova\Http\Client
*/
protected function getClient()
{
return new \GuzzleHttp\Client($this->config);
return new \WF\Hypernova\Http\Client($this->config);
}

/**
Expand All @@ -198,7 +198,7 @@ protected function fallback($topLevelError, $jobs)
$result->error = $topLevelError;
$result->results = array_map(function (\WF\Hypernova\Job $job) {
$jobResult = new JobResult();
$uuid = \Ramsey\Uuid\Uuid::uuid4();
$uuid = random_bytes(16);

Choose a reason for hiding this comment

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

This can cause conflicts, should be a uuid

Copy link
Author

Choose a reason for hiding this comment

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

The id only need to be unique on every render, not need to be globally unique...

$jobResult->html = $this->getFallbackHTML($job->name, $job->data, $uuid);
$jobResult->meta = ['uuid' => (string) $uuid];
$jobResult->originalJob = $job;
Expand All @@ -212,7 +212,7 @@ protected function fallback($topLevelError, $jobs)
/**
* @param string $moduleName
* @param array $data
* @param \Ramsey\Uuid\UuidInterface $uuid
* @param string $uuid
*
* @return string
*/
Expand Down