From ce93c4a60c03bdc592ae27caf4cd3a6b3bd0acdd Mon Sep 17 00:00:00 2001 From: Jiaming Chen Date: Sun, 11 Oct 2015 09:22:08 -0700 Subject: [PATCH] Added NewArbitraryMarkdown class, RenderArbitraryMakrdown method and unit tests for it. --- .../Clients/IObservableMiscellaneousClient.cs | 8 ++ .../Clients/ObservableMiscellaneousClient.cs | 11 ++ .../Clients/MiscellaneousClientTests.cs | 20 ++++ Octokit/Clients/IMiscellaneousClient.cs | 8 ++ Octokit/Clients/MiscellaneousClient.cs | 14 +++ .../Models/Request/NewArbitraryMarkDown.cs | 100 ++++++++++++++++++ Octokit/Octokit-Mono.csproj | 3 +- Octokit/Octokit-Portable.csproj | 3 +- Octokit/Octokit.csproj | 3 +- 9 files changed, 167 insertions(+), 3 deletions(-) create mode 100644 Octokit/Models/Request/NewArbitraryMarkDown.cs diff --git a/Octokit.Reactive/Clients/IObservableMiscellaneousClient.cs b/Octokit.Reactive/Clients/IObservableMiscellaneousClient.cs index 87a9382f..8f1f3bdb 100644 --- a/Octokit.Reactive/Clients/IObservableMiscellaneousClient.cs +++ b/Octokit.Reactive/Clients/IObservableMiscellaneousClient.cs @@ -14,6 +14,14 @@ namespace Octokit.Reactive Justification = "Makes a network request")] IObservable GetAllEmojis(); + /// + /// Gets the rendered Markdown for an arbitrary markdown document. + /// + /// An arbitrary Markdown document + /// Thrown when a general API error occurs. + /// The rendered Markdown. + IObservable RenderArbitraryMarkdown(NewArbitraryMarkdown markdown); + /// /// Gets the rendered Markdown for the specified plain-text Markdown document. /// diff --git a/Octokit.Reactive/Clients/ObservableMiscellaneousClient.cs b/Octokit.Reactive/Clients/ObservableMiscellaneousClient.cs index 8aa2f98b..ea9c4096 100644 --- a/Octokit.Reactive/Clients/ObservableMiscellaneousClient.cs +++ b/Octokit.Reactive/Clients/ObservableMiscellaneousClient.cs @@ -26,6 +26,17 @@ namespace Octokit.Reactive return _client.GetAllEmojis().ToObservable().SelectMany(e => e); } + /// + /// Gets the rendered Markdown for an arbitrary markdown document. + /// + /// An arbitrary Markdown document + /// Thrown when a general API error occurs. + /// The rendered Markdown. + public IObservable RenderArbitraryMarkdown(NewArbitraryMarkdown markdown) + { + return _client.RenderArbitraryMarkdown(markdown).ToObservable(); + } + /// /// Gets the rendered Markdown for the specified plain-text Markdown document. /// diff --git a/Octokit.Tests/Clients/MiscellaneousClientTests.cs b/Octokit.Tests/Clients/MiscellaneousClientTests.cs index 98eaac13..72a9b642 100644 --- a/Octokit.Tests/Clients/MiscellaneousClientTests.cs +++ b/Octokit.Tests/Clients/MiscellaneousClientTests.cs @@ -31,7 +31,27 @@ namespace Octokit.Tests.Clients "text/plain"); } } + public class TheRenderArbitrryMarkdownMethod + { + [Fact] + public async Task RequestsTheEmojiEndpoint() + { + IApiResponse response = new ApiResponse(new Response(), "Test"); + var connection = Substitute.For(); + var forTest = new NewArbitraryMarkdown("testMarkdown", "gfm", "testContext"); + connection.Post(Args.Uri,forTest, "text/html", "text/plain") + .Returns(Task.FromResult(response)); + var client = new MiscellaneousClient(connection); + var html = await client.RenderArbitraryMarkdown(forTest); + Assert.Equal("Test", html); + connection.Received() + .Post(Arg.Is(u => u.ToString() == "markdown"), + forTest, + "text/html", + "text/plain"); + } + } public class TheGetEmojisMethod { [Fact] diff --git a/Octokit/Clients/IMiscellaneousClient.cs b/Octokit/Clients/IMiscellaneousClient.cs index 95367c3e..6df182db 100644 --- a/Octokit/Clients/IMiscellaneousClient.cs +++ b/Octokit/Clients/IMiscellaneousClient.cs @@ -30,6 +30,14 @@ namespace Octokit /// The rendered Markdown. Task RenderRawMarkdown(string markdown); + /// + /// Gets the rendered Markdown for an arbitrary markdown document. + /// + /// An arbitrary Markdown document + /// Thrown when a general API error occurs. + /// The rendered Markdown. + Task RenderArbitraryMarkdown(NewArbitraryMarkdown markdown); + /// /// List all templates available to pass as an option when creating a repository. /// diff --git a/Octokit/Clients/MiscellaneousClient.cs b/Octokit/Clients/MiscellaneousClient.cs index 7510ca84..38cfeff4 100644 --- a/Octokit/Clients/MiscellaneousClient.cs +++ b/Octokit/Clients/MiscellaneousClient.cs @@ -58,6 +58,20 @@ namespace Octokit return response.Body; } + /// + /// Gets the rendered Markdown for an arbitrary markdown document. + /// + /// An arbitrary Markdown document + /// Thrown when a general API error occurs. + /// The rendered Markdown. + public async Task RenderArbitraryMarkdown(NewArbitraryMarkdown markdown) + { + var endpoint = new Uri("markdown", UriKind.Relative); + var response = await _connection.Post(endpoint, markdown, "text/html", "text/plain") + .ConfigureAwait(false); + return response.Body; + } + /// /// List all templates available to pass as an option when creating a repository. /// diff --git a/Octokit/Models/Request/NewArbitraryMarkDown.cs b/Octokit/Models/Request/NewArbitraryMarkDown.cs new file mode 100644 index 00000000..288e6c80 --- /dev/null +++ b/Octokit/Models/Request/NewArbitraryMarkDown.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.Linq; + +namespace Octokit +{ + /// + /// Used to create anarbitrary markdown + /// + /// + /// API: https://developer.github.com/v3/markdown/#render-an-arbitrary-markdown-document + /// + [DebuggerDisplay("{DebuggerDisplay,nq}")] + public class NewArbitraryMarkdown + { + const string _markdown = "markdown"; + const string _gfm = "gfm"; + + /// + /// Create an arbitrary markdown + /// + /// The Markdown text to render + /// The rendering mode. Can be either markdown by default or gfm + /// + /// The repository context. Only taken into account when rendering as gfm + /// + public NewArbitraryMarkdown(string text, string mode, string context) + { + Text = text; + Mode = GetMode(mode); + Context = context; + } + + /// + /// Create an arbitrary markdown + /// + /// The Markdown text to render + /// + + public NewArbitraryMarkdown(string text) + :this(text,_markdown,null) + { + } + + /// + /// Create an arbitrary markdown + /// + /// The Markdown text to render + /// The rendering mode. Can be either markdown by default or gfm + /// + public NewArbitraryMarkdown(string text, string mode) + : this(text, mode, null) + { + } + + /// + /// Gets the markdown text + /// + /// + /// The text. + /// + public string Text { get; private set; } + + /// + /// Gets the mode of the text + /// + /// + /// The mode. + /// + public string Mode { get; private set; } + + /// + /// Gets the context of the markdown + /// + /// + /// The context. + /// + public string Context { get; private set; } + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "gfm")] + static string GetMode(string mode) + { + if (mode != _markdown || mode != _gfm) + { + throw (new FormatException("The mode must be either 'markdown' or 'gfm'")); + } + else + return mode; + } + internal string DebuggerDisplay + { + get + { + return String.Format(CultureInfo.InvariantCulture, "Text: {0}", Text); + } + } + } +} diff --git a/Octokit/Octokit-Mono.csproj b/Octokit/Octokit-Mono.csproj index 3d6206fc..0095d319 100644 --- a/Octokit/Octokit-Mono.csproj +++ b/Octokit/Octokit-Mono.csproj @@ -104,6 +104,7 @@ + @@ -405,4 +406,4 @@ - + \ No newline at end of file diff --git a/Octokit/Octokit-Portable.csproj b/Octokit/Octokit-Portable.csproj index 28280e54..9fca325d 100644 --- a/Octokit/Octokit-Portable.csproj +++ b/Octokit/Octokit-Portable.csproj @@ -184,6 +184,7 @@ + @@ -430,4 +431,4 @@ --> - + \ No newline at end of file diff --git a/Octokit/Octokit.csproj b/Octokit/Octokit.csproj index 3708c95b..eb0a9637 100644 --- a/Octokit/Octokit.csproj +++ b/Octokit/Octokit.csproj @@ -87,6 +87,7 @@ + @@ -441,4 +442,4 @@ --> - + \ No newline at end of file