-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
302 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#region License | ||
/* | ||
Copyright 2022-2024 Dmitrii Evdokimov | ||
Open source software | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
#endregion | ||
|
||
using System.Text; | ||
using System.Xml; | ||
|
||
namespace Api5704; | ||
|
||
public static class ApiExtra | ||
{ | ||
/// <summary> | ||
/// Пакетная обработка папок с запросами (Extra). | ||
/// </summary> | ||
/// <param name="dir">Папка с исходными запросами.</param> | ||
/// <param name="requests">Папка с готовыми запросами.</param> | ||
/// <param name="results">Папка с готовыми результатами.</param> | ||
/// <param name="answers">Папка с готовыми ответами.</param> | ||
public static async Task PostRequestFolderAsync(string dir, string requests, string results, string answers) | ||
{ | ||
Directory.CreateDirectory(requests); | ||
Directory.CreateDirectory(results); | ||
Directory.CreateDirectory(answers); | ||
|
||
foreach (var file in Directory.GetFiles(dir, "*.xml")) | ||
{ | ||
byte[] data = File.ReadAllBytes(file); | ||
|
||
if (data[0] == 0x30) | ||
{ | ||
data = PKCS7.CleanSign(data); | ||
} | ||
|
||
XmlDocument doc = new(); | ||
doc.LoadXml(Encoding.UTF8.GetString(data)); | ||
string id = doc.DocumentElement!.GetAttribute("ИдентификаторЗапроса"); | ||
|
||
string date = DateTime.Now.ToString("yyyy-MM-dd"); | ||
string name = $"{date}.{id}"; | ||
|
||
string request = Path.Combine(results, name + ".request.xml"); | ||
string result = Path.Combine(results, name + ".result.xml"); | ||
string answer = Path.Combine(answers, name + ".answer.xml"); | ||
|
||
File.Copy(file, request, true); | ||
|
||
await Api.PostRequestAsync(Api.auto, request, result, answer); | ||
|
||
if (File.Exists(answer)) | ||
{ | ||
File.Delete(file); | ||
} | ||
|
||
Thread.Sleep(1000); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#region License | ||
/* | ||
Copyright 2022-2024 Dmitrii Evdokimov | ||
Open source software | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
#endregion | ||
|
||
using System.Text; | ||
|
||
namespace Api5704; | ||
|
||
public class ApiHelper | ||
{ | ||
/// <summary> | ||
/// Разобрать ответ сервера, записать файл с текстом полученной квитанции и вернуть код результата запроса | ||
/// и содержимое квитанции. | ||
/// </summary> | ||
/// <param name="file">Имя файла для ответной квитанции. | ||
/// Если в конфиге CleanSign: true, то будут очищенный файл и файл.sig в исходном формате PKCS#7.</param> | ||
/// <param name="response">Комплексный ответ сервера.</param> | ||
/// <returns>Код результата запроса и содержимое квитанции, возвращенные сервером.</returns> | ||
/// <exception cref="UnauthorizedAccessException"></exception> | ||
public static async Task<(int, string)> WriteResultFileAsync(string file, HttpResponseMessage response, bool clean) | ||
{ | ||
if (File.Exists(file)) | ||
{ | ||
File.Delete(file); | ||
|
||
if (File.Exists(file)) | ||
{ | ||
throw new UnauthorizedAccessException("Result file not deleted."); | ||
} | ||
} | ||
|
||
int result = (int)response.StatusCode; | ||
Console.WriteLine($"Status code: {result} {response.StatusCode}"); | ||
byte[] data = await response.Content.ReadAsByteArrayAsync(); | ||
|
||
if (clean) | ||
{ | ||
// Write signed file | ||
await File.WriteAllBytesAsync(file + ".sig", data); | ||
|
||
// Clean data | ||
data = PKCS7.CleanSign(data); | ||
|
||
// Write clean XML | ||
await File.WriteAllBytesAsync(file, data); | ||
} | ||
else | ||
{ | ||
// Write signed file | ||
await File.WriteAllBytesAsync(file, data); | ||
|
||
// Clean data | ||
data = PKCS7.CleanSign(data); | ||
} | ||
|
||
string content = Encoding.UTF8.GetString(data); | ||
|
||
return (result, content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.