Skip to content

Commit 325bccb

Browse files
authored
feat(dapi): add data api endpoint version to connection info (#372)
Add the DataApi target version to the ConnectionInfo object Use the target version to calculate the DataApi endpoints
1 parent 746d4bc commit 325bccb

4 files changed

Lines changed: 122 additions & 7 deletions

File tree

src/FMData.Rest/FileMakerRestClient.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class FileMakerRestClient : FileMakerApiClientBase, IFileMakerRestClient
5252

5353
private readonly IAuthTokenProvider _authTokenProvider;
5454
private readonly bool _useNewClientForContainers = false;
55+
private readonly string _targetVersion = "v1";
5556

5657
#region Constructors
5758
/// <summary>
@@ -98,6 +99,21 @@ public FileMakerRestClient(
9899
{
99100
_authTokenProvider = authTokenProvider;
100101
_useNewClientForContainers = useNewClientForContainers;
102+
switch (_authTokenProvider.ConnectionInfo?.RestTargetVersion)
103+
{
104+
case RestTargetVersion.v1:
105+
_targetVersion = "v1";
106+
break;
107+
case RestTargetVersion.v2:
108+
_targetVersion = "v2";
109+
break;
110+
case RestTargetVersion.vLatest:
111+
_targetVersion = "vLatest";
112+
break;
113+
default:
114+
_targetVersion = "v1";
115+
break;
116+
}
101117
#if NETSTANDARD1_3
102118
var header = new System.Net.Http.Headers.ProductHeaderValue("FMData.Rest", "4");
103119
var userAgent = new System.Net.Http.Headers.ProductInfoHeaderValue(header);
@@ -116,7 +132,7 @@ public FileMakerRestClient(
116132
/// <summary>
117133
/// Note we assume _fmsUri has no trailing slash as its cut off in the constructor.
118134
/// </summary>
119-
private string BaseEndPoint => $"{FmsUri}/fmi/data/v1/databases/{FileName}";
135+
private string BaseEndPoint => $"{FmsUri}/fmi/data/{_targetVersion}/databases/{FileName}";
120136

121137
/// <summary>
122138
/// Generate the appropriate Authentication endpoint uri for this instance of the data client.
@@ -639,7 +655,7 @@ public override async Task<string> RunScriptAsync(string layout, string script,
639655
await UpdateTokenDateAsync().ConfigureAwait(false); // we're about to use the token so update date used
640656

641657
// generate request url
642-
var uri = $"{FmsUri}/fmi/data/v1"
658+
var uri = $"{FmsUri}/fmi/data/{_targetVersion}"
643659
+ $"/databases/{Uri.EscapeDataString(FileName)}"
644660
+ $"/layouts/{Uri.EscapeDataString(layout)}"
645661
+ $"/script/{Uri.EscapeDataString(script)}";
@@ -835,7 +851,7 @@ public override async Task<IResponse> SetGlobalFieldAsync(string baseTable, stri
835851
public override async Task<ProductInformation> GetProductInformationAsync()
836852
{
837853
// generate request url
838-
var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{FmsUri}/fmi/data/v1/productinfo");
854+
var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{FmsUri}/fmi/data/{_targetVersion}/productinfo");
839855

840856
// run the patch action
841857
var response = await Client.SendAsync(requestMessage).ConfigureAwait(false);
@@ -869,7 +885,7 @@ public override async Task<IReadOnlyCollection<string>> GetDatabasesAsync()
869885
// don't need to refresh the token, because this is a basic authentication request
870886

871887
// generate request url
872-
var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{FmsUri}/fmi/data/v1/databases");
888+
var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{FmsUri}/fmi/data/{_targetVersion}/databases");
873889

874890
// special non-token auth to list databases
875891
requestMessage.Headers.Authorization = await _authTokenProvider.GetAuthenticationHeaderValue().ConfigureAwait(false);
@@ -906,7 +922,7 @@ public override async Task<IReadOnlyCollection<LayoutListItem>> GetLayoutsAsync(
906922
await UpdateTokenDateAsync().ConfigureAwait(false); // we're about to use the token so update date used
907923

908924
// generate request url
909-
var uri = $"{FmsUri}/fmi/data/v1/"
925+
var uri = $"{FmsUri}/fmi/data/{_targetVersion}/"
910926
+ $"databases/{Uri.EscapeDataString(FileName)}/layouts";
911927
var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
912928

@@ -945,7 +961,7 @@ public override async Task<IReadOnlyCollection<ScriptListItem>> GetScriptsAsync(
945961
await UpdateTokenDateAsync().ConfigureAwait(false); // we're about to use the token so update date used
946962

947963
// generate request url
948-
var uri = $"{FmsUri}/fmi/data/v1"
964+
var uri = $"{FmsUri}/fmi/data/{_targetVersion}"
949965
+ $"/databases/{Uri.EscapeDataString(FileName)}/scripts";
950966
var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
951967

@@ -986,7 +1002,7 @@ public override async Task<LayoutMetadata> GetLayoutAsync(string layout, int? re
9861002
await UpdateTokenDateAsync().ConfigureAwait(false); // we're about to use the token so update date used
9871003

9881004
// generate request url
989-
var uri = $"{FmsUri}/fmi/data/v1"
1005+
var uri = $"{FmsUri}/fmi/data/{_targetVersion}"
9901006
+ $"/databases/{Uri.EscapeDataString(FileName)}"
9911007
+ $"/layouts/{Uri.EscapeDataString(layout)}";
9921008
if (recordId.HasValue)

src/FMData/ConnectionInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public class ConnectionInfo
2121
/// Password to use when making the connection.
2222
/// </summary>
2323
public string Password { get; set; }
24+
/// <summary>
25+
/// Gets or sets the <see cref="RestTargetVersion"/> to use.
26+
/// </summary>
27+
public RestTargetVersion? RestTargetVersion { get; set; }
2428

2529
#region FileMaker Cloud
2630

src/FMData/RestTargetVersion.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace FMData
2+
{
3+
/// <summary>
4+
/// All supported FileMaker DataAPI endpoint versions.
5+
/// </summary>
6+
public enum RestTargetVersion
7+
{
8+
/// <summary>
9+
/// Uses v1 endpoint of the DataAPI.
10+
/// </summary>
11+
v1,
12+
/// <summary>
13+
/// Uses v2 endpoint of the DataAPI.
14+
/// </summary>
15+
v2,
16+
/// <summary>
17+
/// Uses the latest endpoint version of the DataAPI.
18+
/// </summary>
19+
vLatest
20+
}
21+
}

tests/FMData.Rest.Tests/GeneralTests.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Globalization;
34
using System.Linq;
45
using System.Net.Http;
56
using System.Runtime.Serialization;
67
using System.Threading.Tasks;
78
using FMData.Rest.Requests;
89
using FMData.Rest.Tests.TestModels;
10+
using Newtonsoft.Json.Linq;
911
using RichardSzalay.MockHttp;
1012
using Xunit;
1113

@@ -197,5 +199,77 @@ public async Task Test_DateTime_To_Timestamp_Parsing()
197199
var responseDataContainsResult = response.Any(r => r.Created == DateTime.ParseExact("03/29/2018 15:22:09", "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture));
198200
Assert.True(responseDataContainsResult);
199201
}
202+
203+
204+
[Fact]
205+
public void Test_EndpointVersion_Default()
206+
{
207+
// arrange
208+
var expected = "http://localhost/fmi/data/v1/databases/test-file/sessions";
209+
var testClients = CreateEndpointTestClients(null);
210+
211+
// assert
212+
foreach (var client in testClients)
213+
{
214+
Assert.Equal(expected, client.AuthEndpoint());
215+
}
216+
}
217+
218+
[Fact]
219+
public void Test_EndpointVersion_v1()
220+
{
221+
// arrange
222+
var expected = "http://localhost/fmi/data/v1/databases/test-file/sessions";
223+
var testClients = CreateEndpointTestClients(RestTargetVersion.v1);
224+
225+
// assert
226+
foreach (var client in testClients)
227+
{
228+
Assert.Equal(expected, client.AuthEndpoint());
229+
}
230+
}
231+
232+
[Fact]
233+
public void Test_EndpointVersion_v2()
234+
{
235+
// arrange
236+
var expected = "http://localhost/fmi/data/v2/databases/test-file/sessions";
237+
var testClients = CreateEndpointTestClients(RestTargetVersion.v2);
238+
239+
// assert
240+
foreach (var client in testClients)
241+
{
242+
Assert.Equal(expected, client.AuthEndpoint());
243+
}
244+
}
245+
246+
[Fact]
247+
public void Test_EndpointVersion_vLatest()
248+
{
249+
// arrange
250+
var expected = "http://localhost/fmi/data/vLatest/databases/test-file/sessions";
251+
var testClients = CreateEndpointTestClients(RestTargetVersion.vLatest);
252+
253+
// assert
254+
foreach (var client in testClients)
255+
{
256+
Assert.Equal(expected, client.AuthEndpoint());
257+
}
258+
}
259+
260+
private IEnumerable<FileMakerRestClient> CreateEndpointTestClients(RestTargetVersion? targetVersion)
261+
{
262+
var mockHttp = new MockHttpMessageHandler();
263+
var server = "http://localhost";
264+
var file = "test-file";
265+
var user = "unit";
266+
var pass = "test";
267+
var connectionInfo = new ConnectionInfo { FmsUri = server, Database = file, Username = user, Password = pass, RestTargetVersion = targetVersion };
268+
269+
return [
270+
new(mockHttp.ToHttpClient(), connectionInfo),
271+
new(mockHttp.ToHttpClient(), new DefaultAuthTokenProvider(connectionInfo))
272+
];
273+
}
200274
}
201275
}

0 commit comments

Comments
 (0)