From 6fe8bfd897499593b5769ed84b75c7246051b9cd Mon Sep 17 00:00:00 2001 From: rb111 Date: Wed, 29 Mar 2017 22:08:51 +0200 Subject: [PATCH] fix requester image not loading when using access token --- .../Models/Tfs2017BuildDefinition.cs | 3 + BuildsAppReborn.Access/Models/Tfs2017User.cs | 27 +++++++ .../Tfs2017BuildProvider.cs | 17 +++++ BuildsAppReborn.Client/Cache/BuildCache.cs | 12 +-- .../SampleData/SampleBuildDefinition.cs | 1 + .../Views/BuildsStatusView.xaml | 74 +++++++++---------- BuildsAppReborn.Contracts.UI/BuildItem.cs | 16 ---- .../Models/IBuildDefinition.cs | 2 + BuildsAppReborn.Contracts/Models/IUser.cs | 2 + .../TestData/DummyDefinition.cs | 2 + 10 files changed, 92 insertions(+), 64 deletions(-) diff --git a/BuildsAppReborn.Access/Models/Tfs2017BuildDefinition.cs b/BuildsAppReborn.Access/Models/Tfs2017BuildDefinition.cs index 3d962f6..0036f8f 100644 --- a/BuildsAppReborn.Access/Models/Tfs2017BuildDefinition.cs +++ b/BuildsAppReborn.Access/Models/Tfs2017BuildDefinition.cs @@ -28,6 +28,9 @@ internal class Tfs2017BuildDefinition : IBuildDefinition [JsonProperty("url")] public String Url { get; private set; } + [JsonIgnore] + public String BuildSettingsId { get; internal set; } + #endregion } } \ No newline at end of file diff --git a/BuildsAppReborn.Access/Models/Tfs2017User.cs b/BuildsAppReborn.Access/Models/Tfs2017User.cs index f730922..416a45a 100644 --- a/BuildsAppReborn.Access/Models/Tfs2017User.cs +++ b/BuildsAppReborn.Access/Models/Tfs2017User.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using BuildsAppReborn.Contracts.Models; using Newtonsoft.Json; @@ -26,6 +27,32 @@ internal class Tfs2017User : IUser [JsonProperty("url")] public String Url { get; private set; } + [JsonIgnore] + public Byte[] ImageData + { + get + { + if (this.imageData == null && ImageDataLoader != null) + { + this.imageData = Task.Run(() => ImageDataLoader).Result; + } + return this.imageData; + } + } + + #endregion + + #region Internal Properties + + [JsonIgnore] + internal Task ImageDataLoader { get; set; } + + #endregion + + #region Private Fields + + private Byte[] imageData; + #endregion } } \ No newline at end of file diff --git a/BuildsAppReborn.Access/Tfs2017BuildProvider.cs b/BuildsAppReborn.Access/Tfs2017BuildProvider.cs index 976769a..3158e6b 100644 --- a/BuildsAppReborn.Access/Tfs2017BuildProvider.cs +++ b/BuildsAppReborn.Access/Tfs2017BuildProvider.cs @@ -33,6 +33,10 @@ public async Task>> GetBuildDefinitio { var result = await requestResponse.Content.ReadAsStringAsync(); var data = JsonConvert.DeserializeObject>(JObject.Parse(result)["value"].ToString()); + foreach (var buildDefinition in data) + { + buildDefinition.BuildSettingsId = settings.UniqueId; + } return new DataResponse> {Data = data, StatusCode = requestResponse.StatusCode}; } @@ -65,6 +69,8 @@ public async Task>> GetBuilds(IEnumerable>(JObject.Parse(result)["value"].ToString()); + data.Select(d => d.Definition).OfType().ToList().ForEach(d => d.BuildSettingsId = settings.UniqueId); + data.Select(d => d.Requester).OfType().ToList().ForEach(a => a.ImageDataLoader = GetImageData(settings, a)); return new DataResponse> {Data = data, StatusCode = requestResponse.StatusCode}; } @@ -78,6 +84,17 @@ public async Task>> GetBuilds(IEnumerable GetImageData(BuildMonitorSettings settings, IUser user) + { + var response = await GetRequestResponse(user.ImageUrl, settings); + if (response.IsSuccessStatusCode) + { + return await response.Content.ReadAsByteArrayAsync(); + } + + return null; + } + private static async Task GetRequestResponse(String requestUrl, BuildMonitorSettings settings) { var credentials = settings.GetValueStrict(ProjectCredentialsSettingKey); diff --git a/BuildsAppReborn.Client/Cache/BuildCache.cs b/BuildsAppReborn.Client/Cache/BuildCache.cs index c2177c3..2111fb8 100644 --- a/BuildsAppReborn.Client/Cache/BuildCache.cs +++ b/BuildsAppReborn.Client/Cache/BuildCache.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.ComponentModel.Composition; using System.Linq; -using System.Net; -using System.Threading.Tasks; using System.Windows; using BuildsAppReborn.Client.Resources; using BuildsAppReborn.Contracts; @@ -84,13 +82,7 @@ protected virtual void OnCacheUpdated() #region Private Methods - private async Task GetRequesterImageWithDefaultCredentials(String requesterImageUrl) - { - var respone = await HttpRequestHelper.GetRequestResponse(requesterImageUrl, CredentialCache.DefaultCredentials); - return await respone.Content.ReadAsByteArrayAsync(); - } - - private async void OnBuildsUpdated(ICollection builds) + private void OnBuildsUpdated(ICollection builds) { if (!builds.Any()) return; @@ -102,7 +94,6 @@ private async void OnBuildsUpdated(ICollection builds) newStatus.BuildDefinition = grp.Key; newStatus.AllBuildItems = grp.Select(a => new BuildItem(a)).ToList(); - newStatus.CurrentBuild.RequesterImage = await GetRequesterImageWithDefaultCredentials(newStatus.CurrentBuild.Build.Requester.ImageUrl); buildStatusGroups.Add(newStatus); } Application.Current.Dispatcher.Invoke(() => @@ -163,7 +154,6 @@ private void UpdateCurrentIcon() #region Private Fields private readonly IEqualityComparer buildDefinitionEqualityComparer; - private BuildCacheStatus cacheStatus; private String currentIcon; diff --git a/BuildsAppReborn.Client/SampleData/SampleBuildDefinition.cs b/BuildsAppReborn.Client/SampleData/SampleBuildDefinition.cs index 0f877fb..17c8e60 100644 --- a/BuildsAppReborn.Client/SampleData/SampleBuildDefinition.cs +++ b/BuildsAppReborn.Client/SampleData/SampleBuildDefinition.cs @@ -12,6 +12,7 @@ internal class SampleBuildDefinition : IBuildDefinition public IProject Project { get; set; } public String Type { get; set; } public String Url { get; } + public String BuildSettingsId { get; } #endregion } diff --git a/BuildsAppReborn.Client/Views/BuildsStatusView.xaml b/BuildsAppReborn.Client/Views/BuildsStatusView.xaml index 3329aec..8ec89bc 100644 --- a/BuildsAppReborn.Client/Views/BuildsStatusView.xaml +++ b/BuildsAppReborn.Client/Views/BuildsStatusView.xaml @@ -104,48 +104,48 @@ + Source="{Binding CurrentBuild.Build.Requester.ImageData}" /> - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + @@ -177,9 +177,9 @@ diff --git a/BuildsAppReborn.Contracts.UI/BuildItem.cs b/BuildsAppReborn.Contracts.UI/BuildItem.cs index 9ebb21e..76465b0 100644 --- a/BuildsAppReborn.Contracts.UI/BuildItem.cs +++ b/BuildsAppReborn.Contracts.UI/BuildItem.cs @@ -29,16 +29,6 @@ public DateTime BuildTime } } - public Byte[] RequesterImage - { - get { return this.requesterImage; } - set - { - this.requesterImage = value; - OnPropertyChanged(); - } - } - #endregion #region Private Methods @@ -68,11 +58,5 @@ private DateTime GetBuildTime() } #endregion - - #region Private Fields - - private Byte[] requesterImage; - - #endregion } } \ No newline at end of file diff --git a/BuildsAppReborn.Contracts/Models/IBuildDefinition.cs b/BuildsAppReborn.Contracts/Models/IBuildDefinition.cs index b82079f..3a611b3 100644 --- a/BuildsAppReborn.Contracts/Models/IBuildDefinition.cs +++ b/BuildsAppReborn.Contracts/Models/IBuildDefinition.cs @@ -6,6 +6,8 @@ public interface IBuildDefinition { #region Public Properties + String BuildSettingsId { get; } + Int32 Id { get; } String Name { get; } diff --git a/BuildsAppReborn.Contracts/Models/IUser.cs b/BuildsAppReborn.Contracts/Models/IUser.cs index 92d4b86..d5683f5 100644 --- a/BuildsAppReborn.Contracts/Models/IUser.cs +++ b/BuildsAppReborn.Contracts/Models/IUser.cs @@ -10,6 +10,8 @@ public interface IUser String Id { get; } + Byte[] ImageData { get; } + String ImageUrl { get; } String UniqueName { get; } diff --git a/BuildsAppReborn.Infrastructure.Tests/TestData/DummyDefinition.cs b/BuildsAppReborn.Infrastructure.Tests/TestData/DummyDefinition.cs index 195ef37..58dd61f 100644 --- a/BuildsAppReborn.Infrastructure.Tests/TestData/DummyDefinition.cs +++ b/BuildsAppReborn.Infrastructure.Tests/TestData/DummyDefinition.cs @@ -17,6 +17,8 @@ public class DummyDefinition : IBuildDefinition public String Url { get; } + public String BuildSettingsId { get; } + #endregion } } \ No newline at end of file