diff --git a/composer.json b/composer.json index 89c3e86c087e02893badfe3dba5eaed34a7be2cf..df6ba85fb3cdd672427642242fa500bbb55dee8c 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "homepage": "http://library.wur.nl" } ], - "version": "0.0.9", + "version": "0.1.0", "require": { "php": ">=7.0.0", "guzzlehttp/guzzle": "^6.3" diff --git a/src/library/Webquery.php b/src/library/Webquery.php index d41a14bcb363ff0679f5fd41bb7180bf91291f21..383b370775f4a1a4e4f86ae7e2b705e809137258 100644 --- a/src/library/Webquery.php +++ b/src/library/Webquery.php @@ -9,24 +9,34 @@ class Webquery { use \Library\LoggerTrait; /** - * @var \GuzzleHttp\Client psr-7 http client + * @var array attributes, keys: host, service, suffix, http_options */ - protected $http_client; + protected $attributes = [ + "host" => "library.wur.nl", + "suffix" => "xml", + ]; /** - * @var string webquery host name, default: 'library.wur.nl' + * @var array with default options for http get requests */ - protected $host = 'library.wur.nl'; + protected $default_get_options = [ + "http_errors" => false, // disable exceptions on 4xx and 5xx responses + "timeout" => 5, + ]; /** - * @var string webquery service name + * @var array with default options for http post requests */ - protected $service = ''; + protected $default_post_options = [ + "headers" => ["Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"], + "http_errors" => false, // disable exceptions on 4xx and 5xx responses + "timeout" => 10, + ]; /** - * @var string webquery suffix name, default: 'xml' + * @var \GuzzleHttp\Client psr-7 http client */ - protected $suffix = 'xml'; + protected $http_client; /** * @var string isn parameter @@ -48,12 +58,6 @@ class Webquery { */ protected $query; - /** - * @var string record element name - * if not set then the service name is used - */ - protected $record_name = ''; - /** * @var GuzzleHttp\Psr7\Response */ @@ -77,29 +81,62 @@ class Webquery { /** - * setter for http_client - * @param \GuzzleHttp\Client $http_client + * set attributes, replaces all existing attributes + * @param array $attributes new attributes * @return $this */ - public function set_http_client(\GuzzleHttp\Client $http_client) { + public function set_attributes($attributes) { - $this->http_client = $http_client; + $this->attributes = $attributes; return $this; } /** - * getter for http_client - * if no http_client is set a default http_client is created - * @return \GuzzleHttp\Client + * add attributes to existing attributes + * @param array $attributes + * @return $this */ - public function get_http_client() { + public function add_attributes($attributes) { - if (!isset($this->http_client)) { - $this->debug("no http_client set, returning default GuzzleHttp client"); - $this->http_client = new \GuzzleHttp\Client(); - } - return $this->http_client; + $this->attributes = array_merge($this->attributes, $attributes); + return $this; + } + + + /** + * return array with all attributes + * @return array + */ + public function get_attributes() { + + return $this->attributes; + } + + + /** + * add a key/value pair to the attributes + * @param type $key + * @param type $value + * @return $this + */ + public function set_attribute($key, $value) { + + $this->attributes[$key] = $value; + return $this; + } + + + /** + * return the value of the attribute with given key + * if the attribute is not set then return the given default value + * @param type $key + * @param type $default + * @return type + */ + public function get_attribute($key, $default = '') { + + return key_exists($key, $this->attributes) ? $this->attributes[$key] : $default; } @@ -110,8 +147,17 @@ class Webquery { */ public function set_host($host) { - $this->host = $host; - return $this; + return $this->set_attribute("host", $host); + } + + + /** + * get host name for webquery url + * @return string + */ + public function get_host() { + + return $this->get_attribute("host"); } @@ -122,8 +168,17 @@ class Webquery { */ public function set_service($service) { - $this->service = $service; - return $this; + return $this->set_attribute("service", $service); + } + + + /** + * get webquery service name + * @return string + */ + public function get_service() { + + return $this->get_attribute("service"); } @@ -134,11 +189,89 @@ class Webquery { */ public function set_suffix($suffix) { - $this->suffix = $suffix; + return $this->set_attribute("suffix", $suffix); + } + + + /** + * get webquery suffix + * @return string + */ + public function get_suffix() { + + return $this->get_attribute("suffix"); + } + + + /** + * set name of record element + * if not set then the service name is used as record name + * @param string $record_name record element name + * @return $this + */ + public function set_record_name($record_name) { + + return $this->set_attribute("record_name", $record_name); + } + + + /** + * get name of record element + * if record_name is not set return service name + * @return record name + */ + public function get_record_name() { + + return $this->get_attribute("record_name", $this->get_service()); + } + + + /** + * set options for http client requests + * @param array $http_options http client request options + * @return $this + */ + public function set_http_options($http_options) { + + return $this->set_attribute("http_options", $http_options); + } + + /** + * get options for http client requests + * @return array + */ + public function get_http_options() { + + return $this->get_attribute("http_options", []); + } + + /** + * setter for http_client + * @param \GuzzleHttp\Client $http_client + * @return $this + */ + public function set_http_client(\GuzzleHttp\Client $http_client) { + + $this->http_client = $http_client; return $this; } + /** + * getter for http_client + * if no http_client is set a default http_client is created + * @return \GuzzleHttp\Client + */ + public function get_http_client() { + + if (!isset($this->http_client)) { + $this->debug("no http_client set, returning default GuzzleHttp client"); + $this->http_client = new \GuzzleHttp\Client(); + } + return $this->http_client; + } + + /** * set isn parameter for webquery search query * @param string $isn webquery isn parameter, not used in url if empty @@ -193,29 +326,6 @@ class Webquery { } - /** - * set name of record element - * if not set then the service name is used as record name - * @param string $record_name record element name - * @return $this - */ - public function set_record_name($record_name) { - - $this->record_name = $record_name; - return $this; - } - - - /** - * get name of record element - * if record_name is not set return service name - * @return record name - */ - public function get_record_name() { - - return isset($this->record_name) ? $this->record_name : $this->service; - } - /** * create query string from query parameters * @@ -287,7 +397,7 @@ class Webquery { */ public function get_search_url() { - $url = "http://$this->host/WebQuery/$this->service/$this->suffix"; + $url = "http://" . $this->get_host() . "/WebQuery/" . $this->get_service() . "/" . $this->get_suffix(); if (!empty($this->isn)) { $url .= "/$this->isn"; } @@ -306,7 +416,7 @@ class Webquery { */ public function get_new_url() { - return "http://$this->host/WebQuery/$this->service/new_$this->suffix"; + return "http://" . $this->get_host() . "/WebQuery/" . $this->get_service() . "/new_" . $this->get_suffix(); } /** @@ -316,7 +426,7 @@ class Webquery { */ public function get_update_url() { - return "http://$this->host/WebQuery/$this->service/update_$this->suffix/$this->isn"; + return "http://" . $this->get_host() . "/WebQuery/" . $this->get_service() . "/update_" . $this->get_suffix() . "/$this->isn"; } /** @@ -330,7 +440,7 @@ class Webquery { $this->ndc_push('search'); $this->reset_response(); - if (empty($this->service)) { + if (empty($this->get_service())) { return $this->error("search: service is not set")->ndc_pop(); } @@ -363,7 +473,7 @@ class Webquery { $records = array_merge($records, $this->get_records()); } - $this->debug("records found: " . (empty($records) ? 0 : count($records))); + $this->debug("number of " . $this->get_record_name() . " records found: " . (empty($records) ? 0 : count($records))); $this->ndc_pop(); return $records; } @@ -379,8 +489,7 @@ class Webquery { return false; } - $record_name = $this->get_record_name(); - $records = $xml->xpath("//$record_name"); + $records = $xml->xpath("//" . $this->get_record_name()); if (empty($records)) { return false; } @@ -399,8 +508,7 @@ class Webquery { return false; } - $record_name = $this->get_record_name(); - return $xml->xpath("//$record_name"); + return $xml->xpath("//" . $this->get_record_name()); } @@ -481,7 +589,7 @@ class Webquery { $this->ndc_push('create_record'); $this->reset_response(); - if (empty($this->service)) { + if (empty($this->get_service())) { $this->error("create_record: service is not set")->ndc_pop(); return false; } @@ -512,7 +620,7 @@ class Webquery { $this->ndc_push('update_record'); $this->reset_response(); - if (empty($this->service)) { + if (empty($this->get_service())) { $this->error("update_record: service is not set")->ndc_pop(); return false; } @@ -698,10 +806,7 @@ class Webquery { public function http_client_get($url) { $this->debug("http get, url: $url"); - $options = [ - 'timeout' => 10, - 'http_errors' => false, // disable exceptions on 4xx and 5xx responses - ]; + $options = array_merge($this->default_get_options, $this->get_http_options()); try { return $this->get_http_client()->get($url, $options); } catch (\GuzzleHttp\Exception\RequestException $ex) { @@ -721,12 +826,8 @@ class Webquery { public function http_client_post($url, $body) { $this->debug("http post, url: $url, data: $body"); - $options = [ - "body" => $body, - "headers" => ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'], - 'timeout' => 10, - 'http_errors' => false, // disable exceptions on 4xx and 5xx responses - ]; + $options = array_merge($this->default_post_options, $this->get_http_options()); + $options["body"] = $body; try { return $this->get_http_client()->post($url, $options); } catch (\GuzzleHttp\Exception\RequestException $ex) { diff --git a/tests/library/WebqueryTest.php b/tests/library/WebqueryTest.php index 42aa00b645579b4dc72ac8c7ead4cf17cf3b01b6..8297e1f0c5792c43c5c625e0536a462af8df9fa4 100644 --- a/tests/library/WebqueryTest.php +++ b/tests/library/WebqueryTest.php @@ -7,6 +7,63 @@ use GuzzleHttp\Psr7\Response; class WebqueryTest extends \PHPUnit_Framework_TestCase { + public function test_attributes() { + + $wq = new \Library\Webquery(); + $expect = [ + "host" => "library.wur.nl", + "suffix" => "xml", + ]; + $this->assertEquals($expect, $wq->get_attributes(), 'default attributes'); + + $attribs = [ + "host" => "devel.library.wur.nl", + "service" => "any", + ]; + $wq->set_attributes($attribs); + $this->assertEquals($attribs, $wq->get_attributes(), 'attributes after set_attributes()'); + + $wq = new \Library\Webquery(); + $attribs = [ + "host" => "devel.library.wur.nl", + "service" => "any", + ]; + $expect = [ + "host" => "devel.library.wur.nl", + "service" => "any", + "suffix" => "xml", + ]; + $wq->add_attributes($attribs); + $this->assertEquals($expect, $wq->get_attributes(), 'attributes after add_attributes()'); + + $wq = new \Library\Webquery(); + $value = "newhost"; + $wq->set_host($value); + $this->assertEquals($value, $wq->get_host(), "set/get attribute host"); + $value = "newservice"; + $wq->set_service($value); + $this->assertEquals($value, $wq->get_service(), "set/get attribute service"); + $value = "newsuffix"; + $wq->set_suffix($value); + $this->assertEquals($value, $wq->get_suffix(), "set/get attribute suffix"); + $this->assertEquals("newservice", $wq->get_record_name(), "get attribute record_name if not yet set"); + $value = "newrecord"; + $wq->set_record_name($value); + $this->assertEquals($value, $wq->get_record_name(), "set/get attribute record_name"); + $value = ["timeout" => "40"]; + $wq->set_http_options($value); + $this->assertEquals($value, $wq->get_http_options(), "set/get attribute http_options"); + $attribs = [ + "host" => "newhost", + "service" => "newservice", + "suffix" => "newsuffix", + "record_name" => "newrecord", + "http_options" => ["timeout" => "40"], + ]; + $this->assertEquals($attribs, $wq->get_attributes(), 'attributes after set_*'); + } + + public function test_get_query_string() { $tests = [