Skip to content

Commit 02457d3

Browse files
committed
fix: normalize base url
1 parent b366aa9 commit 02457d3

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/Client.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ public class UnsupportedFormatError: UapiError { public UnsupportedFormatError(s
3333
public class Client {
3434
private readonly HttpClient _http;
3535
public Client(string baseUrl, string? token = null) {
36-
_http = new HttpClient { BaseAddress = new Uri(baseUrl) };
36+
var normalized = baseUrl.EndsWith("/") ? baseUrl : baseUrl + "/";
37+
_http = new HttpClient { BaseAddress = new Uri(normalized) };
3738
if (!string.IsNullOrEmpty(token)) _http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
3839
}
3940
private async Task<object?> RequestAsync(string method, string path, Dictionary<string, object?>? query = null, object? body = null) {
40-
var uri = path + (query is null ? "" : "?" + string.Join("&", query.Select(kv => $"{kv.Key}={kv.Value}")));
41+
var relative = path.TrimStart('/');
42+
var qs = query is null || query.Count == 0
43+
? ""
44+
: "?" + string.Join("&", query.Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value?.ToString() ?? "")}"));
45+
var uri = relative + qs;
4146
var msg = new HttpRequestMessage(new HttpMethod(method), uri);
4247
if (body is not null) msg.Content = JsonContent.Create(body);
4348
var res = await _http.SendAsync(msg);

uapi-sdk-csharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<ImplicitUsings>enable</ImplicitUsings>
55
<Nullable>enable</Nullable>
66
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
7-
<Version>0.1.1</Version>
7+
<Version>0.1.2</Version>
88
</PropertyGroup>
99
<ItemGroup>
1010
<Compile Include="src/**/*.cs" />

0 commit comments

Comments
 (0)