-
Notifications
You must be signed in to change notification settings - Fork 2
/
Api.cs
150 lines (122 loc) · 4.96 KB
/
Api.cs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.Configuration.UserSecrets;
namespace Canaan
{
public abstract class Api
{
#region Constructors
static Api()
{
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CANAAN_PRODUCTION")) || !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("FUNCTIONS_WORKER_RUNTIME")))
{
Configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
}
else if (Assembly.GetEntryAssembly().GetName().Name == "NewsAlpha.CLI")
{
Configuration = new ConfigurationBuilder()
.AddJsonFile("config.json", optional: true)
.Build();
}
else
{
Configuration = new ConfigurationBuilder()
.AddJsonFile("config.json", optional: true)
.AddUserSecrets("81dfcf5f-a19e-4cab-a546-9fa5b09927b8")
.Build();
}
HttpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Canaan/0.1");
}
public Api(CancellationToken ct)
{
if (Logger == null)
{
throw new InvalidOperationException("A logger is not assigned.");
}
CancellationToken = ct;
Type = this.GetType();
}
public Api(): this(Cts.Token) {}
#endregion
#region Properties
public static DirectoryInfo AssemblyDirectory { get; } = new FileInfo(Assembly.GetEntryAssembly().Location).Directory;
public static Version AssemblyVersion { get; } = Assembly.GetEntryAssembly().GetName().Version;
public static DirectoryInfo CurrentDirectory { get; } = new DirectoryInfo(Directory.GetCurrentDirectory());
public static IConfigurationRoot Configuration { get; protected set; }
public static Logger Logger { get; protected set; }
public static CancellationTokenSource Cts { get; } = new CancellationTokenSource();
public static HttpClient HttpClient { get; } = new HttpClient();
public static string YY = DateTime.Now.Year.ToString().Substring(2, 2);
public bool Initialized { get; protected set; }
public static bool IsAzureFunction { get; set; }
public CancellationToken CancellationToken { get; protected set; }
public Type Type { get; }
#endregion
#region Methods
public static void SetLogger(Logger logger)
{
Logger = logger;
}
public static void SetLoggerIfNone(Logger logger)
{
if (Logger == null)
{
Logger = logger;
}
}
public static void SetDefaultLoggerIfNone()
{
if (Logger == null)
{
Logger = new ConsoleLogger();
}
}
public static string Config(string i)
{
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CANAAN_PRODUCTION")) || !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("FUNCTIONS_WORKER_RUNTIME")) || IsAzureFunction)
{
i = i.Replace(":", "_");
return Api.Configuration[i];
}
else
{
return Api.Configuration[i];
}
}
public static void Info(string messageTemplate, params object[] args) => Logger.Info(messageTemplate, args);
public static void Debug(string messageTemplate, params object[] args) => Logger.Debug(messageTemplate, args);
public static void Error(string messageTemplate, params object[] args) => Logger.Error(messageTemplate, args);
public static void Error(Exception ex, string messageTemplate, params object[] args) => Logger.Error(ex, messageTemplate, args);
public static Logger.Op Begin(string messageTemplate, params object[] args) => Logger.Begin(messageTemplate, args);
public string CalculateMD5Hash(string input)
{
// step 1, calculate MD5 hash from input
MD5 md5 = MD5.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
// step 2, convert byte array to hex string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
public void ThrowIfNotInitialized()
{
if (!this.Initialized) throw new ApiNotInitializedException(this);
}
#endregion
}
}