Skip to content

Commit

Permalink
Merge pull request #77 from pengweiqhca/master
Browse files Browse the repository at this point in the history
添加OpenApi客户端实现
  • Loading branch information
pengweiqhca authored Aug 4, 2019
2 parents faeb5b2 + 1ca2924 commit 24933cd
Show file tree
Hide file tree
Showing 60 changed files with 1,358 additions and 126 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*.sln.docstates
.vs

*.cmd

#Auto Generate files
__ConfigTemp/

Expand Down
5 changes: 3 additions & 2 deletions Apollo.ConfigAdapter.Yaml.Tests/YamlConfigAdapterTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Com.Ctrip.Framework.Apollo.ConfigAdapter;
using Com.Ctrip.Framework.Apollo;
using Com.Ctrip.Framework.Apollo.ConfigAdapter;
using Com.Ctrip.Framework.Apollo.Enums;
using System;
using Xunit;

namespace Com.Ctrip.Framework.Apollo
namespace Apollo.ConfigAdapter.Yaml.Tests
{
public class YamlConfigAdapterTest
{
Expand Down
28 changes: 14 additions & 14 deletions Apollo.Configuration.Demo/Apollo.Configuration.Demo.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Apollo.ConfigAdapter.Yaml\Apollo.ConfigAdapter.Yaml.csproj" />
<ProjectReference Include="..\Apollo.Configuration\Apollo.Configuration.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Apollo.ConfigAdapter.Yaml\Apollo.ConfigAdapter.Yaml.csproj" />
<ProjectReference Include="..\Apollo.Configuration\Apollo.Configuration.csproj" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions Apollo.Configuration.Demo/ConfigurationDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public ConfigurationDemo()
services.AddOptions()
.Configure<Value>(config)
.Configure<Value>("other", anotherConfig);

#pragma warning disable 618
services.AddSingleton<ApolloConfigurationManager>();

#pragma warning restore 618
var serviceProvider = services.BuildServiceProvider();

var optionsMonitor = serviceProvider.GetService<IOptionsMonitor<Value>>();
Expand Down
5 changes: 3 additions & 2 deletions Apollo.Configuration.Tests/ApolloConfigurationBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Com.Ctrip.Framework.Apollo.Core;
using Com.Ctrip.Framework.Apollo;
using Com.Ctrip.Framework.Apollo.Core;
using Microsoft.Extensions.Configuration;
using System.Linq;
using Xunit;

namespace Com.Ctrip.Framework.Apollo
namespace Apollo.Configuration.Tests
{
public class ApolloConfigurationBuilderTest
{
Expand Down
1 change: 1 addition & 0 deletions Apollo.Configuration/Apollo.Configuration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
$(PackageReleaseNotes)</Description>
<PackageIconUrl>https://raw.githubusercontent.com/ctripcorp/apollo/master/apollo-portal/src/main/resources/static/img/config.png</PackageIconUrl>
<PackageProjectUrl>$(RepositoryUrl)/$(AssemblyName)</PackageProjectUrl>
<RootNamespace>Com.Ctrip.Framework.Apollo</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.0.3</Version>
</PropertyGroup>
Expand Down
3 changes: 2 additions & 1 deletion Apollo.ConfigurationManager.Demo/ConfigurationManagerDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class ConfigurationManagerDemo

public ConfigurationManagerDemo()
{
#pragma warning disable 618
config = ApolloConfigurationManager.GetConfig(ConfigConsts.NamespaceApplication + ".json",
#pragma warning restore 618
ConfigConsts.NamespaceApplication + ".xml",
ConfigConsts.NamespaceApplication + ".yml",
ConfigConsts.NamespaceApplication + ".yaml",
Expand All @@ -33,7 +35,6 @@ public string GetConfig(string key)

private void OnChanged(object sender, ConfigChangeEventArgs changeEvent)
{
Console.WriteLine("Changes for namespace {0}", changeEvent.Namespace);
foreach (var (key, value) in changeEvent.Changes)
{
Console.WriteLine("Change - key: {0}, oldValue: {1}, newValue: {2}, changeType: {3}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ $(PackageReleaseNotes)</Description>
<PackageIconUrl>https://raw.githubusercontent.com/ctripcorp/apollo/master/apollo-portal/src/main/resources/static/img/config.png</PackageIconUrl>
<PackageProjectUrl>$(RepositoryUrl)/$(AssemblyName)</PackageProjectUrl>
<PackageTags>$(PackageTags) ConfigurationBuilder ConfigurationManager</PackageTags>
<RootNamespace>Com.Ctrip.Framework.Apollo</RootNamespace>
<TargetFrameworks>net471;net45;netstandard2.0</TargetFrameworks>
<Version>2.0.3</Version>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion Apollo.ConfigurationManager/ApolloConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static class ApolloConfigurationManager
/// <returns> config instance </returns>
public static Task<IConfig> GetConfig([NotNull]string namespaceName)
{
if (string.IsNullOrEmpty(namespaceName)) throw new ArgumentException("message", nameof(namespaceName));
if (string.IsNullOrEmpty(namespaceName)) throw new ArgumentNullException(nameof(namespaceName));

return Manager.GetConfig(namespaceName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>Com.Ctrip.Framework.Apollo.OpenApi.DependencyInjection</AssemblyName>
<PackageReleaseNotes></PackageReleaseNotes>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<Description>携程Apollo的OpenApi客户端,入口接口IOpenApiFactory</Description>
<PackageIconUrl>https://raw.githubusercontent.com/ctripcorp/apollo/master/apollo-portal/src/main/resources/static/img/config.png</PackageIconUrl>
<PackageProjectUrl>$(RepositoryUrl)/$(AssemblyName)</PackageProjectUrl>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.0.0</Version>
<RootNamespace>Com.Ctrip.Framework.Apollo.OpenApi</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="2.2.0" />

<ProjectReference Include="..\Apollo.OpenApi\Apollo.OpenApi.csproj" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions Apollo.OpenApi.DependencyInjection/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Com.Ctrip.Framework.Apollo.OpenApi;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System;
using System.Net.Http;

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddApolloOpenApi([NotNull] this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));

services.AddOptions().AddHttpClient()
.TryAddSingleton<IOpenApiFactory>(provider => new OpenApiFactory(
provider.GetRequiredService<IOptions<OpenApiOptions>>().Value,
provider.GetRequiredService<IHttpMessageHandlerFactory>().CreateHandler));

return services;
}
}
}
21 changes: 21 additions & 0 deletions Apollo.OpenApi.Tests/Apollo.OpenApi.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net461;netcoreapp2.2</TargetFrameworks>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.*" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.*" />
<PackageReference Include="xunit" Version="2.*" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.*" />

<ProjectReference Include="..\Apollo.OpenApi.DependencyInjection\Apollo.OpenApi.DependencyInjection.csproj" />

<ProjectReference Include="..\Apollo\Apollo.csproj" />
</ItemGroup>

</Project>
68 changes: 68 additions & 0 deletions Apollo.OpenApi.Tests/AppClusterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Com.Ctrip.Framework.Apollo.Core;
using Com.Ctrip.Framework.Apollo.OpenApi;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace Com.Ctrip.Framework.Apollo
{
public class AppClusterTest : BaseTest
{
public AppClusterTest(ITestOutputHelper output) : base(output) { }

[Fact]
public async Task GetEnvClusterInfo()
{
var result = await CreateAppClusterClient().GetEnvClusterInfo().ConfigureAwait(false);

Dump(result);

Assert.NotNull(result);

var @default = result.FirstOrDefault(ec => ec.Env == "DEV");

Assert.NotNull(@default);

Assert.Contains(ConfigConsts.ClusterNameDefault, @default.Clusters);
}

[Fact]
public async Task GetAppInfo()
{
var result = await CreateAppClusterClient().GetAppInfo().ConfigureAwait(false);

Dump(result);

Assert.NotNull(result);

Assert.Equal(AppIds[0], result.AppId);
}

[Fact]
public async Task GetAppsInfo()
{
var result = await CreateAppClusterClient().GetAppsInfo().ConfigureAwait(false);

Assert.NotNull(result);
Assert.NotEmpty(result); result = await CreateAppClusterClient().GetAppsInfo(AppIds).ConfigureAwait(false);

Dump(result);

Assert.NotNull(result);
Assert.Equal(2, result.Count);
}

[Fact]
public async Task GetNamespaces()
{
var result = await CreateAppClusterClient().GetNamespaces(Env).ConfigureAwait(false);

Dump(result);

Assert.NotNull(result);
Assert.Contains(result, ns => ns.NamespaceName == ConfigConsts.NamespaceApplication);
Assert.NotEmpty(result.First(ns => ns.NamespaceName == ConfigConsts.NamespaceApplication).Items);
}
}
}
32 changes: 32 additions & 0 deletions Apollo.OpenApi.Tests/BaseTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Com.Ctrip.Framework.Apollo.OpenApi;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using Com.Ctrip.Framework.Apollo.Core;
using Xunit.Abstractions;

namespace Com.Ctrip.Framework.Apollo
{
public abstract class BaseTest
{
private readonly ITestOutputHelper _output;

public static IOpenApiFactory Factory { get; } = new OpenApiFactory(new OpenApiOptions
{
PortalUrl = new Uri("http://106.12.25.204:8070"),
Token = "19419f7d3e5a1b0b0cfe3e238b36e09718fb8e94"
});

public static IReadOnlyList<string> AppIds = new List<string> { "apollo-client", "apollo-demo" };

public static string Env => "DEV";

public static IAppClusterClient CreateAppClusterClient() => Factory.CreateAppClusterClient(AppIds[0]);

public static INamespaceClient CreateNamespaceClient() => Factory.CreateNamespaceClient(AppIds[0], Env);

protected BaseTest(ITestOutputHelper output) => _output = output;

protected void Dump(object obj) => _output.WriteLine(JsonConvert.SerializeObject(obj, Formatting.Indented));
}
}
Loading

0 comments on commit 24933cd

Please sign in to comment.