Skip to content

Commit 89f43a9

Browse files
authored
add AuthorizationToken to http headers (#33)
closes #32
1 parent 1bd4052 commit 89f43a9

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

PrinterApp/ConfigFile.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class ConfigFile
1212
public string TempSavePath { get; set; } = Path.GetTempPath() + ".printerApp";
1313
public bool StartWithWindows { get; set; } = false;
1414
public bool AutoUpdate { get; set; } = true;
15+
public string AuthorizationToken { get; set; } = "token";
1516

1617
public void LoadConfig(string fileName)
1718
{
@@ -35,6 +36,7 @@ public void LoadConfig(string fileName)
3536
TempSavePath = config.TempSavePath;
3637
StartWithWindows = config.StartWithWindows;
3738
AutoUpdate = config.AutoUpdate;
39+
AuthorizationToken = config.AuthorizationToken;
3840
WriteConfig(configPath);
3941
Log.Debug("Load from config file\n" +
4042
JsonConvert.SerializeObject(this, Formatting.Indented));

PrinterApp/PrinterApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<Title>PrinterApp</Title>
1212
<Description>gui fo work https://app.profcomff.com/print/docs</Description>
1313
<Authors>Dyakov EI</Authors>
14-
<Version>2.0.11.0</Version>
14+
<Version>2.0.12.0</Version>
1515
<Company>dyakov.space</Company>
1616
<Copyright>dyakov.space @ 2022</Copyright>
1717
</PropertyGroup>

PrinterApp/PrinterModel.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Net;
77
using System.Net.Http;
8+
using System.Net.Http.Headers;
89
using System.Reflection;
910
using System.Threading.Tasks;
1011
using System.Windows;
@@ -33,6 +34,7 @@ public class PrinterModel
3334
public PrinterViewModel PrinterViewModel { get; } = new();
3435

3536
private readonly ConfigFile _configFile;
37+
private readonly HttpClient _httpClient;
3638

3739
public PrinterModel(ConfigFile configFile)
3840
{
@@ -42,6 +44,10 @@ public PrinterModel(ConfigFile configFile)
4244
SecurityProtocolType.Tls |
4345
SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
4446

47+
_httpClient = new HttpClient();
48+
_httpClient.DefaultRequestHeaders.Authorization
49+
= new AuthenticationHeaderValue("token", _configFile.AuthorizationToken);
50+
4551
if (!Directory.Exists(_configFile.TempSavePath))
4652
Directory.CreateDirectory(_configFile.TempSavePath);
4753

@@ -98,9 +104,9 @@ public async void PrintAsync(bool printDialog)
98104
saveFilePath =
99105
saveFilePath.Replace(Path.DirectorySeparatorChar.ToString(), "/");
100106
ShowComplement();
101-
using var client = new HttpClient();
102107
await using var s =
103-
await client.GetStreamAsync("https://cdn.profcomff.com/app/printer/iddqd.pdf");
108+
await _httpClient.GetStreamAsync(
109+
"https://cdn.profcomff.com/app/printer/iddqd.pdf");
104110
await using var fs = new FileStream(saveFilePath, FileMode.OpenOrCreate);
105111
await s.CopyToAsync(fs);
106112
PrintFile(saveFilePath, new PrintOptions("", 1, false),
@@ -117,11 +123,10 @@ public async void PrintAsync(bool printDialog)
117123
$"{GetType().Name} {MethodBase.GetCurrentMethod()?.Name}: Start response code {PrinterViewModel.CodeTextBoxText}");
118124
var patchFrom = $"{FileUrl}/{PrinterViewModel.CodeTextBoxText}";
119125
PrinterViewModel.DownloadNotInProgress = false;
120-
var httpClient = new HttpClient();
121126
try
122127
{
123128
var response =
124-
await httpClient.GetAsync($"{FileUrl}/{PrinterViewModel.CodeTextBoxText}");
129+
await _httpClient.GetAsync($"{FileUrl}/{PrinterViewModel.CodeTextBoxText}");
125130
if (response.StatusCode == HttpStatusCode.OK)
126131
{
127132
Marketing.CheckCode(
@@ -197,9 +202,8 @@ private async Task Download(ReceiveOutput fileWithOptions, string patchFrom,
197202
var saveFilePath = _configFile.TempSavePath + Path.DirectorySeparatorChar + name;
198203
saveFilePath = saveFilePath.Replace(Path.DirectorySeparatorChar.ToString(), "/");
199204
ShowComplement();
200-
using var client = new HttpClient();
201205
await using var s =
202-
await client.GetStreamAsync($"{StaticUrl}/{fileWithOptions.Filename}");
206+
await _httpClient.GetStreamAsync($"{StaticUrl}/{fileWithOptions.Filename}");
203207
await using var fs = new FileStream(saveFilePath, FileMode.OpenOrCreate);
204208
await s.CopyToAsync(fs);
205209
Marketing.FinishDownload(pathFrom: patchFrom,

0 commit comments

Comments
 (0)