Clean up download latest installer code

This commit is contained in:
Kevin Bost
2024-12-26 22:26:07 -08:00
committed by Caelan
parent f0c3a100dc
commit 21c1afc9a2

View File

@@ -18,81 +18,84 @@ public partial class FlowApi
CancellationToken cancellationToken)
{
if (packageId == null)
throw new ArgumentNullException("packageId");
throw new ArgumentNullException(nameof(packageId));
if (channel == null)
throw new ArgumentNullException("channel");
throw new ArgumentNullException(nameof(channel));
var client_ = _httpClient;
var disposeClient_ = false;
try {
using (var request_ = new HttpRequestMessage()) {
request_.Method = new HttpMethod("GET");
var client = _httpClient;
var urlBuilder_ = new StringBuilder();
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "v1/download/{packageId}/{channel}"
urlBuilder_.Append("v1/download/");
urlBuilder_.Append(Uri.EscapeDataString(ConvertToString(packageId, CultureInfo.InvariantCulture)));
urlBuilder_.Append('/');
urlBuilder_.Append(Uri.EscapeDataString(ConvertToString(channel, CultureInfo.InvariantCulture)));
urlBuilder_.Append('?');
if (assetType != null) {
urlBuilder_.Append(Uri.EscapeDataString("assetType")).Append('=')
.Append(Uri.EscapeDataString(ConvertToString(assetType, CultureInfo.InvariantCulture))).Append('&');
}
using var request = new HttpRequestMessage();
request.Method = new HttpMethod("GET");
urlBuilder_.Length--;
PrepareRequest(client_, request_, urlBuilder_);
var url_ = urlBuilder_.ToString();
request_.RequestUri = new Uri(url_, UriKind.RelativeOrAbsolute);
PrepareRequest(client_, request_, url_);
var response_ = await client_.SendAsync(request_, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
var disposeResponse_ = true;
try {
var headers_ = new Dictionary<string, IEnumerable<string>>();
foreach (var item_ in response_.Headers)
headers_[item_.Key] = item_.Value;
if (response_.Content != null && response_.Content.Headers != null) {
foreach (var item_ in response_.Content.Headers)
headers_[item_.Key] = item_.Value;
}
ProcessResponse(client_, response_);
var status_ = (int) response_.StatusCode;
if (status_ == 404) {
string responseText_ = (response_.Content == null) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ApiException("A server side error occurred.", status_, responseText_, headers_, null);
} else if (status_ == 200 || status_ == 204) {
using var fs = File.Create(localFilePath);
#if NET6_0_OR_GREATER
await response_.Content.CopyToAsync(fs, cancellationToken);
#else
await response_.Content.CopyToAsync(fs);
#endif
return;
} else {
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ApiException(
"The HTTP status code of the response was not expected (" + status_ + ").",
status_,
responseData_,
headers_,
null);
}
} finally {
if (disposeResponse_)
response_.Dispose();
}
}
} finally {
if (disposeClient_)
client_.Dispose();
var urlBuilder = new StringBuilder();
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder.Append(_baseUrl);
// Operation Path: "v1/download/{packageId}/{channel}"
urlBuilder.Append("v1/download/");
urlBuilder.Append(Uri.EscapeDataString(ConvertToString(packageId, CultureInfo.InvariantCulture)));
urlBuilder.Append('/');
urlBuilder.Append(Uri.EscapeDataString(ConvertToString(channel, CultureInfo.InvariantCulture)));
urlBuilder.Append('?');
if (assetType != null) {
urlBuilder.Append(Uri.EscapeDataString("assetType")).Append('=')
.Append(Uri.EscapeDataString(ConvertToString(assetType, CultureInfo.InvariantCulture))).Append('&');
}
urlBuilder.Length--;
PrepareRequest(client, request, urlBuilder);
var url = urlBuilder.ToString();
request.RequestUri = new Uri(url, UriKind.RelativeOrAbsolute);
PrepareRequest(client, request, url);
using var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
var headers = new Dictionary<string, IEnumerable<string>>();
foreach (var item in response.Headers)
headers[item.Key] = item.Value;
if (response.Content != null && response.Content.Headers != null) {
foreach (var item in response.Content.Headers)
headers[item.Key] = item.Value;
}
ProcessResponse(client, response);
var status = (int) response.StatusCode;
if (status == 404) {
string responseText_ = (response.Content == null) ? string.Empty :
#if NET6_0_OR_GREATER
await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
#else
await response.Content.ReadAsStringAsync().ConfigureAwait(false);
#endif
throw new ApiException("A server side error occurred.", status, responseText_, headers, null);
} else if (status == 200 || status == 204) {
using var fs = File.Create(localFilePath);
if (response.Content != null) {
#if NET6_0_OR_GREATER
await response.Content.CopyToAsync(fs, cancellationToken);
#else
await response.Content.CopyToAsync(fs);
#endif
}
return;
} else {
var responseData = response.Content == null ? null :
#if NET6_0_OR_GREATER
await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
#else
await response.Content.ReadAsStringAsync().ConfigureAwait(false);
#endif
throw new ApiException(
"The HTTP status code of the response was not expected (" + status + ").",
status,
responseData,
headers,
null);
}
}
}