use of com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult in project mbed-cloud-sdk-java by ARMmbed.
the class TestServer method defineModuleMethodTestRoute.
// TODO Remove when not needed anymore
@SuppressWarnings("boxing")
private void defineModuleMethodTestRoute() {
Route route = router.route(HttpMethod.GET, "/:" + PARAM_MODULE + "/:" + PARAM_METHOD + "*").produces(APPLICATION_JSON);
route.blockingHandler(routingContext -> {
HttpServerRequest request = routingContext.request();
String module = request.getParam(PARAM_MODULE);
String method = request.getParam(PARAM_METHOD);
Map<String, Object> params = retrieveQueryParameters(request);
logger.logInfo("TEST http://localhost:" + String.valueOf(port) + request.uri() + " AT " + new Date().toString());
ModuleInstance instance = null;
try {
instance = engine.createInstance(module, defaultConnectionConfiguration);
APIMethodResult result = engine.callAPIOnInstance(instance.getId(), method, params);
if (!result.wasExceptionRaised()) {
String resultJson = Serializer.convertLegacyResultToJson(result.getResult());
logger.logDebug("RESULT: " + String.valueOf(resultJson));
engine.deleteInstance(instance.getId());
respond(200, routingContext, resultJson);
} else {
engine.deleteInstance(instance.getId());
logger.logDebug("RESULT error happened: " + result.getMetadata());
if (result.getMetadata() == null) {
sendError(setResponse(500, routingContext), null, (result.getException().getMessage() == null) ? "Exception of type " + result.getException() + " was raised" : result.getException().getMessage());
} else {
sendError(setResponse(500, routingContext), result.getMetadata().getStatusCode(), "An error occurred during call. Call metadata: " + result.getMetadata().toString());
}
}
} catch (UnknownAPIException | APICallException | ServerCacheException e) {
if (instance != null) {
try {
engine.deleteInstance(instance.getId());
} catch (ServerCacheException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
sendError(setResponse(500, routingContext), null, (e.getMessage() == null) ? "Exception of type " + e + " was raised" : e.getMessage());
}
});
}
use of com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult in project mbed-cloud-sdk-java by ARMmbed.
the class TestServer method defineRunInstanceMethodRoute.
private void defineRunInstanceMethodRoute() {
Route route = router.route(HttpMethod.POST, "/instances/:" + PARAM_INSTANCE + "/methods/:" + PARAM_METHOD).produces(APPLICATION_JSON);
route.blockingHandler(routingContext -> {
HttpServerRequest request = routingContext.request();
String instanceId = request.getParam(PARAM_INSTANCE);
String methodId = request.getParam(PARAM_METHOD);
Map<String, Object> methodArgs = fetchMethodArgs(routingContext.getBodyAsString());
execute(200, routingContext, new ServerAction() {
@Override
public Object execute() throws Exception {
logger.logInfo("TEST http://localhost:" + String.valueOf(port) + request.uri() + " AT " + new Date().toString());
APIMethodResult result = engine.callAPIOnInstance(instanceId, methodId, methodArgs);
if (!result.wasExceptionRaised()) {
return result.getResult();
}
logger.logDebug("RESULT error happened: " + result.getMetadata());
throw new APICallException(result);
}
}, true);
});
}
use of com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult in project mbed-cloud-sdk-java by ARMmbed.
the class APICaller method callAPIOnModuleInstance.
@SuppressWarnings("null")
public APIMethodResult callAPIOnModuleInstance(ModuleInstance moduleInstance, String method, Map<String, Object> parameters) throws UnknownAPIException, APICallException {
if (moduleInstance == null) {
throwMissingModule(null);
}
APIModule moduleDescription = moduleInstance.getModuleDescription();
if (moduleDescription == null) {
throwMissingModule(moduleDescription);
}
if (method == null) {
throwUnknownAPI(moduleDescription.getSimpleName(), method);
}
final List<APIMethod> methodObjs = moduleDescription.getMethod(method);
if (methodObjs == null) {
throwUnknownAPI(moduleDescription.getSimpleName(), method);
}
APICallException lastException = null;
APIMethodResult result = null;
Object instance = moduleInstance.getInstance();
// then last exception is raised or the last result is returned if not null.
for (final APIMethod methodObj : methodObjs) {
try {
result = null;
lastException = null;
API api = new API(moduleDescription, methodObj);
result = api.call(instance, parameters);
// other methods
if (!result.wasExceptionRaised()) {
return result;
}
} catch (APICallException exception) {
lastException = exception;
}
}
// If the call was successful but an exception was raised during it then the failure is returned.
if (result != null) {
return result;
}
// If the call was not successful and hence, an exception was raised, then it is thrown.
throw lastException;
}
Aggregations