public async Task <IActionResult> TestApi([FromRoute] string id, [FromBody] UserDataTestApiModel request)
        {
            _logger.LogInformation("Testing platform {id}: {@request}", id, request);


            using var session = _documentStore.OpenAsyncSession();
            // Get platform
            var platform = await _platformManager.GetPlatformAsync(id, session);

            // If there is no connection, check that the platform exists
            if (platform == null)
            {
                // TODO: Log this
                return(NotFound(PlatformUserDataResponse.Fail("", $"No platform found with ID '{id}'. [-]")));
            }

            if (string.IsNullOrEmpty(platform.ExportDataUri))
            {
                throw new ApiException(message: "The platform setup is incomplete.", errors: new List <string> {
                    "Missing ExportDataUri."
                });
            }

            UserDataRequest userDataRequest = new UserDataRequest(platform.PlatformToken, request.UserEmail, Guid.NewGuid().ToString());

            var response = await _platformHttpClient.TestUserDataFromPlatformAsync(userDataRequest, platform.ExportDataUri);

            if (response.Response.Status == "NotFound")
            {
                throw new ApiException(message: "The server responded with NotFound.", errors: new List <string> {
                    "Response: " + response.Response.Body
                });
            }

            return(Ok(response));
        }
        public async Task <IActionResult> RequestLatest([FromBody] UserUpdateRequest request)
        {
            // Create an identifier this particular request
            var requestId = Guid.NewGuid().ToString();

            // Log the request
            _logger.LogInformation("Request for latest update {request}", request);
            using var session = _documentStore.OpenAsyncSession();
            // Get platform
            var platform = await _platformManager.GetPlatformAsync(request.PlatformId, session);

            // If there is no connection, check that the platform exists
            if (platform == null)
            {
                _logger.LogWarning("Platform not found {id}", request.PlatformId);
                return(NotFound(PlatformUserDataResponse.Fail(requestId, $"There is no platform registered with ID '{request.PlatformId}'.")));
            }

            if (string.IsNullOrEmpty(platform.ExportDataUri))
            {
                throw new ApiException(message: "The platform setup is incomplete.", errors: new List <string> {
                    "Missing ExportDataUri."
                });
            }

            UserDataRequest userDataRequest       = new UserDataRequest(platform.PlatformToken, request.Username, requestId);
            PlatformUserUpdateDataMessage message = null;

            try
            {
                var response = await _platformHttpClient.GetUserDataFromPlatformAsync(userDataRequest, platform.ExportDataUri);

                message = new PlatformUserUpdateDataMessage(requestId, request.Username, Guid.Parse(request.PlatformId), response);
            }
            catch (UserNotFoundForPlatformException ex)
            {
                _logger.LogInformation("Platform with id {platformId} reported that it could not find user with username {username}.",
                                       platform.Id, ex.UserEmail);
                message = new PlatformUserUpdateDataMessage(requestId, request.Username, Guid.Parse(request.PlatformId), null, PlatformDataUpdateResultType.UserNotFound);
            }
            catch (MalformedPlatformDataException ex)
            {
                _logger.LogInformation(ex, "Data from platform could not be understood");
                message = new PlatformUserUpdateDataMessage(requestId, request.Username, Guid.Parse(request.PlatformId), null, PlatformDataUpdateResultType.MalformedDataResponse);
            }
            catch (PlatformCommunicationException ex)
            {
                _logger.LogError(ex, "Could not communicate with the platform.");
                message = new PlatformUserUpdateDataMessage(requestId, request.Username, Guid.Parse(request.PlatformId), null, PlatformDataUpdateResultType.PlatformCommunicationError);
            }

            try
            {
                await _platformDispatchManager.SendUserDataMessage(message);

                return(Ok(PlatformUserDataResponse.Ok(requestId, $"Request for update received and dispatched to service bus.")));
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex, "Unhandled exception in messaging");
                throw new ApiException(message: ex.Message, errors: new List <string> {
                    ex.StackTrace
                });
            }
        }