Skip to content

Commit

Permalink
better implementation for: make InterConnect Microservice example han…
Browse files Browse the repository at this point in the history
…dle missing catalog
  • Loading branch information
grant-g committed Jan 25, 2016
1 parent 2daf923 commit b32fc3b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 34 deletions.
11 changes: 11 additions & 0 deletions ajaxGetItems.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

include 'getItems.php';
$result = RetrieveItems($catalogRoute . "/items");
if (isset(json_decode($result)->errno)) {
http_response_code(500);
}
echo $result;

?>

34 changes: 34 additions & 0 deletions getItems.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

function RetrieveItems()
{
$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services, true);
for ($i = 0; $i < sizeof($services_json["user-provided"]); $i++){
if ($services_json["user-provided"][$i]["name"] == "catalogAPI"){
$catalogHost = $services_json["user-provided"][$i]["credentials"]["host"];
}
}
$parsedURL = parse_url($catalogHost);
$catalogRoute = $parsedURL["scheme"] . "://" . $parsedURL["host"];
$url = $catalogRoute . "/items";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curlResult = curl_exec($curl);
$curlError = curl_error($curl);
$curlErrno = curl_errno($curl);
curl_close($curl);
$firstChar = substr($curlResult, 0, 1); /* should check if $curlResult === FALSE if newer PHP */
if ($firstChar != "{") {
$errorObject = new stdClass();
$errorObject->error = $curlError;
$errorObject->errno = $curlErrno;
return json_encode($errorObject);
}
return $curlResult;
}

?>

44 changes: 10 additions & 34 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,19 @@
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<?php
$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services, true);

for ($i = 0; $i < sizeof($services_json["user-provided"]); $i++){
if ($services_json["user-provided"][$i]["name"] == "catalogAPI"){
$catalogHost = $services_json["user-provided"][$i]["credentials"]["host"];
}
}

$parsedURL = parse_url($catalogHost);
$catalogRoute = $parsedURL["scheme"] . "://" . $parsedURL["host"];
<?php

function RetrieveItems($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curlResult = curl_exec($curl);
$curlError = "error"; /* should use curl_error($curl) if newer PHP */
curl_close($curl);
include 'getItems.php';
$result = RetrieveItems();

$firstChar = substr($curlResult, 0, 1); /* should check if $curlResult === FALSE if newer PHP */
if ($firstChar != "{") {
/* should create object and use json_encode() here if newer PHP */
return "{\"error\": \"" . $curlError . "\", \"route\": \"" . $url . "\"}";
}
return $curlResult;
}
$result = RetrieveItems($catalogRoute . "/items");
?>

<script>
var RETRY_INTERVAL = 5000;
var items = <?php echo $result?>;

function loadItems(items){
if (items.error) {
reloadCatalog(items.route);
if (items.error !== undefined) {
reloadCatalog();
return;
}
var i = 0;
Expand All @@ -48,19 +24,19 @@ function loadItems(items){
}
}

function reloadCatalog(route) {
function reloadCatalog() {
showErrorMessage("The catalog is not currently available, retrying...");
window.setTimeout(
function() {
$.ajax ({
type: "GET",
contentType: "application/json",
url: route,
url: "ajaxGetItems.php",
success: function(result) {
loadItems(result);
loadItems(JSON.parse(result));
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
reloadCatalog(route);
reloadCatalog();
}
})
},
Expand All @@ -81,7 +57,7 @@ function addItem(item){

function orderItem(itemID){
//create a random customer ID and count
var custID = Math.floor((Math.random() * 999) + 1);
var custID = Math.floor((Math.random() * 9999) + 1);
var count = Math.floor((Math.random() * 9999) + 1);
var myjson = {"itemid": itemID, "customerid":custID, "count":count};

Expand Down

0 comments on commit b32fc3b

Please sign in to comment.