Skip to content

Commit 71ecf17

Browse files
committedMay 20, 2019
Adding possibility to change Kinvey api version.
1 parent cb8ce6b commit 71ecf17

9 files changed

+174
-10
lines changed
 

‎Kinvey.Core/Auth/KinveyAuthRequest.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,14 @@ private HttpRequestMessage BuildRestRequest()
190190

191191
foreach (var header in kinveyHeaders)
192192
{
193-
request.Headers.Add(header.Key, header.Value.FirstOrDefault());
193+
var key = header.Key;
194+
var value = header.Value.FirstOrDefault();
195+
196+
if (key.Equals(Constants.STR_REQUEST_HEADER_API_VERSION) && !string.IsNullOrEmpty(client.ApiVersion) && !value.Equals(client.ApiVersion))
197+
{
198+
value = client.ApiVersion;
199+
}
200+
request.Headers.Add(key, value);
194201
}
195202

196203
appKeyAuthentication.Authenticate(request);

‎Kinvey.Core/Client/AbstractKinveyClient.cs

+16-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public abstract class AbstractKinveyClient
4141
/// </summary>
4242
private HttpClient httpClient;
4343

44+
private string apiVersion;
45+
4446
private string clientAppVersion = null;
4547

4648
private JObject customRequestProperties = new JObject();
@@ -208,11 +210,21 @@ public HttpClient HttpClient
208210
get { return this.httpClient; }
209211
}
210212

211-
/// <summary>
212-
/// Initializes the request.
213+
/// <summary>
214+
/// Gets Kinvey api version.
213215
/// </summary>
214-
/// <param name="request">Request.</param>
215-
/// <typeparam name="T">The Type of the response</typeparam>
216+
/// <value>Kinvey api version.</value>
217+
public string ApiVersion
218+
{
219+
get { return string.IsNullOrEmpty(apiVersion) ? KinveyHeaders.kinveyApiVersion: apiVersion; }
220+
internal set { this.apiVersion = value; }
221+
}
222+
223+
/// <summary>
224+
/// Initializes the request.
225+
/// </summary>
226+
/// <param name="request">Request.</param>
227+
/// <typeparam name="T">The Type of the response</typeparam>
216228
public void InitializeRequest<T>(AbstractKinveyClientRequest<T> request, string clientID = null)
217229
{
218230
if (RequestInitializer != null)

‎Kinvey.Core/Client/Client.cs

+14
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ private string filePath
167167

168168
private Constants.DevicePlatform devicePlatform;
169169

170+
private string apiVersion;
171+
170172
/// <summary>
171173
/// Initializes a new instance of the <see cref="T:KinveyXamarin.Client.Builder"/> class.
172174
/// </summary>
@@ -211,6 +213,7 @@ public virtual Client Build()
211213
c.logger = this.log;
212214
c.senderID = this.senderID;
213215
c.SSOGroupKey = this.ssoGroupKey;
216+
c.ApiVersion = apiVersion;
214217
if (!string.IsNullOrEmpty(this.MICHostName)) c.MICHostName = this.MICHostName;
215218
if (!string.IsNullOrEmpty(instanceID))
216219
{
@@ -335,6 +338,17 @@ public Builder SetInstanceID(string instanceID)
335338
this.setBaseURL(url);
336339
return this;
337340
}
341+
342+
/// <summary>
343+
/// Sets Kinvey api version.
344+
/// </summary>
345+
/// <returns>The current builder.</returns>
346+
/// <param name="version">Kinvey api version.</param>
347+
public Builder SetApiVersion(string version)
348+
{
349+
apiVersion = version;
350+
return this;
351+
}
338352
}
339353
}
340354
}

‎Kinvey.Core/Core/AbstractKinveyClientRequest.cs

+6
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ public HttpRequestMessage BuildRestRequest()
288288

