1+ #region Apache License Version 2.0
2+ /*----------------------------------------------------------------
3+
4+ Copyright 2024 Suzhou Senparc Network Technology Co.,Ltd.
5+
6+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
7+ except in compliance with the License. You may obtain a copy of the License at
8+
9+ http://www.apache.org/licenses/LICENSE-2.0
10+
11+ Unless required by applicable law or agreed to in writing, software distributed under the
12+ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+ either express or implied. See the License for the specific language governing permissions
14+ and limitations under the License.
15+
16+ Detail: https://github.com/Senparc/Senparc.CO2NET/blob/master/LICENSE
17+
18+ ----------------------------------------------------------------*/
19+ #endregion Apache License Version 2.0
20+
21+ /*----------------------------------------------------------------
22+ Copyright (C) 2024 Senparc
23+
24+ 文件名:ApiClient.cs
25+ 文件功能描述:ApiClient, used to provide APiClient Container, work for such as Aspire.
26+
27+
28+ 创建标识:Senparc - 20241118
29+
30+ ----------------------------------------------------------------*/
31+
32+
33+ #if ! NET462
34+ using Microsoft . Extensions . DependencyInjection ;
35+ using Senparc . CO2NET . Extensions ;
36+ using Senparc . CO2NET . HttpUtility ;
37+ using System ;
38+ using System . Collections . Generic ;
39+ using System . Text ;
40+
41+ namespace Senparc . CO2NET
42+ {
43+ /// <summary>
44+ /// ApiClient, used to provide APiClient Container, work for such as Aspire.
45+ /// </summary>
46+ public sealed class ApiClient
47+ {
48+ public SenparcHttpClient SenparcHttpClient { get ; private set ; }
49+
50+ internal ApiClient ( IServiceProvider serviceProvider , string httpClientName , string apiClientName )
51+ {
52+ SenparcHttpClient = SenparcHttpClient . GetInstanceByName ( serviceProvider , httpClientName ) ;
53+
54+ if ( ! apiClientName . IsNullOrEmpty ( ) )
55+ {
56+ SetBaseAddress ( apiClientName ) ;
57+ }
58+ }
59+
60+ internal ApiClient ( IServiceProvider serviceProvider , string apiClientName )
61+ {
62+ SenparcHttpClient = serviceProvider . GetService < SenparcHttpClient > ( ) ;
63+ SetBaseAddress ( apiClientName ) ;
64+ }
65+
66+ private void SetBaseAddress ( string apiClientName )
67+ {
68+ SenparcHttpClient . Client . BaseAddress = new ( $ "https+http://{ apiClientName } ") ;
69+ }
70+ }
71+
72+ /// <summary>
73+ /// ApiClientHelper, set ApiClient parameters and get ApiClient object
74+ /// </summary>
75+ public sealed class ApiClientHelper
76+ {
77+ private readonly IServiceProvider _serviceProvider ;
78+
79+ public ApiClientHelper ( IServiceProvider serviceProvider )
80+ {
81+ _serviceProvider = serviceProvider ;
82+ }
83+
84+ /// <summary>
85+ /// Use an apiClientName to get the ApiClient object
86+ /// </summary>
87+ /// <param name="apiClientName"></param>
88+ /// <returns></returns>
89+ public ApiClient ConnectApiClient ( string apiClientName )
90+ {
91+ var apiClient = new ApiClient ( _serviceProvider , apiClientName ) ;
92+ return apiClient ;
93+ }
94+
95+ /// <summary>
96+ /// Use an httpClientName and an apiClientName(optional) to get the ApiClient object
97+ /// </summary>
98+ /// <param name="httpClientName"></param>
99+ /// <param name="apiClientName"></param>
100+ /// <returns></returns>
101+ public ApiClient ConnectHttpClient ( string httpClientName , string apiClientName = null )
102+ {
103+ var apiClient = new ApiClient ( _serviceProvider , httpClientName , apiClientName ) ;
104+ return apiClient ;
105+ }
106+ }
107+ }
108+ #endif
0 commit comments