From d198d30c697fea5f234c01e488338b2210820ebb Mon Sep 17 00:00:00 2001 From: pengweiqhca Date: Fri, 24 Mar 2023 10:04:04 +0800 Subject: [PATCH 1/2] Add global.json --- Apollo.sln | 1 + global.json | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 global.json diff --git a/Apollo.sln b/Apollo.sln index a64987f..5e8c0d3 100644 --- a/Apollo.sln +++ b/Apollo.sln @@ -20,6 +20,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Directory.Build.props = Directory.Build.props Directory.Build.targets = Directory.Build.targets README.md = README.md + global.json = global.json EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Apollo.ConfigurationManager.Demo", "demo\Apollo.ConfigurationManager.Demo\Apollo.ConfigurationManager.Demo.csproj", "{51A78BF1-4708-45EC-9F2E-59A24D64DB05}" diff --git a/global.json b/global.json new file mode 100644 index 0000000..3e5d3c5 --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "7.0.100", + "rollForward": "latestMajor", + "allowPrerelease": false + } +} From cefe736c6a4c2328db861a5fc94a257d9aee391a Mon Sep 17 00:00:00 2001 From: pengweiqhca Date: Fri, 24 Mar 2023 10:55:17 +0800 Subject: [PATCH 2/2] Add newest open api. --- .../Apollo.OpenApi.DependencyInjection.csproj | 2 +- src/Apollo.OpenApi/Apollo.OpenApi.csproj | 2 +- .../AppClusterClientExtensions.cs | 63 ++++++++++--------- src/Apollo.OpenApi/Model/PageModel.cs | 15 +++++ .../NamespaceClientExtensions.cs | 57 ++++++++++++----- test/Apollo.OpenApi.Tests/BaseTest.cs | 2 +- .../NamespaceClientTest.cs | 12 ++++ 7 files changed, 106 insertions(+), 47 deletions(-) create mode 100644 src/Apollo.OpenApi/Model/PageModel.cs diff --git a/src/Apollo.OpenApi.DependencyInjection/Apollo.OpenApi.DependencyInjection.csproj b/src/Apollo.OpenApi.DependencyInjection/Apollo.OpenApi.DependencyInjection.csproj index 6488732..375d312 100644 --- a/src/Apollo.OpenApi.DependencyInjection/Apollo.OpenApi.DependencyInjection.csproj +++ b/src/Apollo.OpenApi.DependencyInjection/Apollo.OpenApi.DependencyInjection.csproj @@ -4,7 +4,7 @@ Com.Ctrip.Framework.Apollo.OpenApi.DependencyInjection 携程Apollo的OpenApi客户端,入口接口IOpenApiFactory net461;netstandard2.0 - 2.1.1 + 2.2.0 Com.Ctrip.Framework.Apollo.OpenApi diff --git a/src/Apollo.OpenApi/Apollo.OpenApi.csproj b/src/Apollo.OpenApi/Apollo.OpenApi.csproj index 7d84a62..0d9808b 100644 --- a/src/Apollo.OpenApi/Apollo.OpenApi.csproj +++ b/src/Apollo.OpenApi/Apollo.OpenApi.csproj @@ -5,7 +5,7 @@ 携程Apollo的OpenApi客户端,入口接口IOpenApiFactory net40;net45;netstandard2.0 - 2.1.1 + 2.2.0 Com.Ctrip.Framework.Apollo.OpenApi diff --git a/src/Apollo.OpenApi/AppClusterClientExtensions.cs b/src/Apollo.OpenApi/AppClusterClientExtensions.cs index f52eedd..805448f 100644 --- a/src/Apollo.OpenApi/AppClusterClientExtensions.cs +++ b/src/Apollo.OpenApi/AppClusterClientExtensions.cs @@ -8,7 +8,8 @@ namespace Com.Ctrip.Framework.Apollo.OpenApi { public static class AppClusterClientExtensions { - /// 获取App的环境,集群信息 + /// 3.2.1 获取App的环境,集群信息 + /// #if NET40 public static Task?> GetEnvClusterInfo(this IAppClusterClient client, #else @@ -24,7 +25,8 @@ public static class AppClusterClientExtensions #endif } - /// 获取App信息 + /// 3.2.2 获取App信息 + /// public static async Task GetAppInfo(this IAppClusterClient client, CancellationToken cancellationToken = default) { if (client == null) throw new ArgumentNullException(nameof(client)); @@ -36,29 +38,8 @@ public static class AppClusterClientExtensions return list?.FirstOrDefault(); } - /// 获取集群 - public static Task GetCluster(this IAppClusterClient client, string env, - string clusterName = ConfigConsts.ClusterNameDefault, - CancellationToken cancellationToken = default) - { - if (client == null) throw new ArgumentNullException(nameof(client)); - if (env == null) throw new ArgumentNullException(nameof(env)); - - return client.Get($"envs/{env}/apps/{client.AppId}/clusters/{clusterName}", cancellationToken); - } - - /// 创建集群 - public static Task CreateCluster(this IAppClusterClient client, string env, - Cluster cluster, - CancellationToken cancellationToken = default) - { - if (client == null) throw new ArgumentNullException(nameof(client)); - if (env == null) throw new ArgumentNullException(nameof(env)); - - return client.Post($"envs/{env}/apps/{client.AppId}/clusters", cluster, cancellationToken); - } - - /// 获取App信息 + /// 3.2.2 获取App信息 + /// #if NET40 public static Task?> GetAppsInfo(this IAppClusterClient client, ICollection? appIds = null, CancellationToken cancellationToken = default) @@ -81,7 +62,32 @@ public static Task CreateCluster(this IAppClusterClient client, string env, #endif } - /// 获取集群下所有Namespace信息 + /// 3.2.3 获取集群接口 + /// + public static Task GetCluster(this IAppClusterClient client, string env, + string clusterName = ConfigConsts.ClusterNameDefault, + CancellationToken cancellationToken = default) + { + if (client == null) throw new ArgumentNullException(nameof(client)); + if (env == null) throw new ArgumentNullException(nameof(env)); + + return client.Get($"envs/{env}/apps/{client.AppId}/clusters/{clusterName}", cancellationToken); + } + + /// 3.2.4 创建集群接口 + /// + public static Task CreateCluster(this IAppClusterClient client, string env, + Cluster cluster, + CancellationToken cancellationToken = default) + { + if (client == null) throw new ArgumentNullException(nameof(client)); + if (env == null) throw new ArgumentNullException(nameof(env)); + + return client.Post($"envs/{env}/apps/{client.AppId}/clusters", cluster, cancellationToken); + } + + /// 3.2.5 获取集群下所有Namespace信息接口 + /// #if NET40 public static Task?> GetNamespaces(this IAppClusterClient client, string env, #else @@ -99,9 +105,10 @@ public static Task CreateCluster(this IAppClusterClient client, string env, #endif } - /// 创建Namespace,可以通过此接口创建Namespace,调用此接口需要授予第三方APP对目标APP的管理权限。 + /// 3.2.7 创建Namespace。可以通过此接口创建Namespace,调用此接口需要授予第三方APP对目标APP的管理权限。 + /// public static Task CreateAppNamespace(this IAppClusterClient client, - AppNamespace appNamespace, CancellationToken cancellationToken = default) + AppNamespace appNamespace, CancellationToken cancellationToken = default) { if (client == null) throw new ArgumentNullException(nameof(client)); if (appNamespace == null) throw new ArgumentNullException(nameof(appNamespace)); diff --git a/src/Apollo.OpenApi/Model/PageModel.cs b/src/Apollo.OpenApi/Model/PageModel.cs new file mode 100644 index 0000000..846f222 --- /dev/null +++ b/src/Apollo.OpenApi/Model/PageModel.cs @@ -0,0 +1,15 @@ +namespace Com.Ctrip.Framework.Apollo.OpenApi.Model; + +public class PageModel +{ +#if NET40 + public IList? Content { get; set; } +#else + public IReadOnlyList? Content { get; set; } +#endif + public int Page { get; set; } + + public int Size { get; set; } + + public int Total { get; set; } +} diff --git a/src/Apollo.OpenApi/NamespaceClientExtensions.cs b/src/Apollo.OpenApi/NamespaceClientExtensions.cs index 03f95fd..8555037 100644 --- a/src/Apollo.OpenApi/NamespaceClientExtensions.cs +++ b/src/Apollo.OpenApi/NamespaceClientExtensions.cs @@ -7,7 +7,8 @@ namespace Com.Ctrip.Framework.Apollo.OpenApi { public static class NamespaceClientExtensions { - /// 获取信息 + /// 3.2.6 获取某个Namespace信息接口 + /// public static Task GetNamespaceInfo(this INamespaceClient client, CancellationToken cancellationToken = default) { @@ -16,7 +17,8 @@ public static class NamespaceClientExtensions return client.Get($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}", cancellationToken); } - /// 获取当前编辑人 + /// 3.2.8 获取某个Namespace当前编辑人接口 + /// public static Task GetNamespaceLock(this INamespaceClient client, CancellationToken cancellationToken = default) { @@ -25,9 +27,10 @@ public static class NamespaceClientExtensions return client.Get($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/lock", cancellationToken); } - /// 获取配置 + /// 3.2.9 读取配置接口 + /// public static Task GetItem(this INamespaceClient client, - string key, CancellationToken cancellationToken = default) + string key, CancellationToken cancellationToken = default) { if (client == null) throw new ArgumentNullException(nameof(client)); if (key == null) throw new ArgumentNullException(nameof(key)); @@ -35,9 +38,10 @@ public static class NamespaceClientExtensions return client.Get($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/items/{key}", cancellationToken); } - /// 新增配置 + /// 3.2.10 新增配置接口 + /// public static Task CreateItem(this INamespaceClient client, - Item item, CancellationToken cancellationToken = default) + Item item, CancellationToken cancellationToken = default) { if (client == null) throw new ArgumentNullException(nameof(client)); if (item == null) throw new ArgumentNullException(nameof(item)); @@ -47,9 +51,10 @@ public static Task CreateItem(this INamespaceClient client, return client.Post($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/items", item, cancellationToken); } - /// 修改配置 + /// 3.2.11 修改配置接口 + /// public static Task UpdateItem(this INamespaceClient client, - Item item, CancellationToken cancellationToken = default) + Item item, CancellationToken cancellationToken = default) { if (client == null) throw new ArgumentNullException(nameof(client)); if (item == null) throw new ArgumentNullException(nameof(item)); @@ -59,9 +64,10 @@ public static Task UpdateItem(this INamespaceClient client, return client.Put($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/items/{item.Key}", item, cancellationToken); } - /// 创建或修改配置 + /// 3.2.11 修改配置接口 + /// public static Task CreateOrUpdateItem(this INamespaceClient client, - Item item, CancellationToken cancellationToken = default) + Item item, CancellationToken cancellationToken = default) { if (client == null) throw new ArgumentNullException(nameof(client)); if (item == null) throw new ArgumentNullException(nameof(item)); @@ -74,10 +80,11 @@ public static Task CreateOrUpdateItem(this INamespaceClient client, return client.Put($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/items/{item.Key}?createIfNotExists=true", item, cancellationToken); } - /// 删除配置 + /// 3.2.12 删除配置接口 + /// /// 存在时删除后返回true,或者返回false public static Task RemoveItem(this INamespaceClient client, string key, - string @operator, CancellationToken cancellationToken = default) + string @operator, CancellationToken cancellationToken = default) { if (client == null) throw new ArgumentNullException(nameof(client)); if (key == null) throw new ArgumentNullException(nameof(key)); @@ -86,9 +93,10 @@ public static Task RemoveItem(this INamespaceClient client, string key, return client.Delete($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/items/{key}?operator={WebUtility.UrlEncode(@operator)}", cancellationToken); } - /// 发布配置 + /// 3.2.13 发布配置接口 + /// public static Task Publish(this INamespaceClient client, - NamespaceRelease release, CancellationToken cancellationToken = default) + NamespaceRelease release, CancellationToken cancellationToken = default) { if (client == null) throw new ArgumentNullException(nameof(client)); if (release == null) throw new ArgumentNullException(nameof(release)); @@ -98,7 +106,8 @@ public static Task Publish(this INamespaceClient client, return client.Post($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/releases", release, cancellationToken); } - /// 获取当前生效的已发布配置接口 + /// 3.2.14 获取某个Namespace当前生效的已发布配置接口 + /// public static Task GetLatestActiveRelease(this INamespaceClient client, CancellationToken cancellationToken = default) { @@ -107,7 +116,8 @@ public static Task Publish(this INamespaceClient client, return client.Get($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/releases/latest", cancellationToken); } - /// 回滚已发布配置接口 + /// 3.2.15 回滚已发布配置接口 + /// public static Task Rollback(this INamespaceClient client, string @operator, int releaseId, CancellationToken cancellationToken = default) { @@ -115,5 +125,20 @@ public static Task Rollback(this INamespaceClient client, string @operator return client.Put($"envs/{client.Env}/releases/{releaseId}/rollback?operator={WebUtility.UrlEncode(@operator)}", cancellationToken); } + + /// 3.2.16 分页获取配置项接口 + /// + /// 从 0 开始 + /// + /// + /// + public static async Task> GetItems(this INamespaceClient client, int page = 0, int size = 50, CancellationToken cancellationToken = default) + { + if (client == null) throw new ArgumentNullException(nameof(client)); + + var result = await client.Get>($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/items?page={page}&size={size}", cancellationToken); + + return result ?? new PageModel(); + } } } diff --git a/test/Apollo.OpenApi.Tests/BaseTest.cs b/test/Apollo.OpenApi.Tests/BaseTest.cs index d246bf4..9c51f1f 100644 --- a/test/Apollo.OpenApi.Tests/BaseTest.cs +++ b/test/Apollo.OpenApi.Tests/BaseTest.cs @@ -11,7 +11,7 @@ public abstract class BaseTest public static IOpenApiFactory Factory { get; } = new OpenApiFactory(new() { PortalUrl = new("http://81.68.181.139:8070"), - Token = "a65ba5e0f90be078888a070672a243e9b6ec5d65" + Token = "c8924e97b3dabe487e7472c71d91f34dcdfaeada85a3e4b18b68efb5e480074d" }); public static IReadOnlyList AppIds = new List { "apollo.net", "SampleApp" }; diff --git a/test/Apollo.OpenApi.Tests/NamespaceClientTest.cs b/test/Apollo.OpenApi.Tests/NamespaceClientTest.cs index 16f02ef..2f9fe07 100644 --- a/test/Apollo.OpenApi.Tests/NamespaceClientTest.cs +++ b/test/Apollo.OpenApi.Tests/NamespaceClientTest.cs @@ -124,4 +124,16 @@ public async Task ReleaseTest() await client.Rollback("apollo", latestActiveRelease.Id).ConfigureAwait(false); } + + [Fact] + public async Task ItemsTest() + { + var client = CreateNamespaceClient(); + + var result = await client.GetItems().ConfigureAwait(false); + + Assert.True(result.Total > 0); + + Assert.NotNull(result.Content); + } }