Skip to content

Commit 3a4224a

Browse files
Simplify API string results
1 parent bc378e5 commit 3a4224a

File tree

4 files changed

+38
-53
lines changed

4 files changed

+38
-53
lines changed

src/Certify.Server/Certify.Server.Hub.Api.Client/Certify.Server.Hub.Api.Client.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using Certify.Models.Hub;
1313
using Certify.Models.Config.Migration;
1414
using Certify.Shared;
15-
using FileResult=Microsoft.AspNetCore.Mvc.FileResult;
1615

1716
#pragma warning disable 108 // Disable "CS0108 '{derivedDto}.ToJson()' hides inherited member '{dtoBase}.ToJson()'. Use the new keyword if hiding was intended."
1817
#pragma warning disable 114 // Disable "CS0114 '{derivedDto}.RaisePropertyChanged(String)' hides inherited member 'dtoBase.RaisePropertyChanged(String)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword."
@@ -4582,15 +4581,15 @@ public virtual async System.Threading.Tasks.Task<ActionResult> RemoveManagedChal
45824581

45834582
/// <returns>OK</returns>
45844583
/// <exception cref="ApiException">A server side error occurred.</exception>
4585-
public virtual System.Threading.Tasks.Task<FileResponse> GetPreviewAsMarkdownAsync(ManagedCertificate body)
4584+
public virtual System.Threading.Tasks.Task<string> GetPreviewAsMarkdownAsync(ManagedCertificate body)
45864585
{
45874586
return GetPreviewAsMarkdownAsync(body, System.Threading.CancellationToken.None);
45884587
}
45894588

45904589
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
45914590
/// <returns>OK</returns>
45924591
/// <exception cref="ApiException">A server side error occurred.</exception>
4593-
public virtual async System.Threading.Tasks.Task<FileResponse> GetPreviewAsMarkdownAsync(ManagedCertificate body, System.Threading.CancellationToken cancellationToken)
4592+
public virtual async System.Threading.Tasks.Task<string> GetPreviewAsMarkdownAsync(ManagedCertificate body, System.Threading.CancellationToken cancellationToken)
45944593
{
45954594
var client_ = _httpClient;
45964595
var disposeClient_ = false;
@@ -4603,7 +4602,7 @@ public virtual async System.Threading.Tasks.Task<FileResponse> GetPreviewAsMarkd
46034602
content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
46044603
request_.Content = content_;
46054604
request_.Method = new System.Net.Http.HttpMethod("POST");
4606-
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("text/html"));
4605+
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("text/plain"));
46074606

46084607
var urlBuilder_ = new System.Text.StringBuilder();
46094608
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
@@ -4633,12 +4632,11 @@ public virtual async System.Threading.Tasks.Task<FileResponse> GetPreviewAsMarkd
46334632
ProcessResponse(client_, response_);
46344633

