예제 #1
0
        private async Task SaveDataToFile(string fileName, RequestFileContents requestData, bool retryOnFailure = true)
        {
            var folder = ApplicationData.Current.LocalFolder;
            var prefix = string.Empty;

            prefix = ViewModel.SavedFilePrefix.Length == 0
                ? string.Empty
                : $"{ViewModel.SavedFilePrefix}.";

            try
            {
                var file = await folder.CreateFileAsync($"{prefix}{fileName}", CreationCollisionOption.ReplaceExisting);

                if (file != null)
                {
                    await FileIO.WriteTextAsync(file, requestData.ToString());

                    _lastSavedFilePath = file.Path;
                    SavePathToRecentFiles(file.Path);
                    ViewModel.ShowInfoMessage($"Saved to disk: {file.Path}", SavedInfoMessageDisplaySeconds);
                    ViewModel.IsClipboardCopyEnabled = true;
                    return;
                }
            }
            catch (Exception) when(retryOnFailure)
            {
                // Attempt to retry writing the file if it fails using a plain name
                ViewModel.SavedFilePrefix = string.Empty;
                await SaveDataToFile("request.txt", requestData, false);

                return;
            }

            ViewModel.ShowInfoMessage("Failed to save file to disk");
        }
예제 #2
0
 private void SetClosedOrFailedFileState(string errorMessage = "Failed to open resource")
 {
     ViewModel.FileNamePath        = string.Empty;
     ViewModel.OriginalFileContent = string.Empty;
     UpdateTitle(string.Empty);
     _requestFileContents   = null;
     ViewModel.SourceObject = null;
     ViewModel.ShowInfoMessage(errorMessage);
     ViewModel.IsSaveEnabled = false;
 }
예제 #3
0
        private async Task SetOpenedWebResourceState(WebResponse response)
        {
            ViewModel.FileNamePath    = response.ResponseUri.ToString();
            ViewModel.WebFileNamePath = string.Empty;
            _requestFileContents      = await RequestFileContents.ReadResponseContents(response);

            ViewModel.OriginalFileContent = _requestFileContents.Payload;
            ViewModel.SourceObject        = _requestFileContents.GetPayloadObject();
            if (ViewModel.SourceObject == null)
            {
                SetClosedOrFailedFileState("Could not parse json from web resource");
                return;
            }
            ViewModel.IsSaveEnabled = true;
            UpdateTitle(GetFileName());
            ViewModel.ShowInfoMessage($"Opened web resource: {ViewModel.FileNamePath}");
        }
예제 #4
0
        private async Task SetOpenedFileState(StorageFile file)
        {
            ViewModel.FileNamePath    = file.Path;
            ViewModel.WebFileNamePath = string.Empty;
            _requestFileContents      = await RequestFileContents.ReadFileContents(file);

            ViewModel.OriginalFileContent = _requestFileContents.Payload;
            ViewModel.SourceObject        = _requestFileContents.GetPayloadObject();
            if (ViewModel.SourceObject == null)
            {
                SetClosedOrFailedFileState("Could not parse json from local resource");
                return;
            }
            ViewModel.IsSaveEnabled = true;
            UpdateTitle(GetFileName());
            ViewModel.ShowInfoMessage($"Opened filed: {ViewModel.FileNamePath}");
        }
예제 #5
0
        public static async Task <RequestFileContents> ReadResponseContents(WebResponse response)
        {
            var resultRequestFileContents = new RequestFileContents();

            for (int i = 0; i < response.Headers.Count; ++i)
            {
                var key   = response.Headers.AllKeys[i];
                var value = response.Headers[key];
                resultRequestFileContents.Headers += $"{key}: {value}\n";
            }

            using (Stream stream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(stream))
                {
                    while (!reader.EndOfStream)
                    {
                        resultRequestFileContents.Payload += await reader.ReadLineAsync();
                    }
                }

            return(resultRequestFileContents);
        }
예제 #6
0
        public static async Task <RequestFileContents> ReadFileContents(StorageFile file)
        {
            var resultRequestFileContents = new RequestFileContents();
            var hasHitPayload             = false;

            using (var inputStream = await file.OpenReadAsync())
                using (var classicsStream = inputStream.AsStreamForRead())
                    using (var streamReader = new StreamReader(classicsStream))
                    {
                        while (streamReader.Peek() > 0)
                        {
                            var line = $"{streamReader.ReadLine()}\n";
                            if (line == "\n")
                            {
                                hasHitPayload = true;
                                continue;
                            }

                            if (hasHitPayload)
                            {
                                resultRequestFileContents.Payload += line;
                            }
                            else
                            {
                                if (line.ToLower().StartsWith("http/"))
                                {
                                    continue;
                                }

                                resultRequestFileContents.Headers += line;
                            }
                        }
                    }

            return(resultRequestFileContents);
        }