Example #1
0
        public async Task <string> SaveSnippetAsync(IEnumerable <CodeFile> codeFiles)
        {
            if (codeFiles == null)
            {
                throw new ArgumentNullException(nameof(codeFiles));
            }

            var codeFilesValidationError = CodeFilesHelper.ValidateCodeFilesForSnippetCreation(codeFiles);

            if (!string.IsNullOrWhiteSpace(codeFilesValidationError))
            {
                throw new InvalidOperationException(codeFilesValidationError);
            }

            var requestData = new CreateSnippetRequestModel {
                Files = codeFiles
            };

            var response = await this.httpClient.PostAsJsonAsync(this.snippetsOptions.CreateUrl, requestData);

            response.EnsureSuccessStatusCode();

            var id = await response.Content.ReadAsStringAsync();

            return(id);
        }
Example #2
0
        public async Task <string> SaveSnippetAsync(IEnumerable <CodeFile> codeFiles)
        {
            var snippetId = string.Empty;

            if (codeFiles == null)
            {
                throw new ArgumentNullException(nameof(codeFiles));
            }

            var codeFilesValidationError = CodeFilesHelper.ValidateCodeFilesForSnippetCreation(codeFiles);

            if (!string.IsNullOrWhiteSpace(codeFilesValidationError))
            {
                throw new InvalidOperationException(codeFilesValidationError);
            }

            using (var memoryStream = new MemoryStream())
            {
                using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
                {
                    foreach (var codeFile in codeFiles)
                    {
                        var byteArray = Encoding.UTF8.GetBytes(codeFile.Content);
                        var codeEntry = archive.CreateEntry(codeFile.Path);
                        using var entryStream = codeEntry.Open();
                        entryStream.Write(byteArray);
                    }
                }

                memoryStream.Position = 0;

                var inputData = new StreamContent(memoryStream);

                var response = await this.httpClient.PostAsync(this.snippetsService, inputData);

                snippetId = await response.Content.ReadAsStringAsync();
            }

            return(snippetId);
        }
Example #3
0
        public async Task <IEnumerable <CodeFile> > GetSnippetContentAsync(string snippetId)
        {
            if (string.IsNullOrWhiteSpace(snippetId))
            {
                throw new ArgumentException("Invalid snippet ID.", nameof(snippetId));
            }

            IEnumerable <CodeFile> snippetFiles;

            if (snippetId.Length != SnippetIdLength)
            {
                try
                {
                    snippetFiles = snippetId.ToCodeFiles();
                    var codeFilesValidationError = CodeFilesHelper.ValidateCodeFilesForSnippetCreation(snippetFiles);
                    if (!string.IsNullOrWhiteSpace(codeFilesValidationError))
                    {
                        throw new InvalidOperationException(codeFilesValidationError);
                    }
                }
                catch
                {
                    throw new ArgumentException("Invalid snippet ID.", nameof(snippetId));
                }
            }
            else
            {
                var reponse = await this.httpClient.GetAsync($"{this.snippetsService}/{snippetId}");

                var zipStream = await reponse.Content.ReadAsStreamAsync();

                zipStream.Position = 0;
                snippetFiles       = await ExtractSnippetFilesFromZip(zipStream);
            }

            return(snippetFiles);
        }