Skip to content

Commit

Permalink
Merge pull request #231 from pengweiqhca/main
Browse files Browse the repository at this point in the history
Add newest open api.
  • Loading branch information
pengweiqhca authored Mar 24, 2023
2 parents e63a5cb + cefe736 commit 039ddd0
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 47 deletions.
1 change: 1 addition & 0 deletions Apollo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
7 changes: 7 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "7.0.100",
"rollForward": "latestMajor",
"allowPrerelease": false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AssemblyName>Com.Ctrip.Framework.Apollo.OpenApi.DependencyInjection</AssemblyName>
<Description>携程Apollo的OpenApi客户端,入口接口IOpenApiFactory</Description>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
<Version>2.1.1</Version>
<Version>2.2.0</Version>
<RootNamespace>Com.Ctrip.Framework.Apollo.OpenApi</RootNamespace>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Apollo.OpenApi/Apollo.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PackageReleaseNotes></PackageReleaseNotes>
<Description>携程Apollo的OpenApi客户端,入口接口IOpenApiFactory</Description>
<TargetFrameworks>net40;net45;netstandard2.0</TargetFrameworks>
<Version>2.1.1</Version>
<Version>2.2.0</Version>
<RootNamespace>Com.Ctrip.Framework.Apollo.OpenApi</RootNamespace>
</PropertyGroup>

Expand Down
63 changes: 35 additions & 28 deletions src/Apollo.OpenApi/AppClusterClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Com.Ctrip.Framework.Apollo.OpenApi
{
public static class AppClusterClientExtensions
{
/// <summary>获取App的环境,集群信息</summary>
/// <summary>3.2.1 获取App的环境,集群信息</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_321-%e8%8e%b7%e5%8f%96app%e7%9a%84%e7%8e%af%e5%a2%83%ef%bc%8c%e9%9b%86%e7%be%a4%e4%bf%a1%e6%81%af" />
#if NET40
public static Task<IList<EnvCluster>?> GetEnvClusterInfo(this IAppClusterClient client,
#else
Expand All @@ -24,7 +25,8 @@ public static class AppClusterClientExtensions
#endif
}

/// <summary>获取App信息</summary>
/// <summary>3.2.2 获取App信息</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_322-%e8%8e%b7%e5%8f%96app%e4%bf%a1%e6%81%af" />
public static async Task<AppInfo?> GetAppInfo(this IAppClusterClient client, CancellationToken cancellationToken = default)
{
if (client == null) throw new ArgumentNullException(nameof(client));
Expand All @@ -36,29 +38,8 @@ public static class AppClusterClientExtensions
return list?.FirstOrDefault();
}

/// <summary>获取集群</summary>
public static Task<Cluster?> 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<Cluster>($"envs/{env}/apps/{client.AppId}/clusters/{clusterName}", cancellationToken);
}

/// <summary>创建集群</summary>
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<Cluster>($"envs/{env}/apps/{client.AppId}/clusters", cluster, cancellationToken);
}

/// <summary>获取App信息</summary>
/// <summary>3.2.2 获取App信息</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_322-%e8%8e%b7%e5%8f%96app%e4%bf%a1%e6%81%af" />
#if NET40
public static Task<IList<AppInfo>?> GetAppsInfo(this IAppClusterClient client,
ICollection<string>? appIds = null, CancellationToken cancellationToken = default)
Expand All @@ -81,7 +62,32 @@ public static Task CreateCluster(this IAppClusterClient client, string env,
#endif
}

/// <summary>获取集群下所有Namespace信息</summary>
/// <summary>3.2.3 获取集群接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_323-%e8%8e%b7%e5%8f%96%e9%9b%86%e7%be%a4%e6%8e%a5%e5%8f%a3" />
public static Task<Cluster?> 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<Cluster>($"envs/{env}/apps/{client.AppId}/clusters/{clusterName}", cancellationToken);
}

/// <summary>3.2.4 创建集群接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_324-%e5%88%9b%e5%bb%ba%e9%9b%86%e7%be%a4%e6%8e%a5%e5%8f%a3" />
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<Cluster>($"envs/{env}/apps/{client.AppId}/clusters", cluster, cancellationToken);
}

