|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using CommunityToolkit.Datasync.Client.Http; |
| 6 | + |
| 7 | +namespace CommunityToolkit.Datasync.Client.Offline; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// The options builder for the offline operations. |
| 11 | +/// </summary> |
| 12 | +public class DatasyncOfflineOptionsBuilder |
| 13 | +{ |
| 14 | + internal IHttpClientFactory? _httpClientFactory; |
| 15 | + internal readonly Dictionary<string, EntityOfflineOptions> _entities; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Creates the builder based on the required entity types. |
| 19 | + /// </summary> |
| 20 | + /// <param name="entityTypes">The entity type list.</param> |
| 21 | + internal DatasyncOfflineOptionsBuilder(IEnumerable<Type> entityTypes) |
| 22 | + { |
| 23 | + this._entities = entityTypes.ToDictionary(x => x.FullName!, x => new EntityOfflineOptions(x)); |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Sets the default mechanism for getting a <see cref="HttpClient"/> to be |
| 28 | + /// the specified <see cref="IHttpClientFactory"/>. |
| 29 | + /// </summary> |
| 30 | + /// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/> to use.</param> |
| 31 | + /// <returns>The current builder for chaining.</returns> |
| 32 | + public DatasyncOfflineOptionsBuilder UseHttpClientFactory(IHttpClientFactory httpClientFactory) |
| 33 | + { |
| 34 | + ArgumentNullException.ThrowIfNull(httpClientFactory, nameof(httpClientFactory)); |
| 35 | + this._httpClientFactory = httpClientFactory; |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Sets the default mechanism for getting a <see cref="HttpClient"/> to be |
| 41 | + /// a constant <see cref="HttpClient"/> |
| 42 | + /// </summary> |
| 43 | + /// <param name="httpClient">The <see cref="HttpClient"/> to use.</param> |
| 44 | + /// <returns>The current builder for chaining.</returns> |
| 45 | + public DatasyncOfflineOptionsBuilder UseHttpClient(HttpClient httpClient) |
| 46 | + { |
| 47 | + ArgumentNullException.ThrowIfNull(httpClient, nameof(httpClient)); |
| 48 | + this._httpClientFactory = new BasicHttpClientFactory(httpClient); |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Sets the default mechanism for getting a <see cref="HttpClient"/> to be a |
| 54 | + /// standard <see cref="IHttpClientFactory"/> based on the provided endpoint. |
| 55 | + /// </summary> |
| 56 | + /// <param name="endpoint">The <see cref="Uri"/> pointing to the datasync endpoint.</param> |
| 57 | + /// <returns>The current builder for chaining.</returns> |
| 58 | + public DatasyncOfflineOptionsBuilder UseEndpoint(Uri endpoint) |
| 59 | + { |
| 60 | + ArgumentNullException.ThrowIfNull(endpoint, nameof(endpoint)); |
| 61 | + ThrowIf.IsNotValidEndpoint(endpoint, nameof(endpoint)); |
| 62 | + this._httpClientFactory = new HttpClientFactory(new HttpClientOptions { Endpoint = endpoint }); |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Sets the default mechanism for getting a <see cref="HttpClient"/> to be a |
| 68 | + /// standard <see cref="IHttpClientFactory"/> based on the provided client options |
| 69 | + /// </summary> |
| 70 | + /// <param name="clientOptions">The <see cref="HttpClientOptions"/> pointing to the datasync endpoint.</param> |
| 71 | + /// <returns>The current builder for chaining.</returns> |
| 72 | + public DatasyncOfflineOptionsBuilder UseHttpClientOptions(HttpClientOptions clientOptions) |
| 73 | + { |
| 74 | + ArgumentNullException.ThrowIfNull(clientOptions, nameof(clientOptions)); |
| 75 | + this._httpClientFactory = new HttpClientFactory(clientOptions); |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Configures the specified entity type for offline operations. |
| 81 | + /// </summary> |
| 82 | + /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| 83 | + /// <param name="configure">A configuration function for the entity.</param> |
| 84 | + /// <returns>The current builder for chaining.</returns> |
| 85 | + public DatasyncOfflineOptionsBuilder Entity<TEntity>(Action<EntityOfflineOptions> configure) |
| 86 | + => Entity(typeof(TEntity), configure); |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Configures the specified entity type for offline operations. |
| 90 | + /// </summary> |
| 91 | + /// <param name="entityType">The type of the entity.</param> |
| 92 | + /// <param name="configure">A configuration function for the entity.</param> |
| 93 | + /// <returns>The current builder for chaining.</returns> |
| 94 | + public DatasyncOfflineOptionsBuilder Entity(Type entityType, Action<EntityOfflineOptions> configure) |
| 95 | + { |
| 96 | + ArgumentNullException.ThrowIfNull(entityType, nameof(entityType)); |
| 97 | + ArgumentNullException.ThrowIfNull(configure, nameof(configure)); |
| 98 | + if (!this._entities.TryGetValue(entityType.FullName!, out EntityOfflineOptions? options)) |
| 99 | + { |
| 100 | + throw new DatasyncException($"Entity is not synchronizable."); |
| 101 | + } |
| 102 | + |
| 103 | + configure(options); |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Retrieves the offline options for the entity type. |
| 109 | + /// </summary> |
| 110 | + /// <param name="entityType">The entity type.</param> |
| 111 | + /// <returns>The offline options for the entity type.</returns> |
| 112 | + internal DatasyncOfflineOptions GetOfflineOptions(Type entityType) |
| 113 | + { |
| 114 | + ArgumentNullException.ThrowIfNull(entityType, nameof(entityType)); |
| 115 | + if (this._httpClientFactory == null) |
| 116 | + { |
| 117 | + throw new DatasyncException($"Datasync service connection is not set."); |
| 118 | + } |
| 119 | + |
| 120 | + if (!this._entities.TryGetValue(entityType.FullName!, out EntityOfflineOptions? options)) |
| 121 | + { |
| 122 | + throw new DatasyncException($"Entity is not synchronizable."); |
| 123 | + } |
| 124 | + |
| 125 | + HttpClient client = this._httpClientFactory.CreateClient(options.ClientName); |
| 126 | + return new DatasyncOfflineOptions(client, options.Endpoint); |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// The entity offline options that are used for configuration. |
| 131 | + /// </summary> |
| 132 | + /// <param name="entityType">The entity type for this entity offline options.</param> |
| 133 | + public class EntityOfflineOptions(Type entityType) |
| 134 | + { |
| 135 | + /// <summary> |
| 136 | + /// The entity type being configured. |
| 137 | + /// </summary> |
| 138 | + public Type EntityType { get => entityType; } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// The endpoint for the entity type. |
| 142 | + /// </summary> |
| 143 | + public Uri Endpoint { get; set; } = new Uri($"/tables/{entityType.Name.ToLowerInvariant()}", UriKind.Relative); |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// The name of the client to use when requesting a <see cref="HttpClient"/>. |
| 147 | + /// </summary> |
| 148 | + public string ClientName { get; set; } = string.Empty; |
| 149 | + } |
| 150 | +} |
0 commit comments