[icinga-checkins] icinga.org: icingaweb2-module-elasticsearch/master: RestApiClient: Delegate count result creation

git at icinga.org git at icinga.org
Tue May 3 08:58:24 CEST 2016


Module: icingaweb2-module-elasticsearch
Branch: master
Commit: e4daa7ffdae5540e3aafb614ea7d5e09aff80025
URL:    https://git.icinga.org/?p=icingaweb2-module-elasticsearch.git;a=commit;h=e4daa7ffdae5540e3aafb614ea7d5e09aff80025

Author: Johannes Meyer <johannes.meyer at netways.de>
Date:   Tue May  3 08:56:59 2016 +0200

RestApiClient: Delegate count result creation

---

 library/Elasticsearch/RestApi/RestApiClient.php |   11 ++-------
 library/Elasticsearch/RestApi/RestApiQuery.php  |   27 +++++++++++++++++++++++
 2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/library/Elasticsearch/RestApi/RestApiClient.php b/library/Elasticsearch/RestApi/RestApiClient.php
index b25d7e4..38ac422 100644
--- a/library/Elasticsearch/RestApi/RestApiClient.php
+++ b/library/Elasticsearch/RestApi/RestApiClient.php
@@ -199,19 +199,12 @@ class RestApiClient implements Extensible, Reducible, Selectable, Updatable
      */
     public function count(RestApiQuery $query)
     {
-        $request = new CountApiRequest(
-            $query->getIndices(),
-            $query->getTypes(),
-            array('query' => $this->renderFilter($query->getFilter()))
-        );
-
-        $response = $this->request($request);
+        $response = $this->request($query->createCountRequest());
         if (! $response->isSuccess()) {
             throw new QueryException($this->renderErrorMessage($response));
         }
 
-        $json = $response->json();
-        return $json['count'];
+        return $query->createCountResult($response);
     }
 
     /**
diff --git a/library/Elasticsearch/RestApi/RestApiQuery.php b/library/Elasticsearch/RestApi/RestApiQuery.php
index 64173ac..31cfd11 100644
--- a/library/Elasticsearch/RestApi/RestApiQuery.php
+++ b/library/Elasticsearch/RestApi/RestApiQuery.php
@@ -119,6 +119,33 @@ class RestApiQuery extends SimpleQuery
     }
 
     /**
+     * Create and return a new instance of CountApiRequest for this query
+     *
+     * @return  CountApiRequest
+     */
+    public function createCountRequest()
+    {
+        return new CountApiRequest(
+            $this->getIndices(),
+            $this->getTypes(),
+            array('query' => $this->ds->renderFilter($this->getFilter()))
+        );
+    }
+
+    /**
+     * Create and return the result for the given count response
+     *
+     * @param   RestApiResponse     $response
+     *
+     * @return  int
+     */
+    public function createCountResult(RestApiResponse $response)
+    {
+        $json = $response->json();
+        return $json['count'];
+    }
+
+    /**
      * Create and return a new instance of SearchApiRequest for this query
      *
      * @return  SearchApiRequest



More information about the icinga-checkins mailing list