Esempio n. 1
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));
            }
        }
Esempio n. 2
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);
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var requestDTO = await _requestDeserializer.Deserialize <EmailRequestDTO>(req);

            try
            {
                var response = await _controller.Execute(requestDTO);

                return(new OkObjectResult(response));
            } catch (BadRequestException)
            {
                return(new BadRequestObjectResult("Bad email request"));
            }
        }
Esempio n. 4
0
        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));
            }
        }