Files
AdPlatform-Server/MetaApi/Models/ProviderModels.cs
2026-03-14 13:50:09 -07:00

97 lines
2.9 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
namespace MetaApi.Models;
/// <summary>
/// Request from Gateway to MetaApi.
/// Identical contract to GoogleApi.Models.ProviderRequest for Gateway compatibility.
/// </summary>
public sealed class ProviderRequest
{
/// <summary>
/// Operation to execute (e.g., "Ping", "CreateCampaign", "GetCampaignInsights")
/// </summary>
public string Operation { get; set; } = string.Empty;
/// <summary>
/// Tenant/account ID - maps to Meta ad account ID (act_XXXXXXX).
/// Populated by Gateway from tbAdAccount.accExternalAccountId where accNetwork='meta'.
/// </summary>
public string? TenantId { get; set; }
/// <summary>
/// Login customer ID - maps to Meta Business Manager ID.
/// In Meta's agency model, the BM owns and manages client ad accounts.
/// Populated by Gateway from tbAdAccount.accLoginAccountId.
/// </summary>
public string? LoginCustomerId { get; set; }
/// <summary>
/// Correlation ID for request tracing
/// </summary>
public string? RequestId { get; set; }
/// <summary>
/// Operation-specific payload
/// </summary>
public JsonElement? Payload { get; set; }
/// <summary>
/// Deserialize payload to strongly-typed object
/// </summary>
public T GetPayload<T>() where T : new()
{
if (Payload == null || Payload.Value.ValueKind == JsonValueKind.Null || Payload.Value.ValueKind == JsonValueKind.Undefined)
return new T();
try
{
return JsonSerializer.Deserialize<T>(Payload.Value.GetRawText(), JsonOptions.Default) ?? new T();
}
catch
{
return new T();
}
}
}
/// <summary>
/// Response from MetaApi to Gateway.
/// Identical contract to GoogleApi.Models.ProviderResponse.
/// </summary>
public sealed class ProviderResponse
{
public bool Ok { get; set; }
public string? RequestId { get; set; }
public object? Data { get; set; }
public ProviderError? Error { get; set; }
public static ProviderResponse Success(string? requestId, object? data = null)
=> new() { Ok = true, RequestId = requestId, Data = data };
public static ProviderResponse Fail(string? requestId, string code, string message, object? detail = null)
=> new()
{
Ok = false,
RequestId = requestId,
Error = new ProviderError { Code = code, Message = message, Detail = detail }
};
}
public sealed class ProviderError
{
public string Code { get; set; } = "ERROR";
public string Message { get; set; } = "Unknown error";
public object? Detail { get; set; }
}
internal static class JsonOptions
{
public static readonly JsonSerializerOptions Default = new(JsonSerializerDefaults.Web)
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
}