-
-
Notifications
You must be signed in to change notification settings - Fork 598
Update CachedHttpClient.php #265
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,13 @@ class CachedHttpClient extends HttpClient | |
*/ | ||
private $lastCachedResponse; | ||
|
||
/** | ||
* $path + query parameter(s) if they exist. | ||
* | ||
* @var string | ||
*/ | ||
private $path; | ||
|
||
/** | ||
* @return CacheInterface | ||
*/ | ||
|
@@ -51,16 +58,18 @@ public function setCache(CacheInterface $cache) | |
*/ | ||
public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array()) | ||
{ | ||
$this->formatPath($path, $options); | ||
|
||
$response = parent::request($path, $body, $httpMethod, $headers, $options); | ||
|
||
if (304 == $response->getStatusCode()) { | ||
$cacheResponse = $this->getCache()->get($path); | ||
$cacheResponse = $this->getCache()->get($this->path); | ||
$this->lastCachedResponse = $cacheResponse; | ||
|
||
return $cacheResponse; | ||
} | ||
|
||
$this->getCache()->set($path, $response); | ||
$this->getCache()->set($this->path, $response); | ||
|
||
return $response; | ||
} | ||
|
@@ -74,7 +83,7 @@ protected function createRequest($httpMethod, $path, $body = null, array $header | |
{ | ||
$request = parent::createRequest($httpMethod, $path, $body, $headers, $options); | ||
|
||
if ($modifiedAt = $this->getCache()->getModifiedSince($path)) { | ||
if ($modifiedAt = $this->getCache()->getModifiedSince($this->path)) { | ||
$modifiedAt = new \DateTime('@'.$modifiedAt); | ||
$modifiedAt->setTimezone(new \DateTimeZone('GMT')); | ||
|
||
|
@@ -83,7 +92,7 @@ protected function createRequest($httpMethod, $path, $body = null, array $header | |
sprintf('%s GMT', $modifiedAt->format('l, d-M-y H:i:s')) | ||
); | ||
} | ||
if ($etag = $this->getCache()->getETag($path)) { | ||
if ($etag = $this->getCache()->getETag($this->path)) { | ||
$request->addHeader( | ||
'If-None-Match', | ||
$etag | ||
|
@@ -105,4 +114,31 @@ public function getLastResponse($force = false) | |
|
||
return ($force) ? $lastResponse : $this->lastCachedResponse; | ||
} | ||
|
||
/** | ||
* Format the path and add query parameters if they exist. | ||
* | ||
* @param string $path | ||
* @param array $options | ||
* @return void | ||
*/ | ||
private function formatPath($path, array $options) | ||
{ | ||
$this->path = $path; | ||
|
||
if (array_key_exists('query', $options) && !empty($options['query'])) { | ||
$this->path .= '?'; | ||
|
||
$i = 0; | ||
foreach ($options['query'] as $key => $value) { | ||
|
||
if ($i > 0) { | ||
$this->path .= '&'; | ||
} | ||
|
||
$this->path .= $key . '=' . $value; | ||
|
||
$i++; | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please don't store this in a property. The more state is added in the class, the more complex reasonning about it is (I already think that having
getLastResponse
in theHttpClientInterface
is a big mistake).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and btw, the naming is wrong: the value stored in this property is not the path
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof
I added it in a property because it is called in two methods !
request()
andcreateRequest()
. What do you suggest so? You prefer a private method that returns something called intorequest()
andcreateRequest()
methods? Properties exists to be used and in that class only 2 already exist (plus eventually mine) then 4 for the parent class. This is acceptable don't you think?$path
is implemented everywhere and in other places it is called$id
...When I choose
$this->path
it was to respect what as already been implemented. The original variable was$path
as you can see.How do you want to call $path + query (if query parameters exist)?
By suggesting this pull request I'm not fighting for naming but simply trying to fix something that doesn't work as it should be. I'm totally open for constructive suggestions. What do you suggest so?