/// <summary>3.2.5 获取集群下所有Namespace信息接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_325-%e8%8e%b7%e5%8f%96%e9%9b%86%e7%be%a4%e4%b8%8b%e6%89%80%e6%9c%89namespace%e4%bf%a1%e6%81%af%e6%8e%a5%e5%8f%a3" />
#if NET40
public static Task<IList<Namespace>?> GetNamespaces(this IAppClusterClient client, string env,
#else
Expand All @@ -99,9 +105,10 @@ public static Task CreateCluster(this IAppClusterClient client, string env,
#endif
}

/// <summary>创建Namespace,可以通过此接口创建Namespace,调用此接口需要授予第三方APP对目标APP的管理权限。</summary>
/// <summary>3.2.7 创建Namespace。可以通过此接口创建Namespace,调用此接口需要授予第三方APP对目标APP的管理权限。</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_327-%e5%88%9b%e5%bb%banamespace" />
public static Task<AppNamespace> 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));
Expand Down
15 changes: 15 additions & 0 deletions src/Apollo.OpenApi/Model/PageModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Com.Ctrip.Framework.Apollo.OpenApi.Model;

public class PageModel<T>
{
#if NET40
public IList<T>? Content { get; set; }
#else
public IReadOnlyList<T>? Content { get; set; }
#endif
public int Page { get; set; }

public int Size { get; set; }

public int Total { get; set; }
}
57 changes: 41 additions & 16 deletions src/Apollo.OpenApi/NamespaceClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace Com.Ctrip.Framework.Apollo.OpenApi
{
public static class NamespaceClientExtensions
{
/// <summary>获取信息</summary>
/// <summary>3.2.6 获取某个Namespace信息接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_326-%e8%8e%b7%e5%8f%96%e6%9f%90%e4%b8%aanamespace%e4%bf%a1%e6%81%af%e6%8e%a5%e5%8f%a3" />
public static Task<Namespace?> GetNamespaceInfo(this INamespaceClient client,
CancellationToken cancellationToken = default)
{
Expand All @@ -16,7 +17,8 @@ public static class NamespaceClientExtensions
return client.Get<Namespace>($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}", cancellationToken);
}

/// <summary>获取当前编辑人</summary>
/// <summary>3.2.8 获取某个Namespace当前编辑人接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_328-%e8%8e%b7%e5%8f%96%e6%9f%90%e4%b8%aanamespace%e5%bd%93%e5%89%8d%e7%bc%96%e8%be%91%e4%ba%ba%e6%8e%a5%e5%8f%a3" />
public static Task<NamespaceLock?> GetNamespaceLock(this INamespaceClient client,
CancellationToken cancellationToken = default)
{
Expand All @@ -25,19 +27,21 @@ public static class NamespaceClientExtensions
return client.Get<NamespaceLock>($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/lock", cancellationToken);
}

/// <summary>获取配置</summary>
/// <summary>3.2.9 读取配置接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_329-%e8%af%bb%e5%8f%96%e9%85%8d%e7%bd%ae%e6%8e%a5%e5%8f%a3" />
public static Task<Item?> 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));

return client.Get<Item>($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/items/{key}", cancellationToken);
}

/// <summary>新增配置</summary>
/// <summary>3.2.10 新增配置接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_3210-%e6%96%b0%e5%a2%9e%e9%85%8d%e7%bd%ae%e6%8e%a5%e5%8f%a3" />
public static Task<Item> 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));
Expand All @@ -47,9 +51,10 @@ public static Task<Item> CreateItem(this INamespaceClient client,
return client.Post<Item>($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/items", item, cancellationToken);
}

/// <summary>修改配置</summary>
/// <summary>3.2.11 修改配置接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_3211-%e4%bf%ae%e6%94%b9%e9%85%8d%e7%bd%ae%e6%8e%a5%e5%8f%a3" />
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));
Expand All @@ -59,9 +64,10 @@ public static Task UpdateItem(this INamespaceClient client,
return client.Put<Item>($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/items/{item.Key}", item, cancellationToken);
}

/// <summary>创建或修改配置</summary>
/// <summary>3.2.11 修改配置接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_3211-%e4%bf%ae%e6%94%b9%e9%85%8d%e7%bd%ae%e6%8e%a5%e5%8f%a3" />
public static Task<Item> 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));
Expand All @@ -74,10 +80,11 @@ public static Task<Item> CreateOrUpdateItem(this INamespaceClient client,
return client.Put<Item>($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/items/{item.Key}?createIfNotExists=true", item, cancellationToken);
}

/// <summary>删除配置</summary>
/// <summary>3.2.12 删除配置接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_3212-%e5%88%a0%e9%99%a4%e9%85%8d%e7%bd%ae%e6%8e%a5%e5%8f%a3" />
/// <returns>存在时删除后返回true,或者返回false</returns>
public static Task<bool> 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));
Expand All @@ -86,9 +93,10 @@ public static Task<bool> 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);
}

