forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternalApiTest.php
More file actions
155 lines (135 loc) · 5.45 KB
/
Copy pathInternalApiTest.php
File metadata and controls
155 lines (135 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Testing script for the local/internal use of the api
*
* @package OpenEMR
* @link https://www.open-emr.org
* @author Brady Miller <brady.g.miller@gmail.com>
* @copyright Copyright (c) 2019 Brady Miller <brady.g.miller@gmail.com>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
// Enable this script via environment variable
if (!getenv('OPENEMR_ENABLE_INTERNAL_API_TEST')) {
die('Set OPENEMR_ENABLE_INTERNAL_API_TEST=1 environment variable to enable this script');
}
require_once(__DIR__ . "/../../interface/globals.php");
use OpenEMR\BC\ServiceContainer;
use OpenEMR\Common\Csrf\CsrfUtils;
use OpenEMR\Common\Http\HttpRestRequest;
use OpenEMR\Common\Http\HttpRestRouteHandler;
use OpenEMR\Common\Http\HttpSessionFactory;
use OpenEMR\Common\Session\SessionWrapperFactory;
use OpenEMR\Core\Header;
use OpenEMR\Core\OEGlobalsBag;
use OpenEMR\Core\OEHttpKernel;
use OpenEMR\RestControllers\FacilityRestController;
use OpenEMR\RestControllers\Finder\StandardRouteFinder;
use OpenEMR\Services\FacilityService;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
$session = SessionWrapperFactory::getInstance()->getActiveSession();
?>
<html>
<head>
<?php Header::setupAssets('jquery'); ?>
<script>
function testAjaxApi() {
$.ajax({
type: 'GET',
url: '../../apis/default/api/facility',
dataType: 'json',
headers: {
'apicsrftoken': <?php echo js_escape(CsrfUtils::collectCsrfToken($session, 'api')); ?>
},
success: function(thedata){
let thedataJSON = JSON.stringify(thedata);
$("#ajaxapi").html(thedataJSON);
},
error:function(){
}
});
}
function testFetchApi() {
fetch('../../apis/default/api/facility', {
credentials: 'same-origin',
method: 'GET',
headers: new Headers({
'apicsrftoken': <?php echo js_escape(CsrfUtils::collectCsrfToken($session, 'api')); ?>
})
})
.then(response => response.json())
.then(data => {
let dataJSON = JSON.stringify(data);
document.getElementById('fetchapi').innerHTML = dataJSON;
})
.catch(error => console.error(error))
}
$(function () {
testAjaxApi();
testFetchApi();
});
</script>
</head>
<?php
// CALL the api via a local jquery ajax call
// See above testAjaxApi() function for details.
echo "<b>local jquery ajax call:</b><br />";
echo "<div id='ajaxapi'></div>";
echo "<br /><br />";
// CALL the api via a local fetch call
// See above testFetchApi() function for details.
echo "<b>local fetch call:</b><br />";
echo "<div id='fetchapi'></div>";
echo "<br /><br />";
// CALL the api via route handler
// This allows same notation as the calls in the api (ie. '/api/facility'), but
// is limited to get requests at this time.
$globalsBag = OEGlobalsBag::getInstance();
$oeKernel = $globalsBag->getKernel();
$getParams = [];
try {
$restRequest = HttpRestRequest::create('/api/facility', 'GET');
$restRequest->setRequestUserRole("users");
$sessionFactory = new HttpSessionFactory($restRequest, $oeKernel->getWebRoot(), HttpSessionFactory::SESSION_TYPE_CORE);
$restRequest->setSession($sessionFactory->createSession());
$getParams = $restRequest->getQueryParams();
$kernel = new OEHttpKernel($oeKernel->getEventDispatcher(), new ControllerResolver());
$kernel->setSystemLogger(ServiceContainer::getLogger());
$dispatchHandler = new HttpRestRouteHandler($kernel);
$routeFinder = new StandardRouteFinder($kernel);
$routes = $routeFinder->find($restRequest);
$dispatchRestRequest = $dispatchHandler->dispatch($routes, $restRequest);
if (!$dispatchRestRequest->getAttribute("_controller")) {
throw new \Exception("No controller found for the route.");
}
$controller = $dispatchRestRequest->getAttribute("_controller");
$response = $controller();
if (!$response instanceof \Psr\Http\Message\ResponseInterface) {
throw new \Exception("Controller did not return a valid response.");
}
if ($response->getStatusCode() != 200) {
throw new \Exception("Controller returned an error response with status code: " . $response->getStatusCode());
}
echo "<b>api via route handler call returning json:</b><br />";
$contents = $response->getBody()->getContents();
echo $contents;
} catch (\Throwable $e) {
echo "<b>api via route handler call returned error:</b><br />";
echo "Error Message: " . $e->getMessage() . "<br />";
}
echo "<br /><br />";
// CALL the underlying service that is used by the api
echo "<b>service call:</b><br />";
echo json_encode((new FacilityService())->getAllFacility());
echo "<br /><br />";
// CALL the underlying controller that is used by the api
echo "<b>controller call:</b><br />";
$response = (new FacilityRestController())->getAll(HttpRestRequest::create('/api/facility', 'GET'));
if ($response->getStatusCode() != 200) {
echo "Controller returned an error response with status code: " . $response->getStatusCode() . "<br />";
echo "Error Message: " . $response->getReasonPhrase() . "<br />";
} else {
echo $response->getBody()->getContents();
}
echo "<br /><br />";
?>
</html>