zhaolei
3 days ago 4a2e5b9a21940f11757be37d99f0944e240e908b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using Microsoft.Extensions.Logging;
using System;
using System.Net.Http;
 
namespace Prow.Web
{
    /// <summary>
    ///
    /// </summary>
    internal class DefaultIPLocator : IIPLocator
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        public virtual string Locate(IPLocatorOption option) => string.Empty;
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        protected string Locate<T>(IPLocatorOption option) where T : class
        {
            var ret = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(option.Url) && option.HttpClient != null)
                {
                    var task = option.HttpClient.GetAsJsonAsync<T>(option.Url);
                    task.Wait(option.RequestTimeout);
                    if (task.IsCompleted && !task.IsFaulted && task.Result != null) ret = task.Result.ToString() ?? string.Empty;
                }
            }
            catch (Exception ex)
            {
                option.Logger.LogError(ex, option.Url);
            }
            return ret;
        }
    }
}