289289
foreach (var header in requestHeaders)
290290
{
291+
if (header.Key.Equals(Constants.STR_REQUEST_HEADER_API_VERSION) && !string.IsNullOrEmpty(client.ApiVersion) && !header.Value.FirstOrDefault().Equals(client.ApiVersion))
292+
{
293+
request.Headers.Add(header.Key, client.ApiVersion);
294+
continue;
295+
}
296+
291297
if (header.Value.Count() > 1)
292298
{
293299
request.Headers.Add(header.Key, header.Value);

‎Kinvey.Core/Core/KinveyHeaders.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public class KinveyHeaders : List<KeyValuePair<string, IEnumerable<string>>>
2929
public static string VERSION = "4.0.1";
3030

3131
// The kinvey API version.
32-
static string kinveyApiVersionKey = "X-Kinvey-API-Version";
33-
static string kinveyApiVersion = "4";
32+
internal static readonly string kinveyApiVersion = "4";
3433

3534
// The user agent.
3635
static string userAgentKey = "user-agent";
@@ -72,7 +71,7 @@ public KinveyHeaders(Constants.DevicePlatform devicePlatform)
7271
userAgentKey, new List<string> { userAgent }
7372
));
7473
Add(new KeyValuePair<string, IEnumerable<string>>(
75-
kinveyApiVersionKey, new List<string> { kinveyApiVersion }
74+
Constants.STR_REQUEST_HEADER_API_VERSION, new List<string> { kinveyApiVersion }
7675
));
7776

7877
JObject deviceInfo = new JObject();

‎Kinvey.Core/Utils/KinveyConstants.cs

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public static partial class Constants
7272

7373
// Request Headers
7474
internal const string STR_REQUEST_HEADER_DEVICE_INFO = "X-Kinvey-Device-Info";
75+
internal const string STR_REQUEST_HEADER_API_VERSION = "X-Kinvey-API-Version";
7576

7677
// X-Kinvey-Device-Info Strings
7778
internal const string STR_DEVICE_INFO_HEADER_KEY = "hv";

‎Kinvey.Tests/BaseTestClass.cs

+13
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public static class EnvironmentVariable
6868
private static readonly string REQUEST_START_HEADER = "X-Kinvey-Request-Start";
6969
private static readonly string DATE_FORMAT = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK";
7070

71+
private static readonly int MaxApiVersion = 5;
72+
7173
protected static HttpListener httpListener;
7274

