From 0d77131208a8ec55f3ddf2ecee99df0269e7a2d0 Mon Sep 17 00:00:00 2001 From: Roman Kolobov Date: Fri, 6 Aug 2021 15:03:07 +0300 Subject: [PATCH] improve usage of YouTrackClient wrt Hub url --- src/YouTrackSharp/BearerTokenConnection.cs | 6 +- src/YouTrackSharp/Generated/Hub.Api.cs | 60 +++++++++++++++---- .../Generated/YouTrackSharp.Api.cs | 13 +--- src/YouTrackSharp/TimeTracking/WorkItem.cs | 2 - 4 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/YouTrackSharp/BearerTokenConnection.cs b/src/YouTrackSharp/BearerTokenConnection.cs index 1eb860e..653355d 100644 --- a/src/YouTrackSharp/BearerTokenConnection.cs +++ b/src/YouTrackSharp/BearerTokenConnection.cs @@ -117,15 +117,11 @@ public override async Task GetAuthenticatedApiClient() try { - var response = await _youTrackClient.ConfigAsync("ring(url)"); - - _youTrackClient.HubApiUrl = response.Ring.Url.TrimEnd('/') + "/api/rest/"; - var me = await _youTrackClient.HubApiUserGetAsync("me", "guest"); if (me.Guest == true || me.Guest == null) { throw new UnauthorizedConnectionException( - Strings.Exception_CouldNotAuthenticate, (HttpStatusCode)200, "Hub responds that current user is guest"); + Strings.Exception_CouldNotAuthenticate, HttpStatusCode.OK, "Hub responds that current user is guest"); } } catch (YouTrackErrorException e) diff --git a/src/YouTrackSharp/Generated/Hub.Api.cs b/src/YouTrackSharp/Generated/Hub.Api.cs index b7c2ee6..b9bd505 100644 --- a/src/YouTrackSharp/Generated/Hub.Api.cs +++ b/src/YouTrackSharp/Generated/Hub.Api.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using System.Net; +using System.Threading.Tasks; namespace YouTrackSharp.Generated { @@ -6,10 +8,22 @@ public partial class YouTrackClient { private string _hubUrl; - internal string HubApiUrl + private async Task GetHubApiUrl() { - get { return _hubUrl; } - set { _hubUrl = value; } + if (_hubUrl == null) + { + var response = await ConfigAsync("ring(url)"); + var ringUrl = response.Ring?.Url; + + if (ringUrl == null) + { + throw new YouTrackErrorException(Strings.Exception_BadRequest, (int)HttpStatusCode.OK, + "Failed to find Hub url in YouTrack config", null, null); + } + + _hubUrl = ringUrl.TrimEnd('/') + "/api/rest/"; + } + return _hubUrl; } /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. @@ -19,9 +33,11 @@ internal string HubApiUrl { if (id == null) throw new System.ArgumentNullException("id"); + + var hubApiUrl = await GetHubApiUrl(); var urlBuilder_ = new System.Text.StringBuilder(); - urlBuilder_.Append(HubApiUrl != null ? HubApiUrl.TrimEnd('/') : "").Append("/users/{id}?"); + urlBuilder_.Append(hubApiUrl != null ? hubApiUrl.TrimEnd('/') : "").Append("/users/{id}?"); urlBuilder_.Replace("{id}", System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture))); if (fields != null) { @@ -93,8 +109,10 @@ internal string HubApiUrl /// A server side error occurred. internal async System.Threading.Tasks.Task HubApiUsersGetAsync(string query = null, string fields = null, int? skip = null, int? top = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { + var hubApiUrl = await GetHubApiUrl(); var urlBuilder_ = new System.Text.StringBuilder(); - urlBuilder_.Append(HubApiUrl != null ? HubApiUrl.TrimEnd('/') : "").Append("/users?"); + + urlBuilder_.Append(hubApiUrl != null ? hubApiUrl.TrimEnd('/') : "").Append("/users?"); if (query != null) { urlBuilder_.Append(System.Uri.EscapeDataString("query") + "=").Append(System.Uri.EscapeDataString(ConvertToString(query, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); @@ -178,7 +196,9 @@ internal string HubApiUrl internal async System.Threading.Tasks.Task HubUsersPostAsync(string fields = null, HubApiUser body = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { var urlBuilder_ = new System.Text.StringBuilder(); - urlBuilder_.Append(HubApiUrl != null ? HubApiUrl.TrimEnd('/') : "").Append("/users?failOnPermissionReduce=true&"); + var hubApiUrl = await GetHubApiUrl(); + + urlBuilder_.Append(hubApiUrl != null ? hubApiUrl.TrimEnd('/') : "").Append("/users?failOnPermissionReduce=true&"); if (fields != null) { urlBuilder_.Append(System.Uri.EscapeDataString("fields") + "=").Append(System.Uri.EscapeDataString(ConvertToString(fields, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); @@ -255,8 +275,10 @@ internal string HubApiUrl if (id == null) throw new System.ArgumentNullException("id"); + var hubApiUrl = await GetHubApiUrl(); + var urlBuilder_ = new System.Text.StringBuilder(); - urlBuilder_.Append(HubApiUrl != null ? HubApiUrl.TrimEnd('/') : "").Append("/users/{id}?failOnPermissionReduce=true&"); + urlBuilder_.Append(hubApiUrl != null ? hubApiUrl.TrimEnd('/') : "").Append("/users/{id}?failOnPermissionReduce=true&"); urlBuilder_.Replace("{id}", System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture))); if (fields != null) { @@ -334,8 +356,10 @@ internal async System.Threading.Tasks.Task HubUsersMergePostAsync(st string banBadge = null, string banReason = null, string twoFactorAuthentication = null, ICollection body = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { + var hubApiUrl = await GetHubApiUrl(); + var urlBuilder_ = new System.Text.StringBuilder(); - urlBuilder_.Append(HubApiUrl != null ? HubApiUrl.TrimEnd('/') : "").Append("/users/merge?failOnPermissionReduce=true&"); + urlBuilder_.Append(hubApiUrl != null ? hubApiUrl.TrimEnd('/') : "").Append("/users/merge?failOnPermissionReduce=true&"); if (fields != null) { urlBuilder_.Append(System.Uri.EscapeDataString("fields") + "=").Append(System.Uri.EscapeDataString(ConvertToString(fields, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); @@ -442,9 +466,11 @@ internal async System.Threading.Tasks.Task HubUsersMergePostAsync(st if (successorId == null) throw new System.ArgumentNullException("successorId"); + + var hubApiUrl = await GetHubApiUrl(); var urlBuilder_ = new System.Text.StringBuilder(); - urlBuilder_.Append(HubApiUrl != null ? HubApiUrl.TrimEnd('/') : "").Append("/users/{id}?failOnPermissionReduce=true&"); + urlBuilder_.Append(hubApiUrl != null ? hubApiUrl.TrimEnd('/') : "").Append("/users/{id}?failOnPermissionReduce=true&"); urlBuilder_.Replace("{id}", System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture))); urlBuilder_.Append(System.Uri.EscapeDataString("successor") + "=").Append(System.Uri.EscapeDataString(ConvertToString(successorId, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); @@ -509,8 +535,10 @@ internal async System.Threading.Tasks.Task HubUsersMergePostAsync(st if (id == null) throw new System.ArgumentNullException("id"); + var hubApiUrl = await GetHubApiUrl(); + var urlBuilder_ = new System.Text.StringBuilder(); - urlBuilder_.Append(HubApiUrl != null ? HubApiUrl.TrimEnd('/') : "").Append("/userdetails/{id}?failOnPermissionReduce=true&"); + urlBuilder_.Append(hubApiUrl != null ? hubApiUrl.TrimEnd('/') : "").Append("/userdetails/{id}?failOnPermissionReduce=true&"); urlBuilder_.Replace("{id}", System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture))); if (fields != null) { @@ -585,8 +613,10 @@ internal async System.Threading.Tasks.Task HubUsersMergePostAsync(st /// A server side error occurred. internal async System.Threading.Tasks.Task HubApiUsergroupsGetAsync(string query = null, string fields = null, int? skip = null, int? top = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { + var hubApiUrl = await GetHubApiUrl(); + var urlBuilder_ = new System.Text.StringBuilder(); - urlBuilder_.Append(HubApiUrl != null ? HubApiUrl.TrimEnd('/') : "").Append("/usergroups?"); + urlBuilder_.Append(hubApiUrl != null ? hubApiUrl.TrimEnd('/') : "").Append("/usergroups?"); if (fields != null) { urlBuilder_.Append(System.Uri.EscapeDataString("fields") + "=").Append(System.Uri.EscapeDataString(ConvertToString(fields, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); @@ -672,8 +702,10 @@ internal async System.Threading.Tasks.Task HubUsersMergePostAsync(st if (id == null) throw new System.ArgumentNullException("id"); + var hubApiUrl = await GetHubApiUrl(); + var urlBuilder_ = new System.Text.StringBuilder(); - urlBuilder_.Append(HubApiUrl != null ? HubApiUrl.TrimEnd('/') : "").Append("/usergroups/{id}/users?failOnPermissionReduce=true&"); + urlBuilder_.Append(hubApiUrl != null ? hubApiUrl.TrimEnd('/') : "").Append("/usergroups/{id}/users?failOnPermissionReduce=true&"); urlBuilder_.Replace("{id}", System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture))); if (fields != null) { @@ -754,8 +786,10 @@ internal async System.Threading.Tasks.Task HubUsersMergePostAsync(st if (userId == null) throw new System.ArgumentNullException("userId"); + var hubApiUrl = await GetHubApiUrl(); + var urlBuilder_ = new System.Text.StringBuilder(); - urlBuilder_.Append(HubApiUrl != null ? HubApiUrl.TrimEnd('/') : "").Append($"/usergroups/{id}/users/{userId}?failOnPermissionReduce=true&"); + urlBuilder_.Append(hubApiUrl != null ? hubApiUrl.TrimEnd('/') : "").Append($"/usergroups/{id}/users/{userId}?failOnPermissionReduce=true&"); urlBuilder_.Replace("{id}", System.Uri.EscapeDataString(ConvertToString(id, System.Globalization.CultureInfo.InvariantCulture))); urlBuilder_.Replace("{userId}", System.Uri.EscapeDataString(ConvertToString(userId, System.Globalization.CultureInfo.InvariantCulture))); var client_ = _httpClient; diff --git a/src/YouTrackSharp/Generated/YouTrackSharp.Api.cs b/src/YouTrackSharp/Generated/YouTrackSharp.Api.cs index d68ff90..d00f009 100644 --- a/src/YouTrackSharp/Generated/YouTrackSharp.Api.cs +++ b/src/YouTrackSharp/Generated/YouTrackSharp.Api.cs @@ -27,17 +27,8 @@ public partial class YouTrackClient public string BaseUrl { - get - { - if (_baseUrl == null) - { - _baseUrl = _httpClient.BaseAddress.ToString().TrimEnd('/') + "/api/"; - } - - return _baseUrl; - } - - set { _baseUrl = value; } + get => _baseUrl ?? (_baseUrl = _httpClient.BaseAddress.ToString().TrimEnd('/') + "/api/"); + set => _baseUrl = value; } } diff --git a/src/YouTrackSharp/TimeTracking/WorkItem.cs b/src/YouTrackSharp/TimeTracking/WorkItem.cs index dfc7c09..3e98386 100644 --- a/src/YouTrackSharp/TimeTracking/WorkItem.cs +++ b/src/YouTrackSharp/TimeTracking/WorkItem.cs @@ -45,7 +45,6 @@ internal static WorkItem FromApiEntity(IssueWorkItem entity) return new WorkItem() { Id = entity.Id, - //TODO null safety since date _really_ could be null due to quirks in old data Date = entity.Date?.TimestampToDateTime(), Duration = entity.Duration.Minutes.MinutesToTimeSpan(), Description = entity.Text, @@ -61,7 +60,6 @@ internal IssueWorkItem ToApiEntity() { var entity = new IssueWorkItem() { - //TODO null safety since it's user-provided data Date = Date.DateTimeToUnixTimestamp(), Duration = new DurationValue() {Minutes = (int)Duration.TotalMinutes}, Text = Description,