Skip to content
Browse files

logging of requests moved to clients

  • Loading branch information...
1 parent 6a5f461 commit 8805c41020fcb9a2c8e4fab19dff6bb8ef437415 @lm committed Mar 19, 2011
Showing with 26 additions and 19 deletions.
  1. +6 −4 CouchPhp/Clients/CacheClient.php
  2. +13 −2 CouchPhp/Clients/CurlClient.php
  3. +2 −10 CouchPhp/Connection.php
  4. +2 −1 CouchPhp/IClient.php
  5. +3 −2 tests/MockClient.php
View
10 CouchPhp/Clients/CacheClient.php
@@ -5,6 +5,7 @@
use CouchPhp\Object,
CouchPhp\IClient,
CouchPhp\Request,
+ CouchPhp\IProfiler,
Nette\Caching\Cache;
@@ -37,24 +38,25 @@ public function __construct(IClient $client, Cache $cache)
/**
* @param Request
+ * @param IProfiler
* @return Response
*/
- public function makeRequest(Request $request)
+ public function makeRequest(Request $request, IProfiler $profiler = NULL)
{
if (!$request->isCacheable()) {
- return $this->client->makeRequest($request);
+ return $this->client->makeRequest($request, $profiler);
}
$cResponse = $this->cache[$request];
if ($cResponse !== NULL) {
$cRequest = clone $request;
$cRequest->setHeader('If-None-Match', $cResponse->getHeader('Etag'));
- $response = $this->client->makeRequest($cRequest);
+ $response = $this->client->makeRequest($cRequest, $profiler);
if ($response->getCode() === 304) { // Not modified
return $cResponse;
}
} else {
- $response = $this->client->makeRequest($request);
+ $response = $this->client->makeRequest($request, $profiler);
}
if ($response->isJson()) {
View
15 CouchPhp/Clients/CurlClient.php
@@ -6,6 +6,7 @@
CouchPhp\IClient,
CouchPhp\Request,
CouchPhp\Response,
+ CouchPhp\IProfiler,
CouchPhp\RequestException;
@@ -17,10 +18,15 @@ class CurlClient extends Object implements IClient
{
/**
* @param Request
+ * @param IProfiler
* @return Response
*/
- public function makeRequest(Request $request)
+ public function makeRequest(Request $request, IProfiler $profiler = NULL)
{
+ if ($profiler !== NULL) {
+ $profiler->logRequest($request);
+ }
+
$curl = curl_init();
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
@@ -56,11 +62,16 @@ public function makeRequest(Request $request)
}
$hLen = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
- return new Response(
+ $response = new Response(
curl_getinfo($curl, CURLINFO_HTTP_CODE),
(string) substr($body, 0, $hLen),
(string) substr($body, $hLen),
$time
);
+
+ if ($profiler !== NULL) {
+ $profiler->logResponse($response);
+ }
+ return $response;
}
}
View
12 CouchPhp/Connection.php
@@ -204,17 +204,9 @@ public function createRequest($method, $path = '', array $query = NULL)
*/
public function executeRequest(Request $request)
{
- if ($this->profiler !== NULL) {
- $this->profiler->logRequest($request);
- }
-
- $response = $this->client->makeRequest($request);
-
- if ($this->profiler !== NULL) {
- $this->profiler->logResponse($response);
- }
-
+ $response = $this->client->makeRequest($request, $this->profiler);
$data = ($isJson = $response->isJson()) ? $response->getJson() : $response->getBody();
+
if ($request->getHeader('Accept') === 'application/json' && !$isJson) {
$cType = $response->getHeader('Content-Type');
throw new RequestException("Server returns incorrect 'Content-Type: $cType', 'application/json' was expected", 0, $request, $response);
View
3 CouchPhp/IClient.php
@@ -11,8 +11,9 @@
{
/**
* @param Request
+ * @param IProfiler
* @return Response
* @throws RequestException
*/
- function makeRequest(Request $request);
+ function makeRequest(Request $request, IProfiler $profiler = NULL);
}
View
5 tests/MockClient.php
@@ -4,7 +4,8 @@
use CouchPhp\IClient,
CouchPhp\Request,
- CouchPhp\Response;
+ CouchPhp\Response,
+ CouchPhp\IProfiler;
@@ -28,7 +29,7 @@ class MockClient implements IClient
* @param string
* @return stdClass|NULL
*/
- public function makeRequest(Request $request)
+ public function makeRequest(Request $request, IProfiler $profiler = NULL)
{
$this->lastRequest = $request;
$time = microtime();

0 comments on commit 8805c41

Please sign in to comment.
Something went wrong with that request. Please try again.