Skip to content

Commit

Permalink
#4 Requests will now time out after 10 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayson Ward committed Mar 23, 2018
1 parent 00d80bb commit 59893c8
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 81 deletions.
167 changes: 97 additions & 70 deletions AvailableServicesInfoRequest.qml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import ArcGIS.AppFramework.Controls 1.0
import "Portal"
//------------------------------------------------------------------------------

NetworkRequest {

// PROPERTIES //////////////////////////////////////////////////////////////
Item {

id: availableServiceInfoRequest

Expand All @@ -35,111 +33,140 @@ NetworkRequest {

signal complete(var data)

responseType: "json"

method: "GET"

url: serviceUrl + "?f=json" + (portal.signedIn ? "&token=" + portal.token : "")

headers{
userAgent: portal.userAgent
}

// SIGNAL IMPLEMENTATIONS //////////////////////////////////////////////////
// COMPONENTS //////////////////////////////////////////////////////////////

onReadyStateChanged: {
NetworkRequest {

//console.log("url: ", url);
//console.log(readyState);
id: serviceRequest

if (readyState === NetworkRequest.ReadyStateComplete) {
responseType: "json"

//console.log("url: ", url);
//console.log(responseText);
method: "GET"

if(status === 200){
try{
url: serviceUrl + "?f=json" + (portal.signedIn ? "&token=" + portal.token : "")

var serviceJson = JSON.parse(responseText);
headers {
userAgent: portal.userAgent
}

if(serviceJson.hasOwnProperty("error")){
if(serviceJson.error.hasOwnProperty("message")){
if(serviceJson.error.message === "Invalid Token"){
/*
If token is invalid, the service may be unsecured so try again without token.
// SIGNAL IMPLEMENTATIONS //////////////////////////////////////////////

If a token is required for a service, the message would be "Token Required"
Therefore to switch to a unsecured check first paradigm, just swap the message check in
this if clause to "Token Required" and then add the token to the url in this
if clause and remove token from the object url parameter ~ line 26,
set useToken to be false ~ line 18, and set useToken = true in this if clause.
*/
availableServiceInfoRequest.useToken = false;
availableServiceInfoRequest.url = serviceUrl + "?f=json";
availableServiceInfoRequest.send();
onReadyStateChanged: {

//console.log("url: ", url);
//console.log(readyState);

if (readyState === NetworkRequest.ReadyStateComplete) {

//console.log("url: ", url);
//console.log(responseText);

if (status === 200) {
try {

var serviceJson = JSON.parse(responseText);

if (serviceJson.hasOwnProperty("error")) {
if (serviceJson.error.hasOwnProperty("message")) {
if (serviceJson.error.message === "Invalid Token") {
/*
If token is invalid, the service may be unsecured so try again without token.
If a token is required for a service, the message would be "Token Required"
Therefore to switch to a unsecured check first paradigm, just swap the message check in
this if clause to "Token Required" and then add the token to the url in this
if clause and remove token from the object url parameter ~ line 26,
set useToken to be false ~ line 18, and set useToken = true in this if clause.
*/
availableServiceInfoRequest.useToken = false;
serviceRequest.url = serviceUrl + "?f=json";
availableServiceInfoRequest.sendRequest(); //.send();

}
else {
// BAD: 200 Status, Good Json, But Json has error property
availableServiceInfoRequest.complete({
"tileIndex": tileIndex,
"keep": false,
"serviceInfo": null
});
}
}
else{
// BAD: 200 Status, Good Json, But Json has error property
}
else {
if (_exportTilesAllowed(serviceJson)) {
// GOOD: 200 Status, Good Json and I allow exporting tiles.
availableServiceInfoRequest.complete({
"tileIndex": tileIndex,
"keep": true,
"serviceInfo": responseText,
"useToken": useToken
});
}
else {
// BAD: 200 Status, Good Json EXPORTING NOT ALLOWED.
availableServiceInfoRequest.complete({
"tileIndex": tileIndex,
"keep": false,
"serviceInfo": null
});
});
}
}
}
else{
if(_exportTilesAllowed(serviceJson)){
// GOOD: 200 Status, Good Json and I allow exporting tiles.
availableServiceInfoRequest.complete({
"tileIndex": tileIndex,
"keep": true,
"serviceInfo": responseText,
"useToken": useToken
});
}
else{
// BAD: 200 Status, Good Json EXPORTING NOT ALLOWED.
availableServiceInfoRequest.complete({
"tileIndex": tileIndex,
"keep": false,
"serviceInfo": null
});
}
catch(e) {
// BAD: 200 Status, Bad Json or didn't return Json
availableServiceInfoRequest.complete({
"tileIndex": tileIndex,
"keep": false,
"serviceInfo": null
});
}
}
catch(e){
// BAD: 200 Status, Bad Json or didn't return Json
else {
// BAD: Status other than 200 so totally bad.
availableServiceInfoRequest.complete({
"tileIndex": tileIndex,
"keep": false,
"serviceInfo": null
});
}
}
else{
// BAD: Status other than 200 so totally bad.
availableServiceInfoRequest.complete({
"tileIndex": tileIndex,
"keep": false,
"serviceInfo": null
});
}
}
}

Timer {
id: timeoutTimer
interval: 10000
running: false
repeat: false
onTriggered: {
availableServiceInfoRequest.complete({
"tileIndex": tileIndex,
"keep": false,
"serviceInfo": null
});
serviceRequest.abort();
}
}

// METHODS /////////////////////////////////////////////////////////////////

function sendRequest(){
serviceRequest.send();
timeoutTimer.start();
}

function _exportTilesAllowed(serviceInfo) {

if (serviceInfo.hasOwnProperty("exportTilesAllowed")) {
if (serviceInfo.exportTilesAllowed === false) {
return false;
} else {
}
else {
return true;
}
} else {
}
else {
return false;
}

Expand Down
22 changes: 11 additions & 11 deletions AvailableServicesModel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Item {

//console.log(JSON.stringify(result));

try{
try {
if (!result.description) {
result.description = "";
}
Expand All @@ -98,19 +98,19 @@ Item {
var thisRequest = component.createObject(parent, { portal:availableServicesModel.portal, tileIndex: tileServiceCount, serviceUrl: result.url } );
thisRequest.complete.connect(function(serviceData){

if(serviceData.keep === true){
if (serviceData.keep === true) {
servicesListModel.get(serviceData.tileIndex).serviceInfo = JSON.parse(serviceData.serviceInfo);
if(!serviceData.useToken){
if (!serviceData.useToken) {
servicesListModel.get(serviceData.tileIndex).useTokenToAccess = false;
}
}
else{
else {
servicesListModel.tilesToRemove.push(serviceData.tileIndex);
}

availableServicesModel.tileServiceSum -= serviceData.tileIndex;

if(tileServiceSum === 0){
if (tileServiceSum === 0) {
servicesListModel.removeBadTiles();
}

Expand All @@ -126,12 +126,12 @@ Item {

servicesListModel.append(result);
}
catch(error){
catch(error) {
console.log(error);
appMetrics.reportError(error)
}

})
});

if (response.nextStart > 0) {
search(response.nextStart);
Expand All @@ -140,12 +140,12 @@ Item {

availableServicesModel.servicesCountReady(servicesListModel.count);

if(servicesListModel.count > 0){
for(var i = 0; i < servicesListModel.count; i++){
requests[i].send();
if (servicesListModel.count > 0) {
for (var i = 0; i < servicesListModel.count; i++) {
requests[i].sendRequest();
}
}
else{
else {
_getUserSavedTileServices();
//modelComplete();
}
Expand Down

0 comments on commit 59893c8

Please sign in to comment.