| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Text.Json; |
| | 6 | | using Azure.AI.FormRecognizer.Models; |
| | 7 | |
|
| | 8 | | namespace Azure.AI.FormRecognizer.Training |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Authorization for copying a custom model into the target Form Recognizer resource. |
| | 12 | | /// </summary> |
| | 13 | | public class CopyAuthorization |
| | 14 | | { |
| | 15 | | /// <summary>Model identifier in the target Form Recognizer Resource. </summary> |
| 52 | 16 | | public string ModelId { get; } |
| | 17 | | /// <summary> The time when the access token expires. The date is represented as the number of seconds from 1970 |
| 28 | 18 | | public DateTimeOffset ExpiresOn { get; } |
| | 19 | | /// <summary> Token claim used to authorize the request. </summary> |
| 28 | 20 | | internal string AccessToken { get; } |
| | 21 | | /// <summary> Azure Resource Id of the target Form Recognizer resource where the model is copied to. </summary> |
| 28 | 22 | | internal string ResourceId { get; } |
| | 23 | | /// <summary> Location of the target Form Recognizer resource. A valid Azure region name supported by Cognitive |
| 28 | 24 | | internal string Region { get; } |
| | 25 | |
|
| 28 | 26 | | internal CopyAuthorization(string modelId, string accessToken, long expirationDateTimeTicks, string resourceId, |
| | 27 | | { |
| 28 | 28 | | ModelId = modelId; |
| 28 | 29 | | AccessToken = accessToken; |
| 28 | 30 | | ExpiresOn = DateTimeOffset.FromUnixTimeSeconds(expirationDateTimeTicks); |
| 28 | 31 | | ResourceId = resourceId; |
| 28 | 32 | | Region = region; |
| 28 | 33 | | } |
| | 34 | |
|
| | 35 | | internal CopyAuthorization(CopyAuthorizationResult copyAuth, string resourceId, string region) |
| 40 | 36 | | : this(copyAuth.ModelId, copyAuth.AccessToken, copyAuth.ExpirationDateTimeTicks, resourceId, region) { } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Deserializes an opaque string into a <see cref="CopyAuthorization"/> object. |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="copyAuthorization">Opaque string with the copy authorization information for a specific model.< |
| | 42 | | public static CopyAuthorization FromJson(string copyAuthorization) |
| | 43 | | { |
| 4 | 44 | | CopyAuthorizationParse parse = JsonSerializer.Deserialize<CopyAuthorizationParse>(copyAuthorization); |
| 4 | 45 | | return new CopyAuthorization( |
| 4 | 46 | | parse.modelId, |
| 4 | 47 | | parse.accessToken, |
| 4 | 48 | | parse.expirationDateTimeTicks, |
| 4 | 49 | | parse.resourceId, |
| 4 | 50 | | parse.resourceRegion); |
| | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Converts the CopyAuthorization object to its equivalent JSON representation. |
| | 55 | | /// </summary> |
| | 56 | | public string ToJson() |
| | 57 | | { |
| 4 | 58 | | var toParse = new CopyAuthorizationParse(this); |
| 4 | 59 | | return JsonSerializer.Serialize(toParse); |
| | 60 | | } |
| | 61 | |
|
| | 62 | | private class CopyAuthorizationParse |
| | 63 | | { |
| 16 | 64 | | public string modelId { get; set; } |
| 16 | 65 | | public string accessToken { get; set; } |
| 16 | 66 | | public long expirationDateTimeTicks { get; set; } |
| 16 | 67 | | public string resourceId { get; set; } |
| 16 | 68 | | public string resourceRegion { get; set; } |
| | 69 | |
|
| 8 | 70 | | public CopyAuthorizationParse() { } |
| | 71 | |
|
| 4 | 72 | | public CopyAuthorizationParse(CopyAuthorization target) |
| | 73 | | { |
| 4 | 74 | | modelId = target.ModelId; |
| 4 | 75 | | accessToken = target.AccessToken; |
| 4 | 76 | | expirationDateTimeTicks = target.ExpiresOn.ToUnixTimeSeconds(); |
| 4 | 77 | | resourceId = target.ResourceId; |
| 4 | 78 | | resourceRegion = target.Region; |
| 4 | 79 | | } |
| | 80 | | } |
| | 81 | | } |
| | 82 | | } |