<commit_before><?php
/**
 * (C) OpenEyes Foundation, 2014
 * This file is part of OpenEyes.
 * OpenEyes is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * OpenEyes is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with OpenEyes in a file titled COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package OpenEyes
 * @link http://www.openeyes.org.uk
 * @author OpenEyes <<EMAIL>>
 * @copyright Copyright (C) 2014, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */

require_once('Zend/Http/Client.php');

/**
 * Low-level FHIR client
 */
class FhirClient extends CApplicationComponent
{
	public $servers;
	public $http_client;

	public function init()
	{
		$this->http_client = new Zend_Http_Client;
		$this->http_client->setHeaders('Accept', 'application/xml');
	}

	/**
	 * @param string $url
	 * @param string $method
	 * @param string $body
	 * @return FhirResponse
	 */
	public function request($url, $method = 'GET', $body = null)
	{
		$this->http_client->setUri($url);
		$this->http_client->setMethod($method);
		if ($body) {
			$this->http_client->setRawData($body, 'application/xml+fhir; charset=utf-8');
		}
		$response = $this->http_client->request();
		$this->http_client->resetParameters();

		if (($body = $response->getBody())) {
			$use_errors = libxml_use_internal_errors(true);

			$value = Yii::app()->fhirMarshal->parseXml($body);

			$errors = libxml_get_errors();
			libxml_use_internal_errors($use_errors);

			if ($errors) {
				throw new Exception("Error parsing XML response from {$method} to {$url}: " . print_r($errors, true));
			}
		} else {
			$value = null;
		}

		return new FhirResponse($response->getStatus(), $value);
	}
}<commit_msg>Basic auth for FHIR client.<commit_after><?php
/**
 * (C) OpenEyes Foundation, 2014
 * This file is part of OpenEyes.
 * OpenEyes is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * OpenEyes is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with OpenEyes in a file titled COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package OpenEyes
 * @link http://www.openeyes.org.uk
 * @author OpenEyes <<EMAIL>>
 * @copyright Copyright (C) 2014, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */

require_once('Zend/Http/Client.php');

/**
 * Low-level FHIR client
 */
class FhirClient extends CApplicationComponent
{
	public $servers = array();
	public $http_client;

	public function init()
	{
		$this->http_client = new Zend_Http_Client;
		$this->http_client->setHeaders('Accept', 'application/xml');
	}

	/**
	 * @param string $url
	 * @param string $method
	 * @param string $body
	 * @return FhirResponse
	 */
	public function request($url, $method = 'GET', $body = null)
	{
		$server_name = null;
		foreach ($this->servers as $name => $server) {
			if (substr($url, 0, strlen($server['base_url']))) {
				$server_name = $name;
				break;
			}
		}

		$this->applyServerConfig($server_name ? $this->servers[$server_name] : array());

		$this->http_client->setUri($url);
		$this->http_client->setMethod($method);
		if ($body) $this->http_client->setRawData($body, 'application/xml+fhir; charset=utf-8');
		$response = $this->http_client->request();
		$this->http_client->resetParameters();

		if (($body = $response->getBody())) {
			$use_errors = libxml_use_internal_errors(true);
			$value = Yii::app()->fhirMarshal->parseXml($body);
			$errors = libxml_get_errors();
			libxml_use_internal_errors($use_errors);

			if ($errors) {
				throw new Exception("Error parsing XML response from {$method} to {$url}: " . print_r($errors, true));
			}
		} else {
			$value = null;
		}

		return new FhirResponse($response->getStatus(), $value);
	}

	private function applyServerConfig(array $config)
	{
		if (isset($config['auth'])) {
			switch ($config['auth']['type']) {
				case 'basic':
					$this->http_client->setAuth($config['auth']['username'], $config['auth']['password']);
					break;
				default:
					throw new Exception("Unsupported auth type: '{$config['auth']['type']}'");
			}
		} else {
			$this->http_client->setAuth(null);
		}
	}
}
{'fraction_non_alphanumeric': 0.1432, 'fraction_numerical': 0.0058, 'mean_word_length': 4.2642105263157895, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3850546', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1527, 'n_words': 468}