using System.Net.Http.Json; using RobotNet10.FleetManager.Shared.DTOs.RobotModel; using RobotNet10.FleetManager.Shared.DTOs.Responses; namespace RobotNet10.FleetManager.Client.Services; /// /// API service for robot model operations. /// Provides methods to interact with the robot model API endpoints. /// public class RobotModelApiService { private readonly HttpClient _httpClient; private readonly ILogger? _logger; public RobotModelApiService(HttpClient httpClient, ILogger? logger = null) { _httpClient = httpClient; _logger = logger; } /// /// Get all robot models /// public async Task> GetAllAsync() { try { var response = await _httpClient.GetAsync("/api/robot-models"); response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync>() ?? new List(); } catch (Exception ex) { _logger?.LogError(ex, "Error getting all robot models"); throw; } } /// /// Get robot model by ID /// public async Task GetByIdAsync(Guid id) { try { var response = await _httpClient.GetAsync($"/api/robot-models/{id}"); if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { return null; } response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync(); } catch (Exception ex) { _logger?.LogError(ex, "Error getting robot model {Id}", id); throw; } } /// /// Search robot models /// public async Task> SearchAsync(string query) { try { var response = await _httpClient.GetAsync($"/api/robot-models/search?query={Uri.EscapeDataString(query)}"); response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync>() ?? new List(); } catch (Exception ex) { _logger?.LogError(ex, "Error searching robot models with query {Query}", query); throw; } } /// /// Get usage information for a robot model /// public async Task GetUsageInfoAsync(Guid id) { try { var response = await _httpClient.GetAsync($"/api/robot-models/{id}/usage"); response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync() ?? throw new InvalidOperationException("Failed to deserialize usage info"); } catch (Exception ex) { _logger?.LogError(ex, "Error getting usage info for robot model {Id}", id); throw; } } /// /// Create a new robot model /// public async Task CreateAsync(CreateRobotModelRequest request) { try { var response = await _httpClient.PostAsJsonAsync("/api/robot-models", request); if (!response.IsSuccessStatusCode) { var errorContent = await response.Content.ReadAsStringAsync(); _logger?.LogError("Error creating robot model. Status: {StatusCode}, Response: {Error}", response.StatusCode, errorContent); if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { throw new InvalidOperationException($"Validation error: {errorContent}"); } response.EnsureSuccessStatusCode(); } return await response.Content.ReadFromJsonAsync() ?? throw new InvalidOperationException("Failed to deserialize created robot model"); } catch (HttpRequestException ex) { _logger?.LogError(ex, "HTTP error creating robot model"); throw; } catch (Exception ex) { _logger?.LogError(ex, "Error creating robot model"); throw; } } /// /// Update an existing robot model /// public async Task UpdateAsync(Guid id, UpdateRobotModelRequest request) { try { var response = await _httpClient.PutAsJsonAsync($"/api/robot-models/{id}", request); response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync() ?? throw new InvalidOperationException("Failed to deserialize updated robot model"); } catch (Exception ex) { _logger?.LogError(ex, "Error updating robot model {Id}", id); throw; } } /// /// Delete a robot model /// public async Task DeleteAsync(Guid id) { try { var response = await _httpClient.DeleteAsync($"/api/robot-models/{id}"); response.EnsureSuccessStatusCode(); } catch (Exception ex) { _logger?.LogError(ex, "Error deleting robot model {Id}", id); throw; } } /// /// Get robot model image /// public async Task GetImageAsync(Guid id) { try { var response = await _httpClient.GetAsync($"/api/robot-models/{id}/image"); if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { return null; } response.EnsureSuccessStatusCode(); var imageBytes = await response.Content.ReadAsByteArrayAsync(); return Convert.ToBase64String(imageBytes); } catch (Exception ex) { _logger?.LogError(ex, "Error getting image for robot model {Id}", id); throw; } } /// /// Upload robot model image /// public async Task UploadImageAsync(Guid id, Stream imageStream, string fileName) { try { using var content = new MultipartFormDataContent(); using var streamContent = new StreamContent(imageStream); streamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png"); content.Add(streamContent, "file", fileName); var response = await _httpClient.PostAsync($"/api/robot-models/{id}/image", content); if (!response.IsSuccessStatusCode) { var errorContent = await response.Content.ReadAsStringAsync(); _logger?.LogError("Error uploading image. Status: {StatusCode}, Response: {Error}", response.StatusCode, errorContent); if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { throw new InvalidOperationException($"Image upload validation error: {errorContent}"); } response.EnsureSuccessStatusCode(); } } catch (HttpRequestException ex) { _logger?.LogError(ex, "HTTP error uploading image for robot model {Id}", id); throw; } catch (Exception ex) { _logger?.LogError(ex, "Error uploading image for robot model {Id}", id); throw; } } /// /// Delete robot model image /// public async Task DeleteImageAsync(Guid id) { try { var response = await _httpClient.DeleteAsync($"/api/robot-models/{id}/image"); response.EnsureSuccessStatusCode(); } catch (Exception ex) { _logger?.LogError(ex, "Error deleting image for robot model {Id}", id); throw; } } }