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));
            }
        }
        private async Task <UploadFile> UploadFile(UploadFile uploadFile, Guid courseId)
        {
            try
            {
                var cId               = courseId;
                var request           = new UploadPostFileRequestDto(uploadFile.Name, cId);
                var requestSerialized = JsonConvert.SerializeObject(request);
                var dataForServer     = new DataForServer(requestSerialized, request.GetType(), _loginService.LoginToken?.PrimaryTokenId, _languageService.Language, ClientConstants.AppVersionString);
                await uploadFile.Upload(JsonConvert.SerializeObject(dataForServer));

                return(uploadFile);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Esempio n. 3
0
        private async Task <T> SendRequestInnerAsync <T>(RequestBase <T> request, Func <string, HttpContent> dataCreator, string apiPath, Action?finalAction = null, string?callerName = null, int lineNumber = 0) where T : ResponseBase
        {
            try
            {
                // Serialize the request
                var serialized = await Task.Run(() => JsonConvert.SerializeObject(request));

                // Get the type of the request
                var type = request.GetType();

                // Wrap the request to DataForServer
                var wrapped = new DataForServer(serialized, type, _localAuthenticationStateProvider.Token, _languageService.Language, ClientConstants.AppVersionString);

                // Serialize the wrapper
                var serializedWrapped = JsonSerializer.Serialize(wrapped);

                // Create HTTP request
                var data = dataCreator.Invoke(serializedWrapped);

                // Call the server API
                var httpResponse = await _client.PostAsync(apiPath, data);

                // Throw if it was not successful
                httpResponse.EnsureSuccessStatusCode();

                // Get the serialized response
                var serializedResponse = await httpResponse.Content.ReadAsStringAsync();

                // Deserialize the response
                var deserializedResponse = JsonConvert.DeserializeObject <DataForClient <T> >(serializedResponse);

                // Fix fixable errors
                if (deserializedResponse.Fixes != null)
                {
                    foreach (var fix in deserializedResponse.Fixes)
                    {
                        await _fixingService.Fix(fix.ErrorFix);
                    }
                }

                // Display the received error and throw an exception
                if (deserializedResponse.Error != null)
                {
                    throw new RequestException(deserializedResponse.Error.Message);
                }

                // Check if the reponse was completely empty
                if (deserializedResponse.Response == null)
                {
                    var message = "There is a bug on the server! Didnt get any response nor error back :(.";
                    throw new RequestException(message);
                }

                return(deserializedResponse.Response);
            }
            catch (HttpRequestException ex)
            {
                _toastService.ShowError(_textService.Error_ErrorOnServer, _textService.Toast_Error);
                throw new ServerErrorException();
            }
            catch (JsonException ex) // Could not parse the response
            {
                _toastService.ShowError(_textService.Error_ErrorOnServer, _textService.Toast_Error);
                throw new ServerErrorException();
            }
            catch (RequestException ex)
            {
                _toastService.ShowError(ex.HumanMessage, _textService.Toast_Error);
                throw ex;
            }
            catch (Exception ex) // Connection error
            {
                var message = ex.Message;
                _toastService.ShowError(_textService.Error_ConnectionError, _textService.Toast_Error);
                Console.WriteLine(ex);
                throw new ConnectionException();
            }
            finally
            {
                finalAction?.Invoke();
            }
        }