feat: 初始功能
This commit is contained in:
55
Services/Grpc/CloudPrintGrpcMapper.cs
Normal file
55
Services/Grpc/CloudPrintGrpcMapper.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using CloudPrint.Client.Models;
|
||||
|
||||
namespace CloudPrint.Client.Services.Grpc;
|
||||
|
||||
public static class CloudPrintGrpcMapper
|
||||
{
|
||||
public static PrintJob ToPrintJob(Cloudprint.PrintJob backendJob)
|
||||
{
|
||||
var orderItem = backendJob.OrderItem;
|
||||
var file = orderItem?.File;
|
||||
|
||||
return new PrintJob
|
||||
{
|
||||
Id = backendJob.Id,
|
||||
JobNo = backendJob.JobNo,
|
||||
PrinterId = backendJob.PrinterId,
|
||||
OrderItemId = backendJob.OrderItemId,
|
||||
FileName = file?.FileName ?? backendJob.JobNo,
|
||||
FilePath = file?.FilePath ?? string.Empty,
|
||||
FileId = orderItem?.FileId ?? 0,
|
||||
Copies = Math.Max(1, orderItem?.Copies ?? 1),
|
||||
Color = orderItem?.Color ?? false,
|
||||
Duplex = orderItem?.Duplex ?? false,
|
||||
PageRange = string.IsNullOrWhiteSpace(orderItem?.PageRange) ? "all" : orderItem.PageRange,
|
||||
Status = FromBackendStatus(backendJob.Status),
|
||||
Progress = backendJob.Progress,
|
||||
ErrorMessage = backendJob.ErrorMsg
|
||||
};
|
||||
}
|
||||
|
||||
public static string ToBackendStatus(PrintJobStatus status)
|
||||
{
|
||||
return status switch
|
||||
{
|
||||
PrintJobStatus.Pending => "pending",
|
||||
PrintJobStatus.Printing => "printing",
|
||||
PrintJobStatus.Completed => "completed",
|
||||
PrintJobStatus.Failed => "failed",
|
||||
PrintJobStatus.Cancelled => "cancelled",
|
||||
_ => "pending"
|
||||
};
|
||||
}
|
||||
|
||||
private static PrintJobStatus FromBackendStatus(string status)
|
||||
{
|
||||
return status?.ToLowerInvariant() switch
|
||||
{
|
||||
"printing" => PrintJobStatus.Printing,
|
||||
"completed" => PrintJobStatus.Completed,
|
||||
"failed" => PrintJobStatus.Failed,
|
||||
"cancelled" => PrintJobStatus.Cancelled,
|
||||
_ => PrintJobStatus.Pending
|
||||
};
|
||||
}
|
||||
}
|
||||
30
Services/Grpc/GrpcMetadataFactory.cs
Normal file
30
Services/Grpc/GrpcMetadataFactory.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using CloudPrint.Client.Models;
|
||||
using Grpc.Core;
|
||||
|
||||
namespace CloudPrint.Client.Services.Grpc;
|
||||
|
||||
public static class GrpcMetadataFactory
|
||||
{
|
||||
public static Metadata Create(string apiKey, string printerToken, AdminSession adminSession)
|
||||
{
|
||||
var metadata = new Metadata();
|
||||
|
||||
AddIfNotEmpty(metadata, "x-api-key", apiKey);
|
||||
AddIfNotEmpty(metadata, "x-printer-token", printerToken);
|
||||
|
||||
if (adminSession.IsAuthenticated)
|
||||
{
|
||||
metadata.Add("authorization", $"Bearer {adminSession.AccessToken}");
|
||||
}
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
private static void AddIfNotEmpty(Metadata metadata, string key, string value)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
metadata.Add(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user