feat: 初始功能
This commit is contained in:
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