BasicARMWebAPI.Utils.ResourceUtilities.GetLatestAPIVersion C# (CSharp) Method

GetLatestAPIVersion() public static method

public static GetLatestAPIVersion ( Microsoft.Azure.Management.Resources.ResourceManagementClient client, string resourceId, string apiVersion = null ) : Task
client Microsoft.Azure.Management.Resources.ResourceManagementClient
resourceId string
apiVersion string
return Task
        public async static Task<string> GetLatestAPIVersion(ResourceManagementClient client, string resourceId, string apiVersion = null)
        {
            if (apiVersion == null)
            {
                var parameters = new Microsoft.Azure.Management.Resources.Models.ProviderListParameters();
                var listProvidersResponse = await client.Providers.ListAsync(parameters).ConfigureAwait(continueOnCapturedContext: false);

                var identity = GetIdentityFromId(resourceId, apiVersion);

                var provider = listProvidersResponse.Providers.Where(x => x.Namespace.Equals(identity.ResourceProviderNamespace)).FirstOrDefault();
                if (provider == null)
                {
                    throw new InvalidOperationException("This is not a valid resource provider namespace");
                }

                var type = provider.ResourceTypes.Where(x => x.Name.Equals(identity.ResourceType)).FirstOrDefault();
                if (type == null)
                {
                    throw new InvalidOperationException("This is not a valid resource type");
                }

                apiVersion = type.ApiVersions.OrderByDescending(x => x).FirstOrDefault();

            }
            return apiVersion;
        }