Exemple #1
0
        public async Task <HttpResponseMessage> PostAsync(string apiServer, HttpContent data)
        {
            // Read the provided data
            var stringData = await data.ReadAsStringAsync();

            // Deserialize the given data
            var dataForServer = JsonConvert.DeserializeObject <DataForServer>(stringData);

            var inputContext = new InputContext(dataForServer.AccessToken, Guid.NewGuid().ToString(), dataForServer.Language, ClientConstants.AppVersionString, new System.Net.IPAddress(0x2414188f));

            var request = _requestDeserializer.Deserialize(dataForServer.Request, dataForServer.Type);

            // Push it to the MVC module
            var response = await _mvcProcessor.Process(request, inputContext);

            // Serialize response (to simulate real reciving of a request)
            var serializedResponse = JsonConvert.SerializeObject(response);

            // Create content for http response
            var stringContent = new StringContent(serializedResponse);

            // Create http response to return
            var httpResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = stringContent
            };

            return(httpResponse);
        }
Exemple #2
0
        public async Task <ActionResult> Post(DataForServer dataForServer)
        {
            try
            {
                var inputContext = _inputContextService.CreateFromHttpContext(dataForServer.AccessToken, dataForServer.Language, dataForServer.Version, HttpContext);

                _logger.LogInformation("Received Request: {Type}, ID: {RequestId}, Has token: {HasToken}, Language: {Language}", dataForServer.Type, inputContext.RequestId, inputContext.AccessToken.HasValue, dataForServer.Language);

                var request = _requestDeserializer.Deserialize(dataForServer.Request, dataForServer.Type);

                _logger.LogInformation("Successfully deserialized the request");

                var result = await _mvcProcessor.Process(request, inputContext);

                _logger.LogInformation("Processed the request");

                return(new JsonResult(result));
            }
            catch (RequestException e)
            {
                return(_mvcRequestExceptionHandlerService.HandleRequestException(e, this));
            }
            catch (Exception e)
            {
                return(_mvcRequestExceptionHandlerService.HandleException(e, this));
            }
        }
        public async Task <string> GetFeedback([FromQuery(Name = "AccessKey")] string accessKey)
        {
            // Check the access key
            ThrowIfWrongAccessKey(accessKey);

            // Create input context
            var inputContext = _inputContextService.CreateFromHttpContext(null, Language.English, ClientConstants.AppVersionString, HttpContext);

            var feedbacksCount = 100;
            var request        = new GetFeedbackSummaryRequestDto(feedbacksCount);

            var clientData = await _mvcProcessor.Process(request, inputContext);

            var response = clientData.Response as GetFeedbackSummaryResponseDto;

            if (response == null)
            {
                return("Error");
            }

            return
                ($"Average rating: {response.AverageRating}\n" +
                 $"Feedbacks count total: {response.FeedbackCount}\n" +
                 $"Text only feedbacks count: {response.TextOnlyFeedbacksCount} ({response.TextOnlyFeedbacksCountPercentage} %)\n" +
                 $"Ratings only feedbacks count: {response.RatingOnlyFeedbacksCount} ({response.RatingOnlyFeedbacksCountPercentage} %)\n" +
                 $"Text and rating feedbacks count: {response.TextAndRatingFeedbacksCount} ({response.TextAndRatingFeedbacksCountPercentage} %)\n" +
                 $"Last {feedbacksCount} feedbacks: \n{response.Feedbacks.Aggregate(string.Empty, (a, b) => $"{a}\n{b}")}");
        }
        public async Task <ActionResult> Post()
        {
            _logger.LogInformation("Received a file upload request");

            // Chack if there are some files
            if (HttpContext.Request.Form.Files.Count == 0)
            {
                _logger.LogWarning("The received file upload didnt contain any files!");
                return(BadRequest(_textService.Error_NoFilesReceived));
            }

            // Check if there is exactly one file
            if (HttpContext.Request.Form.Files.Count > 1)
            {
                _logger.LogWarning("The received file upload contains too many files (FilesCount)!", HttpContext.Request.Form.Files.Count);
                return(BadRequest(_textService.Error_TooManyFiles));
            }

            // Check if the form contains the field 'Data'
            if (!HttpContext.Request.Form.ContainsKey("Data"))
            {
                _logger.LogWarning("Tried to upload a file without filling in the data for server");
                return(BadRequest(_textService.Error_DataFieldIsMissing));
            }

            try
            {
                // Get the file
                var file = HttpContext.Request.Form.Files.First();

                // Prepare the file to be served to a server action
                _uploadFileService.RegisterFile(file);

                // Get the data for server
                var serializedDataForServer = HttpContext.Request.Form["Data"].First();

                // Deserialize the data for server
                _logger.LogInformation("Deserializing the data for server");
                var dataForServer = JsonConvert.DeserializeObject <DataForServer>(serializedDataForServer);

                // Check if serialization succeeded
                if (dataForServer == null)
                {
                    _logger.LogWarning("Could not deserialize the data for server");
                    return(BadRequest(_textService.Error_CouldNotDeserializeRequest));
                }

                // Deserialize the request
                _logger.LogInformation("Deserializing the request");
                var request = _requestDeserializer.Deserialize(dataForServer.Request, dataForServer.Type);

                // Get the input context
                var inputContext = _inputContextService.CreateFromHttpContext(dataForServer.AccessToken, dataForServer.Language, ClientConstants.AppVersionString, HttpContext);

                // Process the request
                _logger.LogInformation("Processing the upload request");
                var response = await _mvcProcessor.Process(request, inputContext);

                _logger.LogInformation("Successfully processed the upload request");

                return(new JsonResult(response));
            }
            catch (RequestException e)
            {
                return(_mvcRequestExceptionHandlerService.HandleRequestException(e, this));
            }
            catch (Exception e)
            {
                return(_mvcRequestExceptionHandlerService.HandleException(e, this));
            }
        }