Files
WorkFlowCheck/src/WorkFlowCheck.MAUI/Services/BaseService.cs
T
2025-04-07 07:57:10 +02:00

67 lines
2.3 KiB
C#

using AutoMapper;
using DevExpress.XtraPrinting.Native;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using WorkFlowCheck.MAUI.DataLayer;
using WorkFlowCheck.MAUI.Services.Interfaces;
using Android.Provider;
using Android.DeviceLock;
namespace WorkFlowCheck.MAUI.Services
{
public class BaseService
{
public readonly HttpClient _httpClient;
public readonly AppDbContext _dbContext;
public readonly IConfiguration _configuration;
public readonly IMapper _mapper;
private string _deviceId;
public string DeviceId => _deviceId.ToUpper(); // Getter a DeviceId-hez
public BaseService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext, IMapper mapper)
{
_httpClient = httpClient;
_dbContext = dbContext;
_configuration = configuration;
_mapper = mapper;
var apiBaseUrl = configuration["ApiBaseUrl"]; // API-cím elérése a konfigurációból
_httpClient.BaseAddress = new Uri(apiBaseUrl);
var apiKey = configuration["ApiKey"];
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", apiKey);
_deviceId = GetDeviceId();
}
protected async Task PrepareAuthenticatedRequestAsync()
{
var jwtToken = await SecureStorage.GetAsync("AuthToken");
if (!string.IsNullOrEmpty(jwtToken) && _httpClient != null)
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
_httpClient.DefaultRequestHeaders.Add("DeviceId", _deviceId);
}
}
protected string GetDeviceId()
{
try
{
var context = Android.App.Application.Context;
return Settings.Secure.GetString(context.ContentResolver, Settings.Secure.AndroidId) ?? "NO_ANDROID_DEVICE";
}
catch (Exception ex)
{
Console.WriteLine($"Error retrieving device ID: {ex.Message}");
return "UnknownDevice";
}
}
}
}