Skip to content

Commit 84db65a

Browse files
committed
feat: allow /v1 or /v2 prefixes in path
Signed-off-by: Adrien Barreau <[email protected]>
1 parent c33c7f4 commit 84db65a

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

csharp-ovh/Client/Client.cs

+19-7
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,22 @@ public static string GenerateSignature(string applicationSecret, string consumer
238238
return $"$1${signature}";
239239
}
240240

241+
private string GetTarget(string path)
242+
{
243+
if (path.StartsWith("/"))
244+
{
245+
path = path.Substring(1);
246+
}
247+
248+
var endpoint = Endpoint.ToString();
249+
if (endpoint.EndsWith("/1.0/") && (path.StartsWith("v1/") || path.StartsWith("v2/")))
250+
{
251+
endpoint = endpoint.Substring(0, endpoint.Length - 4);
252+
}
253+
254+
return new Uri(endpoint) + path;
255+
}
256+
241257
private async Task SetHeaders(HttpRequestMessage msg, string method, string target, string data, bool needAuth, bool isBatch = false)
242258
{
243259
var headers = msg.Headers;
@@ -317,23 +333,19 @@ public async Task<long> GetTimeDelta()
317333
/// <exception cref="InvalidResponseException">when API response could not be decoded</exception>
318334
private async Task<string> CallAsync(string method, string path, string data = null, bool needAuth = true, bool isBatch = false, TimeSpan? timeout = null)
319335
{
320-
if (path.StartsWith("/"))
321-
{
322-
path = path.Substring(1);
323-
}
324-
325336
HttpResponseMessage response = null;
326337

327338
try
328339
{
329340
var httpMethod = new HttpMethod(method);
330-
HttpRequestMessage msg = new HttpRequestMessage(httpMethod, new Uri(Endpoint + path));
341+
var target = GetTarget(path);
342+
HttpRequestMessage msg = new HttpRequestMessage(httpMethod, target);
331343
if (httpMethod != HttpMethod.Get && data != null)
332344
{
333345
msg.Content = new StringContent(data ?? "", Encoding.UTF8, "application/json");
334346
}
335347

336-
await SetHeaders(msg, method, Endpoint + path, data, needAuth, isBatch).ConfigureAwait(false);
348+
await SetHeaders(msg, method, target.ToString(), data, needAuth, isBatch).ConfigureAwait(false);
337349

338350
using (var cts = new CancellationTokenSource(timeout ?? _defaultTimeout))
339351
{

csharp-ovh/csharp-ovh.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<AssemblyName>csharp-ovh</AssemblyName>
66
<Title>OVH C# API Wrapper</Title>
77
<PackageVersion>4.0.5</PackageVersion>

test/GetRequests.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ public async Task GET_auth_time()
4545
}
4646

4747
[Test]
48-
public async Task GET_me_as_string()
48+
[TestCase("/me", "https://eu.api.ovh.com/1.0/me", "$1$dfe0b86bf2ab0d9eb3f785dc1ab00de58984d80c")]
49+
[TestCase("/v1/me", "https://eu.api.ovh.com/v1/me", "$1$b6849b8a25d6bc46c6ad1dfb0fc67d07db9553a3")]
50+
[TestCase("/v2/me", "https://eu.api.ovh.com/v2/me", "$1$291bb7bdbef11b1050200a109a4fe5109ed96cdd")]
51+
public async Task GET_me_as_string(string call, string called, string sig)
4952
{
5053
var fake = A.Fake<FakeHttpMessageHandler>(a => a.CallsBaseMethods());
5154
MockAuthTimeCallWithFakeItEasy(fake);
@@ -57,20 +60,25 @@ public async Task GET_me_as_string()
5760

5861

5962
var c = ClientFactory.GetClient(fake);
60-
var result = await c.GetAsync("/me");
63+
64+
var result = await c.GetAsync(call);
6165
Assert.AreEqual(Responses.Get.me_content, result);
6266

6367
var meCall = Fake.GetCalls(fake).Where(call =>
6468
call.Method.Name == "Send" &&
6569
call.GetArgument<HttpRequestMessage>("request").RequestUri.ToString().Contains("/me")).First();
6670

6771
var requestMessage = meCall.GetArgument<HttpRequestMessage>("request");
72+
73+
var uri = requestMessage.RequestUri;
74+
Assert.AreEqual(called, uri.AbsoluteUri);
75+
6876
var headers = requestMessage.Headers;
6977
Assert.Multiple(() => {
7078
Assert.AreEqual(Constants.APPLICATION_KEY, headers.GetValues(Client.OVH_APP_HEADER).First());
7179
Assert.AreEqual(Constants.CONSUMER_KEY, headers.GetValues(Client.OVH_CONSUMER_HEADER).First());
7280
Assert.AreEqual(currentServerTimestamp.ToString(), headers.GetValues(Client.OVH_TIME_HEADER).First());
73-
Assert.AreEqual("$1$dfe0b86bf2ab0d9eb3f785dc1ab00de58984d80c", headers.GetValues(Client.OVH_SIGNATURE_HEADER).First());
81+
Assert.AreEqual(sig, headers.GetValues(Client.OVH_SIGNATURE_HEADER).First());
7482
});
7583
}
7684

test/test.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageReference Include="FakeItEasy" Version="5.1.1" />
@@ -12,4 +12,4 @@
1212
<ItemGroup>
1313
<ProjectReference Include="..\csharp-ovh\csharp-ovh.csproj" />
1414
</ItemGroup>
15-
</Project>
15+
</Project>

0 commit comments

Comments
 (0)