Skip to content

Commit

Permalink
improve usage of YouTrackClient wrt Hub url
Browse files Browse the repository at this point in the history
  • Loading branch information
rekolobov committed Aug 6, 2021
1 parent 590ae98 commit 0d77131
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
6 changes: 1 addition & 5 deletions src/YouTrackSharp/BearerTokenConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,11 @@ public override async Task<YouTrackClient> 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)
Expand Down
60 changes: 47 additions & 13 deletions src/YouTrackSharp/Generated/Hub.Api.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;

namespace YouTrackSharp.Generated
{
public partial class YouTrackClient
{
private string _hubUrl;

internal string HubApiUrl
private async Task<string> 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;
}

/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
Expand All @@ -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)
{
Expand Down Expand Up @@ -93,8 +109,10 @@ internal string HubApiUrl
/// <exception cref="YouTrackErrorException">A server side error occurred.</exception>
internal async System.Threading.Tasks.Task<HubApiUsersPage> 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("&");
Expand Down Expand Up @@ -178,7 +196,9 @@ internal string HubApiUrl
internal async System.Threading.Tasks.Task<HubApiUser> 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("&");
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -334,8 +356,10 @@ internal async System.Threading.Tasks.Task<HubApiUser> HubUsersMergePostAsync(st
string banBadge = null, string banReason = null, string twoFactorAuthentication = null,
ICollection<HubApiUser> 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("&");
Expand Down Expand Up @@ -442,9 +466,11 @@ internal async System.Threading.Tasks.Task<HubApiUser> 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("&");

Expand Down Expand Up @@ -509,8 +535,10 @@ internal async System.Threading.Tasks.Task<HubApiUser> 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)
{
Expand Down Expand Up @@ -585,8 +613,10 @@ internal async System.Threading.Tasks.Task<HubApiUser> HubUsersMergePostAsync(st
/// <exception cref="YouTrackErrorException">A server side error occurred.</exception>
internal async System.Threading.Tasks.Task<HubApiUsergroupsPage> 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("&");
Expand Down Expand Up @@ -672,8 +702,10 @@ internal async System.Threading.Tasks.Task<HubApiUser> 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)
{
Expand Down Expand Up @@ -754,8 +786,10 @@ internal async System.Threading.Tasks.Task<HubApiUser> 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;
Expand Down
13 changes: 2 additions & 11 deletions src/YouTrackSharp/Generated/YouTrackSharp.Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/YouTrackSharp/TimeTracking/WorkItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 0d77131

Please sign in to comment.