Files
admin/e2e/Azaion.E2E/Helpers/ApiClient.cs
T
Oleksandr Bezdieniezhnykh 5ca9ccab2c [AZ-513] [AZ-196] [AZ-183] Add /classes CRUD, /devices, fleet OTA
AZ-513: POST/PATCH/DELETE /classes for detection-class CRUD; new
DetectionClass entity, schema, DTOs, IDetectionClassService. Unblocks
ui/AZ-512.

AZ-196: POST /devices auto-assigns sequential azj-NNNN serial+email
+password and inserts a CompanionPC user. Returns plaintext credentials
for the provisioning script.

AZ-183: Resources table + POST /get-update + POST /resources/publish
for fleet OTA. Per-resource encryption_key column AES-256-CBC encrypted
at rest with ResourcesConfig.EncryptionMasterKey; ICache wraps the
per-(arch,stage) latest-versions lookup and is invalidated on publish.

Adds IDbFactory.RunAdmin<T> overload for write-and-return.

Backfills _docs/02_document/module-layout.md to satisfy the implement
skill's File Ownership prerequisite (the _docs/ artifact set predates
the Step 1.5 module-layout addition).

Code review: PASS_WITH_WARNINGS — see
_docs/03_implementation/reviews/batch_05_review.md.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-13 04:34:42 +03:00

92 lines
3.6 KiB
C#

using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
namespace Azaion.E2E.Helpers;
public sealed class ApiClient : IDisposable
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true
};
private readonly HttpClient _httpClient;
private readonly bool _disposeClient;
public ApiClient(HttpClient httpClient, bool disposeClient = false)
{
_httpClient = httpClient;
_disposeClient = disposeClient;
}
public void Dispose()
{
if (_disposeClient)
_httpClient.Dispose();
}
public void SetAuthToken(string token)
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
public async Task<string> LoginAsync(string email, string password, CancellationToken cancellationToken = default)
{
using var response = await PostAsync("/login", new { email, password }, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadFromJsonAsync<LoginResponse>(JsonOptions, cancellationToken)
.ConfigureAwait(false);
if (body?.Token is not { Length: > 0 } t)
throw new InvalidOperationException("Login response did not contain a token.");
return t;
}
public Task<HttpResponseMessage> PostAsync<T>(string url, T body, CancellationToken cancellationToken = default)
{
var json = JsonSerializer.Serialize(body, JsonOptions);
var content = new StringContent(json, Encoding.UTF8, "application/json");
return _httpClient.PostAsync(url, content, cancellationToken);
}
public Task<HttpResponseMessage> GetAsync(string url, CancellationToken cancellationToken = default) =>
_httpClient.GetAsync(url, cancellationToken);
public Task<HttpResponseMessage> PutAsync(string url, CancellationToken cancellationToken = default) =>
_httpClient.PutAsync(url, null, cancellationToken);
public Task<HttpResponseMessage> PutAsync<T>(string url, T body, CancellationToken cancellationToken = default)
{
var json = JsonSerializer.Serialize(body, JsonOptions);
var content = new StringContent(json, Encoding.UTF8, "application/json");
return _httpClient.PutAsync(url, content, cancellationToken);
}
public Task<HttpResponseMessage> PatchAsync<T>(string url, T body, CancellationToken cancellationToken = default)
{
var json = JsonSerializer.Serialize(body, JsonOptions);
var content = new StringContent(json, Encoding.UTF8, "application/json");
return _httpClient.PatchAsync(url, content, cancellationToken);
}
public Task<HttpResponseMessage> DeleteAsync(string url, CancellationToken cancellationToken = default) =>
_httpClient.DeleteAsync(url, cancellationToken);
public Task<HttpResponseMessage> UploadFileAsync(string url, byte[] fileContent, string fileName,
string formFieldName = "data", CancellationToken cancellationToken = default)
{
var content = new MultipartFormDataContent();
var fileContentBytes = new ByteArrayContent(fileContent);
fileContentBytes.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
content.Add(fileContentBytes, formFieldName, fileName);
return _httpClient.PostAsync(url, content, cancellationToken);
}
private sealed class LoginResponse
{
public string Token { get; init; } = "";
}
}