/// <summary>发布配置</summary>
/// <summary>3.2.13 发布配置接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_3213-%e5%8f%91%e5%b8%83%e9%85%8d%e7%bd%ae%e6%8e%a5%e5%8f%a3" />
public static Task<Release> 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));
Expand All @@ -98,7 +106,8 @@ public static Task<Release> Publish(this INamespaceClient client,
return client.Post<Release>($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/releases", release, cancellationToken);
}

/// <summary>获取当前生效的已发布配置接口</summary>
/// <summary>3.2.14 获取某个Namespace当前生效的已发布配置接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_3214-%e8%8e%b7%e5%8f%96%e6%9f%90%e4%b8%aanamespace%e5%bd%93%e5%89%8d%e7%94%9f%e6%95%88%e7%9a%84%e5%b7%b2%e5%8f%91%e5%b8%83%e9%85%8d%e7%bd%ae%e6%8e%a5%e5%8f%a3" />
public static Task<Release?> GetLatestActiveRelease(this INamespaceClient client,
CancellationToken cancellationToken = default)
{
Expand All @@ -107,13 +116,29 @@ public static Task<Release> Publish(this INamespaceClient client,
return client.Get<Release>($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/releases/latest", cancellationToken);
}

/// <summary>回滚已发布配置接口</summary>
/// <summary>3.2.15 回滚已发布配置接口</summary>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_3215-%e5%9b%9e%e6%bb%9a%e5%b7%b2%e5%8f%91%e5%b8%83%e9%85%8d%e7%bd%ae%e6%8e%a5%e5%8f%a3" />
public static Task<bool> Rollback(this INamespaceClient client, string @operator, int releaseId,
CancellationToken cancellationToken = default)
{
if (client == null) throw new ArgumentNullException(nameof(client));

return client.Put($"envs/{client.Env}/releases/{releaseId}/rollback?operator={WebUtility.UrlEncode(@operator)}", cancellationToken);
}

/// <summary>3.2.16 分页获取配置项接口</summary>
/// <param name="client"></param>
/// <param name="page">从 0 开始</param>
/// <param name="size"></param>
/// <param name="cancellationToken"></param>
/// <see href="https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform?id=_3216-%e5%88%86%e9%a1%b5%e8%8e%b7%e5%8f%96%e9%85%8d%e7%bd%ae%e9%a1%b9%e6%8e%a5%e5%8f%a3" />
public static async Task<PageModel<Item>> 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<PageModel<Item>>($"envs/{client.Env}/apps/{client.AppId}/clusters/{client.Cluster}/namespaces/{client.Namespace}/items?page={page}&size={size}", cancellationToken);

return result ?? new PageModel<Item>();
}
}
}
2 changes: 1 addition & 1 deletion test/Apollo.OpenApi.Tests/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> AppIds = new List<string> { "apollo.net", "SampleApp" };
Expand Down
12 changes: 12 additions & 0 deletions test/Apollo.OpenApi.Tests/NamespaceClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 039ddd0

Please sign in to comment.