| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.Search.Serialization |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using Models; |
| | 9 | | using Newtonsoft.Json; |
| | 10 | |
|
| | 11 | | internal class SearchContinuationTokenConverter : JsonConverter |
| | 12 | | { |
| | 13 | | // MAINTENANCE NOTE: Remember to change this when the REST API version changes. |
| | 14 | | private const string TargetApiVersion = "2019-05-06"; |
| | 15 | |
|
| 0 | 16 | | public override bool CanConvert(Type objectType) => objectType == typeof(SearchContinuationToken); |
| | 17 | |
|
| | 18 | | public override object ReadJson( |
| | 19 | | JsonReader reader, |
| | 20 | | Type objectType, |
| | 21 | | object existingValue, |
| | 22 | | JsonSerializer serializer) |
| | 23 | | { |
| 14 | 24 | | Payload payload = serializer.Deserialize<Payload>(reader); |
| | 25 | |
|
| | 26 | | Uri nextLinkUri; |
| | 27 | | try |
| | 28 | | { |
| 14 | 29 | | nextLinkUri = new Uri(payload.NextLink); |
| 12 | 30 | | } |
| 2 | 31 | | catch (FormatException e) |
| | 32 | | { |
| 2 | 33 | | throw new JsonSerializationException( |
| 2 | 34 | | "Cannot deserialize continuation token. Failed to parse nextLink because it is not a valid URL.", |
| 2 | 35 | | e); |
| | 36 | | } |
| | 37 | |
|
| 12 | 38 | | string apiVersion = ParseApiVersion(nextLinkUri.Query); |
| | 39 | |
|
| 12 | 40 | | if (string.IsNullOrWhiteSpace(apiVersion)) |
| | 41 | | { |
| 6 | 42 | | throw new JsonSerializationException( |
| 6 | 43 | | "Cannot deserialize continuation token because the api-version is missing."); |
| | 44 | | } |
| | 45 | |
|
| 6 | 46 | | if (apiVersion != TargetApiVersion) |
| | 47 | | { |
| 2 | 48 | | string message = |
| 2 | 49 | | "Cannot deserialize a continuation token for a different api-version. Token contains version " + |
| 2 | 50 | | $"'{apiVersion}'; Expected version '{TargetApiVersion}'."; |
| | 51 | |
|
| 2 | 52 | | throw new JsonSerializationException(message); |
| | 53 | | } |
| | 54 | |
|
| 4 | 55 | | return new SearchContinuationToken(payload.NextLink, payload.NextPageParameters); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
| | 59 | | { |
| 4 | 60 | | var token = (SearchContinuationToken)value; |
| | 61 | |
|
| 4 | 62 | | var payload = |
| 4 | 63 | | new Payload() |
| 4 | 64 | | { |
| 4 | 65 | | NextLink = token.NextLink, |
| 4 | 66 | | NextPageParameters = token.NextPageParameters |
| 4 | 67 | | }; |
| | 68 | |
|
| 4 | 69 | | serializer.Serialize(writer, payload); |
| 4 | 70 | | } |
| | 71 | |
|
| | 72 | | private static string ParseApiVersion(string query) |
| | 73 | | { |
| 12 | 74 | | if (String.IsNullOrWhiteSpace(query)) |
| | 75 | | { |
| 2 | 76 | | return null; |
| | 77 | | } |
| | 78 | |
|
| 10 | 79 | | if (query[0] == '?') |
| | 80 | | { |
| 10 | 81 | | query = query.Substring(1); |
| | 82 | | } |
| | 83 | |
|
| 10 | 84 | | string[] pairs = query.Split('&'); |
| | 85 | |
|
| 52 | 86 | | foreach (string pair in pairs) |
| | 87 | | { |
| 20 | 88 | | string[] nameAndValue = pair.Split('='); |
| | 89 | |
|
| 20 | 90 | | if (nameAndValue.Length == 2 && nameAndValue[0] == "api-version") |
| | 91 | | { |
| 8 | 92 | | return nameAndValue[1]; |
| | 93 | | } |
| | 94 | | } |
| | 95 | |
|
| 2 | 96 | | return null; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | private class Payload |
| | 100 | | { |
| | 101 | | [JsonProperty("@odata.nextLink")] |
| 40 | 102 | | public string NextLink { get; set; } |
| | 103 | |
|
| | 104 | | [JsonProperty("@search.nextPageParameters")] |
| 30 | 105 | | public SearchRequest NextPageParameters { get; set; } |
| | 106 | | } |
| | 107 | | } |
| | 108 | | } |