46354634
var status_ = (int)response_.StatusCode;
4636-
if (status_ == 200 || status_ == 206)
4635+
if (status_ == 200)
46374636
{
4638-
var responseStream_ = response_.Content == null ? System.IO.Stream.Null : await response_.Content.ReadAsStreamAsync().ConfigureAwait(false);
4639-
var fileResponse_ = new FileResponse(status_, headers_, responseStream_, null, response_);
4640-
disposeClient_ = false; disposeResponse_ = false; // response and client are disposed by FileResponse
4641-
return fileResponse_;
4637+
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
4638+
var result_ = (string)System.Convert.ChangeType(responseData_, typeof(string));
4639+
return result_;
46424640
}
46434641
else
46444642
{
@@ -4662,15 +4660,15 @@ public virtual async System.Threading.Tasks.Task<FileResponse> GetPreviewAsMarkd
46624660

46634661
/// <returns>OK</returns>
46644662
/// <exception cref="ApiException">A server side error occurred.</exception>
4665-
public virtual System.Threading.Tasks.Task<FileResponse> RenderMarkdownAsync(string body)
4663+
public virtual System.Threading.Tasks.Task<string> RenderMarkdownAsync(string body)
46664664
{
46674665
return RenderMarkdownAsync(body, System.Threading.CancellationToken.None);
46684666
}
46694667

46704668
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
46714669
/// <returns>OK</returns>
46724670
/// <exception cref="ApiException">A server side error occurred.</exception>
4673-
public virtual async System.Threading.Tasks.Task<FileResponse> RenderMarkdownAsync(string body, System.Threading.CancellationToken cancellationToken)
4671+
public virtual async System.Threading.Tasks.Task<string> RenderMarkdownAsync(string body, System.Threading.CancellationToken cancellationToken)
46744672
{
46754673
var client_ = _httpClient;
46764674
var disposeClient_ = false;
@@ -4713,12 +4711,14 @@ public virtual async System.Threading.Tasks.Task<FileResponse> RenderMarkdownAsy
47134711
ProcessResponse(client_, response_);
47144712

47154713
var status_ = (int)response_.StatusCode;
4716-
if (status_ == 200 || status_ == 206)
4714+
if (status_ == 200)
47174715
{
4718-
var responseStream_ = response_.Content == null ? System.IO.Stream.Null : await response_.Content.ReadAsStreamAsync().ConfigureAwait(false);
4719-
var fileResponse_ = new FileResponse(status_, headers_, responseStream_, null, response_);
4720-
disposeClient_ = false; disposeResponse_ = false; // response and client are disposed by FileResponse
4721-
return fileResponse_;
4716+
var objectResponse_ = await ReadObjectResponseAsync<string>(response_, headers_, cancellationToken).ConfigureAwait(false);
4717+
if (objectResponse_.Object == null)
4718+
{
4719+
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
4720+
}
4721+
return objectResponse_.Object;
47224722
}
47234723
else
47244724
{
Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFrameworks>net9.0</TargetFrameworks>
5-
<ImplicitUsings>enable</ImplicitUsings>
6-
<Nullable>enable</Nullable>
7-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFrameworks>net9.0</TargetFrameworks>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
88

9-
<ItemGroup>
10-
<Content Include="nswag.json">
11-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
12-
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
13-
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
14-
</Content>
15-
</ItemGroup>
9+
<ItemGroup>
10+
<Content Include="nswag.json">
11+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
12+
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
13+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
14+
</Content>
15+
</ItemGroup>
1616

17-
<ItemGroup>
18-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.3.0" />
19-
</ItemGroup>
20-
21-
<ItemGroup>
22-
<ProjectReference Include="..\..\Certify.Models\Certify.Models.csproj" />
23-
</ItemGroup>
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\Certify.Models\Certify.Models.csproj" />
19+
</ItemGroup>
2420

2521
</Project>

src/Certify.Server/Certify.Server.Hub.Api.Client/nswag.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@
5454
"Certify.Models.Providers",
5555
"Certify.Models.Hub",
5656
"Certify.Models.Config.Migration",
57-
"Certify.Shared",
58-
"FileResult=Microsoft.AspNetCore.Mvc.FileResult"
57+
"Certify.Shared"
5958
],
6059
"additionalContractNamespaceUsages": [],
6160
"generateOptionalParameters": false,

src/Certify.Server/Certify.Server.Hub.Api/Controllers/internal/PreviewController.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public async Task<IActionResult> GetPreview([FromBody] ManagedCertificate item)
4646

4747
[HttpPost]
4848
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
49-
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK, "text/html")]
49+
[ProducesResponseType(typeof(string), StatusCodes.Status200OK, "text/plain")]
5050
[Route("managedcertificate")]
51-
public async Task<IActionResult> GetPreviewAsMarkdown([FromBody] ManagedCertificate item)
51+
public async Task<string> GetPreviewAsMarkdown([FromBody] ManagedCertificate item)
5252
{
5353
var previewSteps = await _mgmtAPI.GetPreviewActions(item.InstanceId, item, CurrentAuthContext);
5454

@@ -61,19 +61,14 @@ public async Task<IActionResult> GetPreviewAsMarkdown([FromBody] ManagedCertific
6161
.UseAdvancedExtensions()
6262
.Build();
6363

64-
var render = Markdown.ToHtml(markdown, pipeline);
65-
return new ContentResult
66-
{
67-
Content = render,
68-
ContentType = "text/html"
69-
};
64+
return Markdown.ToHtml(markdown, pipeline);
7065
}
7166

7267
[HttpPost]
7368
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
74-
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK, "text/html")]
69+
[ProducesResponseType(typeof(string), StatusCodes.Status200OK, "text/html")]
7570
[Route("rendermarkdown")]
76-
public async Task<IActionResult> RenderMarkdown([FromBody] string markdown)
71+
public async Task<string> RenderMarkdown([FromBody] string markdown)
7772
{
7873

7974
// output steps as html
@@ -83,12 +78,7 @@ public async Task<IActionResult> RenderMarkdown([FromBody] string markdown)
8378
.UseAdvancedExtensions()
8479
.Build();
8580

86-
var render = Markdown.ToHtml(markdown, pipeline);
87-
return new ContentResult
88-
{
89-
Content = render,
90-
ContentType = "text/html"
91-
};
81+
return Markdown.ToHtml(markdown, pipeline);
9282
}
9383
}
9484
}

0 commit comments

Comments
 (0)