handleUriCall method
Future<Result>
handleUriCall(
- Server server,
- String endpointName,
- Uri uri,
- String body,
- HttpRequest request,
inherited
Dispatches a call to the Server
to the correct Endpoint
method. If
successful, it returns the object from the method. If unsuccessful it will
return a Result
object.
Implementation
Future<Result> handleUriCall(Server server, String endpointName, Uri uri,
String body, HttpRequest request) async {
var endpointComponents = endpointName.split('.');
if (endpointComponents.isEmpty || endpointComponents.length > 2) {
return ResultInvalidParams(
'Endpoint $endpointName is not a valid endpoint name');
}
// Find correct connector
var connector = getConnectorByName(endpointName);
if (connector == null) {
return ResultInvalidParams('Endpoint $endpointName does not exist');
}
MethodCallSession session;
try {
session = MethodCallSession(
server: server,
uri: uri,
body: body,
endpointName: endpointName,
httpRequest: request,
enableLogging: connector.endpoint.logSessions,
);
} catch (e) {
return ResultInvalidParams('Malformed call: $uri');
}
var methodName = session.methodName;
var inputParams = session.queryParameters;
try {
var authFailed = await canUserAccessEndpoint(session, connector.endpoint);
if (authFailed != null) {
return authFailed;
}
var method = connector.methodConnectors[methodName];
if (method == null) {
await session.close();
return ResultInvalidParams(
'Method $methodName not found in call: $uri');
}
// TODO: Check parameters and check null safety
var paramMap = <String, dynamic>{};
for (var paramName in inputParams.keys) {
var type = method.params[paramName]?.type;
if (type == null) continue;
var formatted = _formatArg(
inputParams[paramName], type, server.serializationManager);
paramMap[paramName] = formatted;
}
var result = await method.call(session, paramMap);
// Print session info
// var authenticatedUserId = connector.endpoint.requireLogin ? await session.auth.authenticatedUserId : null;
await session.close();
return ResultSuccess(
result,
sendByteDataAsRaw: connector.endpoint.sendByteDataAsRaw,
);
} catch (e, stackTrace) {
// Something did not work out
var sessionLogId = await session.close(error: e, stackTrace: stackTrace);
return ResultInternalServerError(
e.toString(), stackTrace, sessionLogId ?? 0);
}
}