Skip to content
Snippets Groups Projects
Commit 09c1602c authored by Hoop, Bert Jan de's avatar Hoop, Bert Jan de
Browse files

Merge branch 'L05DOCBST-38-add-update-method' into prod

* L05DOCBST-38-add-update-method:
  L05DOCBST-38 set version 0.0.9
  L05DOCBST-38-add update_record method
  L05DOCBST refactor http client post method
parents 90bdd3d7 16be3211
No related branches found
No related tags found
No related merge requests found
......@@ -9,7 +9,7 @@
"homepage": "http://library.wur.nl"
}
],
"version": "0.0.8",
"version": "0.0.9",
"require": {
"php": ">=7.0.0",
"guzzlehttp/guzzle": "^6.3"
......
......@@ -295,7 +295,6 @@ class Webquery {
if (!empty($qry)) {
$url .= "?$qry";
}
$this->debug("search url: $url");
return $url;
}
......@@ -307,9 +306,17 @@ class Webquery {
*/
public function get_new_url() {
$url = "http://$this->host/WebQuery/$this->service/new_$this->suffix";
$this->debug("new url: $url");
return $url;
return "http://$this->host/WebQuery/$this->service/new_$this->suffix";
}
/**
* create url for update_record method from properties
* uses host, service, suffix (if set) and isn
* @return string url
*/
public function get_update_url() {
return "http://$this->host/WebQuery/$this->service/update_$this->suffix/$this->isn";
}
/**
......@@ -348,16 +355,15 @@ class Webquery {
}
$records = $this->get_records();
$this->debug("records found: ".(empty($records) ? 0 : count($records)));
while ($ofs = $this->get_next_ofs()) {
if ( ! $this->set_wq_ofs($ofs)->search()->is_success()) {
$this->warning("search failed, stopping")->ndc_pop();
return false;
}
$records = array_merge($records, $this->get_records());
$this->debug("records found: ".(empty($records) ? 0 : count($records)));
}
$this->debug("records found: " . (empty($records) ? 0 : count($records)));
$this->ndc_pop();
return $records;
}
......@@ -480,34 +486,44 @@ class Webquery {
return false;
}
$url = $this->get_new_url();
$headers = ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'];
$body = $this->xml_to_wwwform($xml);
$this->debug("request url: $url");
$this->debug("request data: $body");
$request = new Request('POST', $url, $headers, $body);
$options = [
'timeout' => 10,
'http_errors' => false, // disable exceptions on 4xx and 5xx responses
];
try {
$this->response = $this->get_http_client()->send($request, $options);
} catch (\GuzzleHttp\Exception\RequestException $ex) {
$this->error("exception posting to webquery: ".$ex->getMessage());
$this->resp_error = [
'status' => '500',
'code' => $ex->getCode(),
'message' => 'request exception: '.$ex->getMessage(),
];
$this->ndc_pop();
$this->response = $this->http_client_post($this->get_new_url(), $this->xml_to_wwwform($xml));
$success = $this->is_success();
$code = $this->get_error_code();
$message = $this->get_error_message();
if ($success) {
$this->info("success, code=$code, message=$message, new isn=" . $this->get_new_isn());
} else {
$this->error("failed, code=$code, message=$message");
}
$this->ndc_pop();
return $success;
}
/**
* update existing record in webquery
* uses properties host, service and suffix for the url
* @param string $form_data post data (x-www-form-urlencoded string)
* @return boolean true on success
*/
public function update_record($form_data) {
$this->ndc_push('update_record');
$this->reset_response();
if (empty($this->service)) {
$this->error("update_record: service is not set")->ndc_pop();
return false;
}
$this->response = $this->http_client_post($this->get_update_url(), $form_data);
$success = $this->is_success();
$code = $this->get_error_code();
$message = $this->get_error_message();
if ($success) {
$this->info("success, code=$code, message=$message, new isn=" . $this->get_new_isn());
$this->info("success, code=$code, message=$message");
} else {
$this->error("failed, code=$code, message=$message");
}
......@@ -681,6 +697,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
......@@ -695,6 +712,30 @@ class Webquery {
}
/**
* run post request with the http_client
* @param type $url post url
* @param type $body post data
* @return \GuzzleHttp\Psr7\Response
*/
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
];
try {
return $this->get_http_client()->post($url, $options);
} catch (\GuzzleHttp\Exception\RequestException $ex) {
$this->error("exception post request to webquery: " . $ex->getMessage());
return new \GuzzleHttp\Psr7\Response(500, [], "<exception><error code=" . $ex->getCode() . "><message>" . $ex->getMessage() . "</message></error></exception>");
}
}
/**
* parse xml content from message
*
......
......@@ -102,6 +102,24 @@ class WebqueryTest extends \PHPUnit_Framework_TestCase {
}
public function test_get_update_url() {
// with default settings
$wq = new \Library\Webquery();
$expect = 'http://library.wur.nl/WebQuery//update_xml/';
$this->assertEquals($expect, $wq->get_update_url(), 'no properties set, default url');
// with url properties set
$wq = new \Library\Webquery();
$wq->set_host('devel.library.wur.nl')
->set_service('testservice')
->set_suffix('record')
->set_isn("13");
$expect = 'http://devel.library.wur.nl/WebQuery/testservice/update_record/13';
$this->assertEquals($expect, $wq->get_update_url(), 'url properties set, default url');
}
public function test_search() {
// create mock http client that stores requests and responses
......@@ -126,7 +144,7 @@ EOT;
$wq = new \Library\Webquery();
$wq->set_http_client($mock_httpclient);
$wq->set_logger(new \Monolog\Logger('search'));
//$wq->set_logger(new \Monolog\Logger('search'));
$wq->set_host('devel.library.wur.nl')
->set_service('testservice')
......@@ -214,7 +232,7 @@ EOT;
$wq = new \Library\Webquery();
$wq->set_http_client($mock_httpclient);
$wq->set_logger(new \Monolog\Logger('search_all_records'));
//$wq->set_logger(new \Monolog\Logger('search_all_records'));
$wq->set_host('devel.library.wur.nl')
->set_service('test')
......@@ -246,6 +264,7 @@ EOT;
$this->assertEquals($expect, (string) $request->getUri(), 'request 3 url');
}
public function test_xml_to_wwwform() {
$xml = get_test_xml();
......@@ -307,12 +326,64 @@ EOT;
$this->assertEquals($expect, $request->getUri(), 'request 2 url');
$expect = 'sub1=subval1&sub2=&child1=childval1&child2=childval2&sub3=subval+3';
$this->assertEquals($expect, $request->getBody(), 'request 2 body');
}
public function test_update_record() {
// create mock http client that stores requests and responses
$request_history = [];
$record_13 = "<record isn='13' crc='123abcde' cu='tester'><a>0</a><a>1</a><b>2</b></record>";
$resp_13 = "<recordset><error status='200' isn='13'><code>WQW_UPDATE_OK</code><message>record updated</message></error>$record_13</recordset>";
$resp_err = "<recordset><error status='501'><code>WQE_FAILED</code><message>record not updated</message></error></recordset>";
$responses = [
new Response(200, [], $resp_13),
new Response(500, [], $resp_err),
];
$mock_httpclient = get_mock_httpclient($responses, $request_history);
$wq = new \Library\Webquery();
$wq->set_http_client($mock_httpclient);
//$wq->set_logger(new \Monolog\Logger('create_record'));
$wq->set_host('devel.library.wur.nl')
->set_service('testservice');
$data = "a=&a=1&b=2";
// first request succeeds
$result = $wq->set_isn("13")->update_record($data);
$this->assertTrue($result, 'first request return value');
$this->assertEquals('200', $wq->get_error_status(), 'first response error status');
$this->assertEquals('WQW_UPDATE_OK', $wq->get_error_code(), 'first response error code');
$this->assertEquals('record updated', $wq->get_error_message(), 'first response error message');
$this->assertEquals('13', $wq->get_new_isn(), 'first response isn');
// second request fails
$wq->set_suffix('record')->set_isn("14");
$result = $wq->update_record("$data&c=3");
$this->assertFalse($result, 'second request return value');
$this->assertEquals('501', $wq->get_error_status(), 'second response error status');
$this->assertEquals('WQE_FAILED', $wq->get_error_code(), 'second response error code');
$this->assertEquals('record not updated', $wq->get_error_message(), 'second response error message');
$this->assertEquals('', $wq->get_new_isn(), 'second response isn');
$this->assertEquals(2, count($request_history), 'request history count');
// check first request
$request = $request_history[0]['request'];
$this->assertEquals('POST', $request->getMethod(), 'request 1 method');
$expect = 'http://devel.library.wur.nl/WebQuery/testservice/update_xml/13';
$this->assertEquals($expect, $request->getUri(), 'request 1 url');
$this->assertEquals($data, $request->getBody(), 'request 1 body');
// check second request
$request = $request_history[1]['request'];
$this->assertEquals('POST', $request->getMethod(), 'request 2 method');
$expect = 'http://devel.library.wur.nl/WebQuery/testservice/update_record/14';
$this->assertEquals($expect, $request->getUri(), 'request 2 url');
$this->assertEquals("$data&c=3", $request->getBody(), 'request 2 body');
}
}
/*********** Helper Functions **************/
function get_test_xml() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment