mirror of
https://dev.azure.com/tstanciu94/PhantomMind/_git/Bitip
synced 2025-10-13 01:52:19 +03:00
fix: update RealApiBaseUrl and improve URL handling in BitipClient
This commit is contained in:
parent
c27573e95a
commit
d89a57cb9e
@ -15,7 +15,7 @@ namespace Bitip.Client.Tests
|
||||
/// <summary>
|
||||
/// Real Bitip API base URL for integration tests.
|
||||
/// </summary>
|
||||
public const string RealApiBaseUrl = "http://localhost:5172/api/";
|
||||
public const string RealApiBaseUrl = "http://localhost:5172";
|
||||
|
||||
/// <summary>
|
||||
/// Real API key for integration tests.
|
||||
|
@ -1,6 +1,7 @@
|
||||
// Copyright (c) 2025 Tudor Stanciu
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bitip.Client.Helpers
|
||||
{
|
||||
@ -13,5 +14,47 @@ namespace Bitip.Client.Helpers
|
||||
var address = trimmed.EndsWith("/") ? trimmed : $"{trimmed}/";
|
||||
return new Uri(address);
|
||||
}
|
||||
|
||||
public static Uri EnsureTrailingSlash(string url, string suffix)
|
||||
{
|
||||
if (url is null) throw new ArgumentNullException(nameof(url));
|
||||
if (url is null) throw new ArgumentNullException(nameof(url));
|
||||
if (string.IsNullOrWhiteSpace(suffix)) return EnsureTrailingSlash(url);
|
||||
|
||||
var normalizedSuffix = suffix.Trim('/');
|
||||
if (normalizedSuffix.Length == 0) return EnsureTrailingSlash(url);
|
||||
|
||||
// Get last non-empty segment from the url
|
||||
var parts = url.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
||||
var lastSegment = parts.Length == 0 ? string.Empty : parts.Last();
|
||||
|
||||
if (!string.Equals(lastSegment, normalizedSuffix, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
url = PathCombine(url, normalizedSuffix);
|
||||
}
|
||||
|
||||
return EnsureTrailingSlash(url);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combines multiple URL segments into a single path, ensuring proper slashes.
|
||||
/// </summary>
|
||||
public static string PathCombine(params string[] segments) // Move it to Netmash
|
||||
{
|
||||
var parts = segments
|
||||
.Where(s => !string.IsNullOrEmpty(s))
|
||||
.Select(s => s.Trim())
|
||||
.ToArray();
|
||||
if (parts.Length == 0) return string.Empty;
|
||||
if (parts.Length == 1) return parts[0];
|
||||
|
||||
var result = parts[0].TrimEnd('/');
|
||||
for (int i = 1; i < parts.Length; i++)
|
||||
{
|
||||
var seg = parts[i].Trim('/');
|
||||
if (seg.Length > 0) result += "/" + seg;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ namespace Bitip.Client.Services
|
||||
|
||||
public BitipClient(HttpClient httpClient, IOptions<BitipOptions> options)
|
||||
{
|
||||
httpClient.BaseAddress = UriNormalizer.EnsureTrailingSlash(options.Value.BaseUrl);
|
||||
httpClient.BaseAddress = UriNormalizer.EnsureTrailingSlash(options.Value.BaseUrl, "/api");
|
||||
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
httpClient.DefaultRequestHeaders.Add(ApiKeys.HttpHeader, options.Value.ApiKey);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user