Skip to content

Commit 06728eb

Browse files
authored
feat(JuHeIpLocatorProvider): bump version 10.1.0 (#946)
* doc: 更新注释文档 * chore: bump version 10.0.1 * refactor: 更新未配置 Key 逻辑提高性能 * refactor: 更新依赖增加 LastError 参数 * revert: 撤销项目依赖 * chore: bump version 10.1.0
1 parent da35f14 commit 06728eb

6 files changed

Lines changed: 203 additions & 152 deletions

File tree

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

3+
<PropertyGroup>
4+
<Version>10.1.0</Version>
5+
</PropertyGroup>
6+
37
<PropertyGroup>
48
<PackageTags>Bootstrap Blazor WebAssembly wasm UI Components JuHe Ip Locator</PackageTags>
59
<Description>Bootstrap UI components extensions of JuHeIpLocator</Description>
610
</PropertyGroup>
711

812
<ItemGroup>
9-
<PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" />
13+
<PackageReference Include="BootstrapBlazor" Version="10.4.0" />
1014
</ItemGroup>
1115

1216
</Project>

src/components/BootstrapBlazor.JuHeIpLocatorProvider/Extensions/ServiceCollectionExtensions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

@@ -7,12 +7,14 @@
77
namespace Microsoft.Extensions.DependencyInjection;
88

99
/// <summary>
10-
/// BootstrapBlazor 服务扩展类
10+
/// <para lang="zh">BootstrapBlazor 服务扩展类</para>
11+
/// <para lang="en">BootstrapBlazor service extensions</para>
1112
/// </summary>
12-
public static class BootstrapBlazoJuHeIpLocatorExtensions
13+
public static class BootstrapBlazorJuHeIpLocatorExtensions
1314
{
1415
/// <summary>
15-
/// 添加 AzureOpenAIService 服务
16+
/// <para lang="zh">添加聚合搜索引擎 IP 定位器服务</para>
17+
/// <para lang="en">Adds JuHe IP locator service</para>
1618
/// </summary>
1719
/// <param name="services"></param>
1820
public static IServiceCollection AddBootstrapBlazorJuHeIpLocatorService(this IServiceCollection services)
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
55

66
namespace BootstrapBlazor.Components;
77

88
/// <summary>
9-
/// 聚合搜索引擎 IP 定位器配置类
9+
/// <para lang="zh">聚合搜索引擎 IP 定位器配置类</para>
10+
/// <para lang="en">JuHe IP locator options</para>
1011
/// </summary>
1112
class JuHeIpLocatorOptions
1213
{
1314
/// <summary>
14-
/// 聚合搜索引擎 IP 定位器 AppKey
15+
/// <para lang="zh">获得/设置 聚合搜索引擎 IP 定位器 AppKey</para>
16+
/// <para lang="en">Gets or sets the JuHe IP locator AppKey</para>
1517
/// </summary>
16-
public string Key { get; set; } = "";
18+
public string? Key { get; set; }
1719

1820
/// <summary>
19-
/// 聚合搜索引擎 IP 定位器请求地址
21+
/// <para lang="zh">获得/设置 聚合搜索引擎 IP 定位器请求地址</para>
22+
/// <para lang="en">Gets or sets the JuHe IP locator request URL</para>
2023
/// </summary>
21-
public string Url { get; set; } = "http://apis.juhe.cn/ip/ipNew";
24+
public string? Url { get; set; }
2225

2326
/// <summary>
24-
/// 聚合搜索引擎 IP 定位器请求超时时间 默认 5 秒
27+
/// <para lang="zh">获得/设置 聚合搜索引擎 IP 定位器请求超时时间 默认 5 秒</para>
28+
/// <para lang="en">Gets or sets the JuHe IP locator request timeout, default is 5 seconds</para>
2529
/// </summary>
2630
public TimeSpan Timeout { get; set; }
2731
}
Lines changed: 19 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

5-
using Microsoft.Extensions.Configuration;
65
using Microsoft.Extensions.Logging;
76
using Microsoft.Extensions.Options;
87
using System.Net.Http.Json;
9-
using System.Text.Json.Serialization;
108

119
namespace BootstrapBlazor.Components;
1210

1311
/// <summary>
14-
/// 聚合搜索引擎 IP 定位器
12+
/// <para lang="zh">聚合搜索引擎 IP 定位器</para>
13+
/// <para lang="en">JuHe IP locator provider</para>
1514
/// </summary>
1615
/// <param name="httpClientFactory"></param>
1716
/// <param name="options"></param>
@@ -26,6 +25,8 @@ class JuHeIpLocatorProvider(IHttpClientFactory httpClientFactory,
2625

2726
private JuHeIpLocatorOptions? _options;
2827

28+
private const string Url = "http://apis.juhe.cn/ip/ipNewV3";
29+
2930
/// <summary>
3031
/// <inheritdoc/>
3132
/// </summary>
@@ -52,164 +53,42 @@ private JuHeIpLocatorOptions GetOptions()
5253
{
5354
var options = juHeIpLocatorOptions.Value;
5455

55-
try
56+
if (string.IsNullOrEmpty(options.Key))
5657
{
57-
if (string.IsNullOrEmpty(options.Key))
58-
{
59-
throw new InvalidOperationException($"{nameof(JuHeIpLocatorOptions)}:Key not value in appsettings configuration file. 未配置 {nameof(JuHeIpLocatorOptions)}:Key 请在 appsettings.json 中配置 {nameof(JuHeIpLocatorOptions)}:Key");
60-
}
61-
if (string.IsNullOrEmpty(options.Url))
62-
{
63-
options.Url = "http://apis.juhe.cn/ip/ipNewV3";
64-
}
58+
LastError = $"{nameof(JuHeIpLocatorOptions)}:Key not value in appsettings configuration file. 未配置 {nameof(JuHeIpLocatorOptions)}:Key 请在 appsettings.json 中配置 {nameof(JuHeIpLocatorOptions)}:Key";
59+
Log(LastError);
60+
6561
}
66-
catch (Exception ex)
62+
if (string.IsNullOrEmpty(options.Url))
6763
{
68-
logger.LogError(ex, "{GetOptions} failed", nameof(GetOptions));
64+
options.Url = Url;
6965
}
7066
return options;
7167
}
7268

7369
/// <summary>
74-
/// 请求获得地理位置接口方法
70+
/// <para lang="zh">请求获得地理位置接口方法</para>
71+
/// <para lang="en">Fetches the geolocation data</para>
7572
/// </summary>
7673
/// <param name="url"></param>
7774
/// <param name="client"></param>
7875
/// <param name="token"></param>
79-
/// <returns></returns>
8076
protected virtual async Task<string?> Fetch(string url, HttpClient client, CancellationToken token)
8177
{
82-
var result = await client.GetFromJsonAsync<LocationResult>(url, token);
78+
var result = await client.GetFromJsonAsync<JuHeLocationResult>(url, token);
8379
if (result != null && result.ErrorCode != 0)
8480
{
85-
logger.LogError("ErrorCode: {ErrorCode} Reason: {Reason}", result.ErrorCode, result.Reason);
81+
LastError = $"ErrorCode: {result.ErrorCode} Reason: {result.Reason}";
82+
Log(LastError);
8683
}
8784
return result?.ToString();
8885
}
8986

90-
/// <summary>
91-
/// LocationResult 结构体
92-
/// </summary>
93-
class LocationResult
87+
private void Log(string? message)
9488
{
95-
/// <summary>
96-
/// 获得/设置 结果状态返回码 为 查询成功 时通讯正常
97-
/// </summary>
98-
public string? Reason { get; set; }
99-
100-
/// <summary>
101-
/// 获得/设置 错误码
102-
/// </summary>
103-
[JsonPropertyName("error_code")]
104-
public int ErrorCode { get; set; }
105-
106-
/// <summary>
107-
/// 获得/设置 定位信息
108-
/// </summary>
109-
public LocationData? Result { get; set; }
110-
111-
/// <summary>
112-
/// <inheritdoc/>
113-
/// </summary>
114-
/// <returns></returns>
115-
public override string? ToString()
89+
if (logger.IsEnabled(LogLevel.Error))
11690
{
117-
string? ret = null;
118-
if (ErrorCode == 0)
119-
{
120-
ret = Result?.Country == "中国"
121-
? $"{Result?.Prov}{Result?.City}{Result?.District} {Result?.Isp}"
122-
: $"{Result?.Continent} {Result?.Country} {Result?.City}";
123-
}
124-
return ret;
91+
logger.LogError("{message}", message);
12592
}
12693
}
127-
128-
class LocationData
129-
{
130-
/// <summary>
131-
/// 获得/设置 州
132-
/// </summary>
133-
public string? Continent { get; set; }
134-
135-
/// <summary>
136-
/// 获得/设置 国家
137-
/// </summary>
138-
public string? Country { get; set; }
139-
140-
/// <summary>
141-
/// 获得/设置 邮编
142-
/// </summary>
143-
public string? ZipCode { get; set; }
144-
145-
/// <summary>
146-
/// 获得/设置 时区
147-
/// </summary>
148-
public string? TimeZone { get; set; }
149-
150-
/// <summary>
151-
/// 获得/设置 精度
152-
/// </summary>
153-
public string? Accuracy { get; set; }
154-
155-
/// <summary>
156-
/// 获得/设置 所属
157-
/// </summary>
158-
public string? Owner { get; set; }
159-
160-
/// <summary>
161-
/// 获得/设置 运营商
162-
/// </summary>
163-
public string? Isp { get; set; }
164-
165-
/// <summary>
166-
/// 获得/设置 来源
167-
/// </summary>
168-
public string? Source { get; set; }
169-
170-
/// <summary>
171-
/// 获得/设置 区号
172-
/// </summary>
173-
public string? AreaCode { get; set; }
174-
175-
/// <summary>
176-
/// 获得/设置 行政区划代码
177-
/// </summary>
178-
public string? AdCode { get; set; }
179-
180-
/// <summary>
181-
/// 获得/设置 国家代码
182-
/// </summary>
183-
public string? AsNumber { get; set; }
184-
185-
/// <summary>
186-
/// 获得/设置 经度
187-
/// </summary>
188-
public string? Lat { get; set; }
189-
190-
/// <summary>
191-
/// 获得/设置 纬度
192-
/// </summary>
193-
public string? Lng { get; set; }
194-
195-
/// <summary>
196-
/// 获得/设置 半径
197-
/// </summary>
198-
public string? Radius { get; set; }
199-
200-
/// <summary>
201-
/// 获得/设置 省份
202-
/// </summary>
203-
public string? Prov { get; set; }
204-
205-
/// <summary>
206-
/// 获得/设置 城市
207-
/// </summary>
208-
public string? City { get; set; }
209-
210-
/// <summary>
211-
/// 获得/设置 区县
212-
/// </summary>
213-
public string? District { get; set; }
214-
}
21594
}

0 commit comments

Comments
 (0)