Implement GetInstallStatusAsync(), as without it we cannot detect installation failures. (#3)

This commit is contained in:
TautvydasZilys 2019-08-23 16:05:46 -07:00 коммит произвёл GitHub
Родитель 58ccd5d8aa
Коммит da9ddf47db
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 81 добавлений и 31 удалений

Просмотреть файл

@ -57,7 +57,6 @@
<Compile Include="Source\AppxManifest.cs" />
<Compile Include="Source\PackageHelper.cs" />
<Compile Include="Source\ParameterHelper.cs" />
<Compile Include="WindowsDevicePortalWrapper\AppDeployment.cs" />
<Compile Include="WindowsDevicePortalWrapper\ApplicationManager.cs" />
<Compile Include="WindowsDevicePortalWrapper\CertificateHandling.cs" />
<Compile Include="WindowsDevicePortalWrapper\Core\AppCrashDumpCollection.cs" />

Просмотреть файл

@ -1,30 +0,0 @@
//----------------------------------------------------------------------------------------------
// <copyright file="AppDeployment.cs" company="Microsoft Corporation">
// Licensed under the MIT License. See LICENSE.TXT in the project root license information.
// </copyright>
//----------------------------------------------------------------------------------------------
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace Microsoft.Tools.WindowsDevicePortal
{
/// <content>
/// MOCK implementation of App Deployment methods.
/// </content>
public partial class DevicePortal
{
/// <summary>
/// API for getting installation status.
/// </summary>
/// <returns>The status</returns>
public async Task<ApplicationInstallStatus> GetInstallStatusAsync()
{
ApplicationInstallStatus status = ApplicationInstallStatus.Completed;
return await Task.FromResult(status);
}
}
}

Просмотреть файл

@ -12,8 +12,10 @@ using System.Net;
using System.Net.Http;
#endif // !WINDOWS_UWP
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.Tools.WindowsDevicePortal.DevicePortalException;
#if WINDOWS_UWP
using Windows.Foundation;
using Windows.Security.Credentials;
@ -225,6 +227,85 @@ namespace Microsoft.Tools.WindowsDevicePortal
new ApplicationInstallStatusEventArgs(status, phase, message));
}
private async Task<ApplicationInstallStatus> GetInstallStatusAsync()
{
ApplicationInstallStatus status = ApplicationInstallStatus.None;
Uri uri = Utilities.BuildEndpoint(
this.deviceConnection.Connection,
InstallStateApi);
WebRequestHandler handler = new WebRequestHandler();
handler.UseDefaultCredentials = false;
handler.Credentials = this.deviceConnection.Credentials;
handler.ServerCertificateValidationCallback = this.ServerCertificateValidation;
using (HttpClient client = new HttpClient(handler))
{
this.ApplyHttpHeaders(client, HttpMethods.Get);
using (HttpResponseMessage response = await client.GetAsync(uri).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
if (response.StatusCode == HttpStatusCode.OK)
{
// Status code: 200
if (response.Content == null)
{
status = ApplicationInstallStatus.Completed;
}
else
{
// If we have a response body, it's possible this was an error
// (even though we got an HTTP 200).
Stream dataStream = null;
using (HttpContent content = response.Content)
{
dataStream = new MemoryStream();
await content.CopyToAsync(dataStream).ConfigureAwait(false);
// Ensure we point the stream at the origin.
dataStream.Position = 0;
}
if (dataStream != null)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HttpErrorResponse));
HttpErrorResponse errorResponse = (HttpErrorResponse)serializer.ReadObject(dataStream);
if (errorResponse.Success)
{
status = ApplicationInstallStatus.Completed;
}
else
{
throw new DevicePortalException(response.StatusCode, errorResponse, uri);
}
}
else
{
throw new DevicePortalException(response.StatusCode, "Failed to deserialize GetInstallStatus response.");
}
}
}
else if (response.StatusCode == HttpStatusCode.NoContent)
{
// Status code: 204
status = ApplicationInstallStatus.InProgress;
}
}
else
{
throw await DevicePortalException.CreateAsync(response);
}
}
}
return status;
}
#region Data contract
/// <summary>
/// Object representing a list of Application Packages