7375
public void Delete(string fileName)
@@ -917,6 +919,17 @@ protected static void MockResponses(uint? expectedRequests = null, Client client
917919
count++;
918920
Console.WriteLine($"{count}");
919921

922+
var apiVersion = context.Request.Headers[Constants.STR_REQUEST_HEADER_API_VERSION];
923+
var version = 0;
924+
var isParsed = int.TryParse(apiVersion, out version);
925+
926+
if (!isParsed || (isParsed && version > MaxApiVersion))
927+
{
928+
context.Response.StatusCode = (int)HttpStatusCode.NotImplemented;
929+
Write(context, "Not implemented");
930+
continue;
931+
}
932+
920933
var authorization = context.Request.Headers["Authorization"];
921934
if (!context.Request.Url.LocalPath.StartsWith("/_uploadURL/", StringComparison.Ordinal) && !context.Request.Url.LocalPath.StartsWith("/_downloadURL/", StringComparison.Ordinal))
922935
{

‎Kinvey.Tests/ClientTests/ClientIntegrationTests.cs

+113-1
Original file line numberDiff line numberDiff line change
@@ -270,5 +270,117 @@ public async Task TestCustomEndpointBad()
270270
KinveyException ke = e as KinveyException;
271271
Assert.AreEqual(404, ke.StatusCode);
272272
}
273-
}
273+
274+
[TestMethod]
275+
public async Task TestSetApiVersionAuthRequests()
276+
{
277+
// Arrange
278+
var notSupportingApiVersion = int.MaxValue.ToString();
279+
280+
var builder = new Client.Builder(AppKey, AppSecret);
281+
builder.SetFilePath(TestSetup.db_dir);
282+
283+
if (MockData)
284+
{
285+
builder.setBaseURL("http://localhost:8080");
286+
builder.setMICHostName("http://localhost:8081");
287+
}
288+
289+
var client1 = builder.Build();
290+
291+
builder.SetApiVersion(notSupportingApiVersion);
292+
var client2 = builder.Build();
293+
294+
builder.SetApiVersion(KinveyHeaders.kinveyApiVersion);
295+
var client3 = builder.Build();
296+
297+
if (MockData)
298+
{
299+
MockResponses(3);
300+
}
301+
302+
// Act
303+
var userForClient1 = await User.LoginAsync(client1);
304+
305+
var exception = await Assert.ThrowsExceptionAsync<KinveyException>(async delegate
306+
{
307+
await User.LoginAsync(client2);
308+
});
309+
310+
var userForClient3 = await User.LoginAsync(client3);
311+
312+
// Assert
313+
Assert.AreEqual(typeof(KinveyException), exception.GetType());
314+
var kinveyException = exception as KinveyException;
315+
Assert.IsTrue(kinveyException.ErrorCategory == EnumErrorCategory.ERROR_BACKEND);
316+
Assert.IsTrue(kinveyException.ErrorCode == EnumErrorCode.ERROR_JSON_RESPONSE);
317+
318+
Assert.IsNotNull(client1.ActiveUser);
319+
Assert.IsTrue(userForClient1.Active);
320+
321+
Assert.IsNotNull(client3.ActiveUser);
322+
Assert.IsTrue(userForClient3.Active);
323+
324+
Assert.AreEqual(KinveyHeaders.kinveyApiVersion, client1.ApiVersion);
325+
Assert.AreEqual(notSupportingApiVersion, client2.ApiVersion);
326+
Assert.AreEqual(KinveyHeaders.kinveyApiVersion, client3.ApiVersion);
327+
}
328+
329+
[TestMethod]
330+
public async Task TestSetApiVersionKinveyClientRequests()
331+
{
332+
// Arrange
333+
var notSupportingApiVersion = int.MaxValue.ToString();
334+
335+
var builder = new Client.Builder(AppKey, AppSecret);
336+
builder.SetFilePath(TestSetup.db_dir);
337+
338+
if (MockData)
339+
{
340+
builder.setBaseURL("http://localhost:8080");
341+
builder.setMICHostName("http://localhost:8081");
342+
}
343+
344+
var client1 = builder.Build();
345+
346+
builder.SetApiVersion(notSupportingApiVersion);
347+
var client2 = builder.Build();
348+
349+
builder.SetApiVersion(KinveyHeaders.kinveyApiVersion);
350+
var client3 = builder.Build();
351+
352+
if (MockData)
353+
{
354+
MockResponses(3);
355+
}
356+
357+
// Act
358+
var pingResponse1 = await client1.PingAsync();
359+
360+
var exception = await Assert.ThrowsExceptionAsync<KinveyException>(async delegate
361+
{
362+
await client2.PingAsync();
363+
});
364+
365+
var pingResponse3 = await client3.PingAsync();
366+
367+
// Assert
368+
Assert.AreEqual(typeof(KinveyException), exception.GetType());
369+
var kinveyException = exception as KinveyException;
370+
Assert.IsTrue(kinveyException.ErrorCategory == EnumErrorCategory.ERROR_BACKEND);
371+
Assert.IsTrue(kinveyException.ErrorCode == EnumErrorCode.ERROR_JSON_RESPONSE);
372+
373+
Assert.IsNotNull(pingResponse1.kinvey);
374+
Assert.IsTrue(pingResponse1.kinvey.StartsWith("hello", StringComparison.Ordinal));
375+
Assert.IsNotNull(pingResponse1.version);
376+
377+
Assert.IsNotNull(pingResponse3.kinvey);
378+
Assert.IsTrue(pingResponse3.kinvey.StartsWith("hello", StringComparison.Ordinal));
379+
Assert.IsNotNull(pingResponse3.version);
380+
381+
Assert.AreEqual(KinveyHeaders.kinveyApiVersion, client1.ApiVersion);
382+
Assert.AreEqual(notSupportingApiVersion, client2.ApiVersion);
383+
Assert.AreEqual(KinveyHeaders.kinveyApiVersion, client3.ApiVersion);
384+
}
385+
}
274386
}

‎sqlite-net

0 commit comments

Comments
 (0)
Please sign in to comment.