コード例 #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);
        }
コード例 #2
0
        public async Task <string> SaveSnippetAsync(string content)
        {
            if (string.IsNullOrWhiteSpace(content) || content.Trim().Length < SnippetContentMinLength)
            {
                throw new ArgumentException($"The snippet content should be at least {SnippetContentMinLength} symbols.", nameof(content));
            }

            var requestData = new CreateSnippetRequestModel
            {
                Files = new[] { new CreateSnippetFileRequestModel {
                                    Content = content
                                }, },
            };

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

            if (response.IsSuccessStatusCode)
            {
                var id = await response.Content.ReadAsStringAsync();

                return(id);
            }

            throw new Exception("Something went wrong. Please try again later.");
        }