< Summary

Class:Azure.Data.Tables.TableServiceClient
Assembly:Azure.Data.Tables
File(s):C:\Git\azure-sdk-for-net\sdk\tables\Azure.Data.Tables\src\TableServiceClient.cs
Covered lines:145
Uncovered lines:31
Coverable lines:176
Total lines:442
Line coverage:82.3% (145 of 176)
Covered branches:9
Total branches:10
Branch coverage:90% (9 of 10)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-96.15%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
.ctor(...)-0%100%
.ctor()-100%100%
GetSasBuilder(...)-100%100%
GetSasBuilder(...)-100%100%
GetTableClient(...)-100%100%
GetTablesAsync(...)-86.96%100%
GetTables(...)-83.78%100%
CreateTable(...)-100%100%
CreateTableAsync()-100%100%
DeleteTable(...)-100%100%
DeleteTableAsync()-100%100%
SetProperties(...)-57.14%100%
SetPropertiesAsync()-57.14%100%
GetProperties(...)-62.5%100%
GetPropertiesAsync()-62.5%100%
GetStatisticsAsync()-62.5%100%
GetStatistics(...)-62.5%100%
IsPremiumEndpoint(...)-100%83.33%

File(s)

C:\Git\azure-sdk-for-net\sdk\tables\Azure.Data.Tables\src\TableServiceClient.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Threading;
 7using System.Threading.Tasks;
 8using Azure.Core;
 9using Azure.Core.Pipeline;
 10using Azure.Data.Tables.Models;
 11using Azure.Data.Tables.Sas;
 12
 13namespace Azure.Data.Tables
 14{
 15    public class TableServiceClient
 16    {
 17        private readonly ClientDiagnostics _diagnostics;
 18        private readonly TableRestClient _tableOperations;
 19        private readonly ServiceRestClient _serviceOperations;
 20        private readonly ServiceRestClient _secondaryServiceOperations;
 021        private readonly OdataMetadataFormat _format = OdataMetadataFormat.ApplicationJsonOdataFullmetadata;
 22        private readonly string _version;
 23        internal readonly bool _isPremiumEndpoint;
 24
 25        /// <summary>
 26        /// Initializes a new instance of the <see cref="TableServiceClient"/>.
 27        /// </summary>
 28        /// <param name="endpoint">
 29        /// A <see cref="Uri"/> referencing the table service account.
 30        /// This is likely to be similar to "https://{account_name}.table.core.windows.net/" or "https://{account_name}.
 31        /// </param>
 32        public TableServiceClient(Uri endpoint)
 4433                : this(endpoint, options: null)
 4034        { }
 35
 36        /// <summary>
 37        /// Initializes a new instance of the <see cref="TableServiceClient"/>.
 38        /// </summary>
 39        /// <param name="endpoint">
 40        /// A <see cref="Uri"/> referencing the table service account.
 41        /// This is likely to be similar to "https://{account_name}.table.core.windows.net/" or "https://{account_name}.
 42        /// </param>
 43        /// <param name="options">
 44        /// Optional client options that define the transport pipeline policies for authentication, retries, etc., that 
 45        /// </param>
 46        public TableServiceClient(Uri endpoint, TableClientOptions options = null)
 8447            : this(endpoint, default(TableSharedKeyPipelinePolicy), options)
 48        {
 8449            if (endpoint.Scheme != "https")
 50            {
 451                throw new ArgumentException("Cannot use TokenCredential without HTTPS.", nameof(endpoint));
 52            }
 8053        }
 54
 55        /// <summary>
 56        /// Initializes a new instance of the <see cref="TableServiceClient"/>.
 57        /// </summary>
 58        /// <param name="endpoint">
 59        /// A <see cref="Uri"/> referencing the table service account.
 60        /// This is likely to be similar to "https://{account_name}.table.core.windows.net/" or "https://{account_name}.
 61        /// </param>
 62        /// <param name="credential">The shared key credential used to sign requests.</param>
 63        public TableServiceClient(Uri endpoint, TableSharedKeyCredential credential)
 1664            : this(endpoint, new TableSharedKeyPipelinePolicy(credential), null)
 65        {
 1266            Argument.AssertNotNull(credential, nameof(credential));
 867        }
 68
 69        /// <summary>
 70        /// Initializes a new instance of the <see cref="TableServiceClient"/>.
 71        /// </summary>
 72        /// <param name="endpoint">
 73        /// A <see cref="Uri"/> referencing the table service account.
 74        /// This is likely to be similar to "https://{account_name}.table.core.windows.net/" or "https://{account_name}.
 75        /// </param>
 76        /// <param name="credential">The shared key credential used to sign requests.</param>
 77        /// <param name="options">
 78        /// Optional client options that define the transport pipeline policies for authentication, retries, etc., that 
 79        /// </param>
 80        public TableServiceClient(Uri endpoint, TableSharedKeyCredential credential, TableClientOptions options = null)
 37681            : this(endpoint, new TableSharedKeyPipelinePolicy(credential), options)
 82        {
 37683            Argument.AssertNotNull(credential, nameof(credential));
 37684        }
 85
 47686        internal TableServiceClient(Uri endpoint, TableSharedKeyPipelinePolicy policy, TableClientOptions options)
 87        {
 47688            Argument.AssertNotNull(endpoint, nameof(endpoint));
 89
 47290            options ??= new TableClientOptions();
 47291            var endpointString = endpoint.ToString();
 47292            var secondaryEndpoint = endpointString.Insert(endpointString.IndexOf('.'), "-secondary");
 47293            HttpPipeline pipeline = HttpPipelineBuilder.Build(options, policy);
 94
 47295            _diagnostics = new ClientDiagnostics(options);
 47296            _tableOperations = new TableRestClient(_diagnostics, pipeline, endpointString);
 47297            _serviceOperations = new ServiceRestClient(_diagnostics, pipeline, endpointString);
 47298            _secondaryServiceOperations = new ServiceRestClient(_diagnostics, pipeline, secondaryEndpoint);
 47299            _version = options.VersionString;
 472100            _isPremiumEndpoint = IsPremiumEndpoint(endpoint);
 472101        }
 102
 103        /// <summary>
 104        /// Initializes a new instance of the <see cref="TableServiceClient"/>
 105        /// class for mocking.
 106        /// </summary>
 0107        internal TableServiceClient(TableRestClient internalClient)
 108        {
 0109            _tableOperations = internalClient;
 0110        }
 111
 112        /// <summary>
 113        /// Initializes a new instance of the <see cref="TableServiceClient"/>
 114        /// class for mocking.
 115        /// </summary>
 452116        protected TableServiceClient()
 452117        { }
 118
 119        /// <summary>
 120        /// Gets a <see cref="TableSasBuilder"/> instance scoped to the current account.
 121        /// </summary>
 122        /// <param name="permissions"><see cref="TableAccountSasPermissions"/> containing the allowed permissions.</para
 123        /// <param name="resourceTypes"><see cref="TableAccountSasResourceTypes"/> containing the accessible resource ty
 124        /// <param name="expiresOn">The time at which the shared access signature becomes invalid.</param>
 125        /// <returns>An instance of <see cref="TableAccountSasBuilder"/>.</returns>
 126        public virtual TableAccountSasBuilder GetSasBuilder(TableAccountSasPermissions permissions, TableAccountSasResou
 127        {
 36128            return new TableAccountSasBuilder(permissions, resourceTypes, expiresOn) { Version = _version };
 129        }
 130
 131        /// <summary>
 132        /// Gets a <see cref="TableAccountSasBuilder"/> instance scoped to the current table.
 133        /// </summary>
 134        /// <param name="rawPermissions">The permissions associated with the shared access signature. This string should
 135        /// <param name="resourceTypes"><see cref="TableAccountSasResourceTypes"/> containing the accessible resource ty
 136        /// <param name="expiresOn">The time at which the shared access signature becomes invalid.</param>
 137        /// <returns>An instance of <see cref="TableAccountSasBuilder"/>.</returns>
 138        public virtual TableAccountSasBuilder GetSasBuilder(string rawPermissions, TableAccountSasResourceTypes resource
 139        {
 4140            return new TableAccountSasBuilder(rawPermissions, resourceTypes, expiresOn) { Version = _version };
 141        }
 142
 143        public virtual TableClient GetTableClient(string tableName)
 144        {
 412145            Argument.AssertNotNull(tableName, nameof(tableName));
 146
 408147            return new TableClient(tableName, _tableOperations, _version, _diagnostics, _isPremiumEndpoint);
 148        }
 149
 150        /// <summary>
 151        /// Gets a list of tables from the storage account.
 152        /// </summary>
 153        /// <param name="filter">Returns only tables that satisfy the specified filter.</param>
 154        /// <param name="maxPerPage">The maximum number of tables that will be returned per page.</param>
 155        /// <param name="select">Returns the desired properties of an entity from the set. </param>
 156        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 157        /// <returns></returns>
 158        public virtual AsyncPageable<TableItem> GetTablesAsync(string filter = null, int? maxPerPage = null, string sele
 159        {
 20160            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(GetTables)}");
 20161            scope.Start();
 162            try
 163            {
 20164                return PageableHelpers.CreateAsyncEnumerable(async _ =>
 20165            {
 40166                var response = await _tableOperations.QueryAsync(
 40167                    null,
 40168                    null,
 40169                    new QueryOptions() { Filter = filter, Select = select, Top = maxPerPage, Format = _format },
 40170                    cancellationToken).ConfigureAwait(false);
 40171                return Page.FromValues(response.Value.Value, response.Headers.XMsContinuationNextTableName, response.Get
 40172            }, async (nextLink, _) =>
 20173            {
 28174                var response = await _tableOperations.QueryAsync(
 28175                       null,
 28176                       nextTableName: nextLink,
 28177                       new QueryOptions() { Filter = filter, Select = select, Top = maxPerPage, Format = _format },
 28178                       cancellationToken).ConfigureAwait(false);
 28179                return Page.FromValues(response.Value.Value, response.Headers.XMsContinuationNextTableName, response.Get
 28180            });
 181            }
 0182            catch (Exception ex)
 183            {
 0184                scope.Failed(ex);
 0185                throw;
 186            }
 20187        }
 188
 189        /// <summary>
 190        /// Gets a list of tables from the storage account.
 191        /// </summary>
 192        /// <param name="filter">Returns only tables or entities that satisfy the specified filter.</param>
 193        /// <param name="maxPerPage">The maximum number of tables that will be returned per page.</param>
 194        /// <param name="select">Returns the desired properties of an entity from the set. </param>
 195        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 196        /// <returns></returns>
 197        public virtual Pageable<TableItem> GetTables(string filter = null, int? maxPerPage = null, string select = null,
 198        {
 199
 20200            return PageableHelpers.CreateEnumerable(_ =>
 20201            {
 40202                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(GetTables)
 40203                scope.Start();
 20204                try
 20205                {
 40206                    var response = _tableOperations.Query(
 40207                            null,
 40208                            null,
 40209                            new QueryOptions() { Filter = filter, Select = select, Top = maxPerPage, Format = _format },
 40210                            cancellationToken);
 40211                    return Page.FromValues(response.Value.Value, response.Headers.XMsContinuationNextTableName, response
 20212                }
 0213                catch (Exception ex)
 20214                {
 0215                    scope.Failed(ex);
 0216                    throw;
 20217                }
 40218            }, (nextLink, _) =>
 20219            {
 28220                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(GetTables)
 28221                scope.Start();
 20222                try
 20223                {
 28224                    var response = _tableOperations.Query(
 28225                        null,
 28226                        nextTableName: nextLink,
 28227                        new QueryOptions() { Filter = filter, Select = select, Top = maxPerPage, Format = _format },
 28228                        cancellationToken);
 28229                    return Page.FromValues(response.Value.Value, response.Headers.XMsContinuationNextTableName, response
 20230                }
 0231                catch (Exception ex)
 20232                {
 0233                    scope.Failed(ex);
 0234                    throw;
 20235                }
 28236            });
 237        }
 238
 239        /// <summary>
 240        /// Creates a table in the storage account.
 241        /// </summary>
 242        /// <param name="tableName">The table name to create.</param>
 243        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 244        /// <returns></returns>
 245        public virtual Response<TableItem> CreateTable(string tableName, CancellationToken cancellationToken = default)
 246        {
 326247            Argument.AssertNotNull(tableName, nameof(tableName));
 324248            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(CreateTable)}"
 324249            scope.Start();
 250            try
 251            {
 324252                var response = _tableOperations.Create(new TableProperties() { TableName = tableName }, null, queryOptio
 316253                return Response.FromValue(response.Value as TableItem, response.GetRawResponse());
 254            }
 8255            catch (Exception ex)
 256            {
 8257                scope.Failed(ex);
 8258                throw;
 259            }
 316260        }
 261
 262        /// <summary>
 263        /// Creates a table in the storage account.
 264        /// </summary>
 265        /// <param name="tableName">The table name to create.</param>
 266        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 267        /// <returns></returns>
 268        public virtual async Task<Response<TableItem>> CreateTableAsync(string tableName, CancellationToken cancellation
 269        {
 326270            Argument.AssertNotNull(tableName, nameof(tableName));
 324271            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(CreateTable)}"
 324272            scope.Start();
 273            try
 274            {
 324275                var response = await _tableOperations.CreateAsync(new TableProperties() { TableName = tableName }, null,
 316276                return Response.FromValue(response.Value as TableItem, response.GetRawResponse());
 277            }
 8278            catch (Exception ex)
 279            {
 8280                scope.Failed(ex);
 8281                throw;
 282            }
 316283        }
 284
 285        /// <summary>
 286        /// Deletes a table in the storage account.
 287        /// </summary>
 288        /// <param name="tableName">The table name to create.</param>
 289        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 290        /// <returns></returns>
 291        public virtual Response DeleteTable(string tableName, CancellationToken cancellationToken = default)
 292        {
 318293            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(DeleteTable)}"
 318294            scope.Start();
 295            try
 296            {
 318297                return _tableOperations.Delete(tableName, null, cancellationToken: cancellationToken);
 298            }
 2299            catch (Exception ex)
 300            {
 2301                scope.Failed(ex);
 2302                throw;
 303            }
 316304        }
 305
 306        /// <summary>
 307        /// Deletes a table in the storage account.
 308        /// </summary>
 309        /// <param name="tableName">The table name to create.</param>
 310        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
 311        /// <returns></returns>
 312        public virtual async Task<Response> DeleteTableAsync(string tableName, CancellationToken cancellationToken = def
 313        {
 318314            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(DeleteTable)}"
 318315            scope.Start();
 316            try
 317            {
 318318                return await _tableOperations.DeleteAsync(tableName, null, cancellationToken: cancellationToken).Configu
 319            }
 2320            catch (Exception ex)
 321            {
 2322                scope.Failed(ex);
 2323                throw;
 324            }
 316325        }
 326
 327        /// <summary> Sets properties for an account&apos;s Table service endpoint, including properties for Analytics a
 328        /// <param name="tableServiceProperties"> The Table Service properties. </param>
 329        /// <param name="cancellationToken"> The cancellation token to use. </param>
 330        public virtual Response SetProperties(TableServiceProperties tableServiceProperties, CancellationToken cancellat
 331        {
 2332            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(SetProperties)
 2333            scope.Start();
 334            try
 335            {
 2336                return _serviceOperations.SetProperties(tableServiceProperties, cancellationToken: cancellationToken);
 337            }
 0338            catch (Exception ex)
 339            {
 0340                scope.Failed(ex);
 0341                throw;
 342            }
 2343        }
 344
 345        /// <summary> Sets properties for an account&apos;s Table service endpoint, including properties for Analytics a
 346        /// <param name="tableServiceProperties"> The Table Service properties. </param>
 347        /// <param name="cancellationToken"> The cancellation token to use. </param>
 348        public virtual async Task<Response> SetPropertiesAsync(TableServiceProperties tableServiceProperties, Cancellati
 349        {
 2350            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(SetProperties)
 2351            scope.Start();
 352            try
 353            {
 2354                return await _serviceOperations.SetPropertiesAsync(tableServiceProperties, cancellationToken: cancellati
 355            }
 0356            catch (Exception ex)
 357            {
 0358                scope.Failed(ex);
 0359                throw;
 360            }
 2361        }
 362
 363        /// <summary> Gets the properties of an account&apos;s Table service, including properties for Analytics and COR
 364        /// <param name="cancellationToken"> The cancellation token to use. </param>
 365        public virtual Response<TableServiceProperties> GetProperties(CancellationToken cancellationToken = default)
 366        {
 12367            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(GetProperties)
 12368            scope.Start();
 369            try
 370            {
 12371                var response = _serviceOperations.GetProperties(cancellationToken: cancellationToken);
 12372                return Response.FromValue(response.Value, response.GetRawResponse());
 373            }
 0374            catch (Exception ex)
 375            {
 0376                scope.Failed(ex);
 0377                throw;
 378            }
 12379        }
 380
 381        /// <summary> Gets the properties of an account&apos;s Table service, including properties for Analytics and COR
 382        /// <param name="cancellationToken"> The cancellation token to use. </param>
 383        public virtual async Task<Response<TableServiceProperties>> GetPropertiesAsync(CancellationToken cancellationTok
 384        {
 12385            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(GetProperties)
 12386            scope.Start();
 387            try
 388            {
 12389                var response = await _serviceOperations.GetPropertiesAsync(cancellationToken: cancellationToken).Configu
 12390                return Response.FromValue(response.Value, response.GetRawResponse());
 391            }
 0392            catch (Exception ex)
 393            {
 0394                scope.Failed(ex);
 0395                throw;
 396            }
 12397        }
 398
 399        /// <summary> Retrieves statistics related to replication for the Table service. It is only available on the sec
 400        /// <param name="cancellationToken"> The cancellation token to use. </param>
 401        public virtual async Task<Response<TableServiceStatistics>> GetStatisticsAsync(CancellationToken cancellationTok
 402        {
 2403            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(GetStatistics)
 2404            scope.Start();
 405            try
 406            {
 2407                var response = await _secondaryServiceOperations.GetStatisticsAsync(cancellationToken: cancellationToken
 2408                return Response.FromValue(response.Value, response.GetRawResponse());
 409            }
 0410            catch (Exception ex)
 411            {
 0412                scope.Failed(ex);
 0413                throw;
 414            }
 2415        }
 416
 417        /// <summary> Retrieves statistics related to replication for the Table service. It is only available on the sec
 418        /// <param name="cancellationToken"> The cancellation token to use. </param>
 419        public virtual Response<TableServiceStatistics> GetStatistics(CancellationToken cancellationToken = default)
 420        {
 2421            using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(TableServiceClient)}.{nameof(GetStatistics)
 2422            scope.Start();
 423            try
 424            {
 2425                var response = _secondaryServiceOperations.GetStatistics(cancellationToken: cancellationToken);
 2426                return Response.FromValue(response.Value, response.GetRawResponse());
 427            }
 0428            catch (Exception ex)
 429            {
 0430                scope.Failed(ex);
 0431                throw;
 432            }
 2433        }
 434
 435        internal static bool IsPremiumEndpoint(Uri endpoint)
 436        {
 496437            string absoluteUri = endpoint.OriginalString.ToLowerInvariant();
 496438            return (endpoint.Host.Equals("localhost", StringComparison.OrdinalIgnoreCase) && endpoint.Port != 10002) ||
 496439                absoluteUri.Contains(TableConstants.CosmosTableDomain) || absoluteUri.Contains(TableConstants.LegacyCosm
 440        }
 441    }
 442}