如何将所有交易模板从一个 SendGrid 帐户迁移到另一个帐户?
How does one migrate all transactional templates from one SendGrid account to another?
给定大量 SendGrid 模板库,如何一键将它们移动到不同的 SendGrid 帐户,比如用于测试环境?
似乎不可能开箱即用。以下 C# 代码将通过 API.
完成工作
代码使用以下块:NewtonSoft Json.NET、SendGrid API 客户端。
一个重要警告:迁移后模板 ID 会有所不同。似乎不是保存它们的方法。
public ActionResult MigrateSendGridTemplates() {
//https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/templates.html
//https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/versions.html
var fromClient = new SendGridClient("full access api key"); //full access key
var toClient = new SendGridClient("full access api key"); //full access key - assume blank slate
//fetch all existing templates
var templatesRaw = fromClient.RequestAsync(SendGridClient.Method.GET, null, null, "templates").Result;
var templates = templatesRaw.DeserializeResponseBody(templatesRaw.Body);
var templatesEnumerable = ((IEnumerable)templates.First().Value).Cast<dynamic>().Reverse();
foreach (var template in templatesEnumerable)
{
//fetch template with versions attached
var templateWithVerisonRaw = fromClient.RequestAsync(SendGridClient.Method.GET, null, null, $"templates/{template.id}").Result;
var templateWithVersion = templateWithVerisonRaw.DeserializeResponseBody(templateWithVerisonRaw.Body);
//create template on the new account
var templateNewRaw = toClient.RequestAsync(SendGridClient.Method.POST, templateWithVerisonRaw.Body.ReadAsStringAsync().Result, null, "templates").Result;
var activeVersion = ((IEnumerable)templateWithVersion["versions"]).Cast<dynamic>().Where(v => v.active).SingleOrDefault();
if (activeVersion == null)
continue; //this template does not have any versions to migrate
//create template version on new account
var templateNewId = templateNewRaw.DeserializeResponseBody(templateNewRaw.Body)["id"];
var templateSerialized = JsonConvert.SerializeObject(activeVersion, Formatting.None);
var templateVersionNewRaw = toClient.RequestAsync(SendGridClient.Method.POST, templateSerialized, null, $"templates/{templateNewId}/versions").Result;
}
return Content($"Processed {templatesEnumerable.Count()} templates.");
}
由此得出的结论是,如果您希望您的代码适用于不同的 sendgrid 帐户,则您的代码不应依赖于模板 ID。相反,您可以构建一个查找字典并通过友好名称引用您的模板,如下所示:
public static Dictionary<string, string> GetSendGridTemplates()
{
var templatesRaw = Persistent.ConfiguredSendGridClient.RequestAsync(SendGridClient.Method.GET, null, null, "templates").Result;
var templates = templatesRaw.DeserializeResponseBody(templatesRaw.Body);
var templatesEnumerable = ((IEnumerable)templates.First().Value).Cast<dynamic>();
var results = new Dictionary<string, string>();
foreach (dynamic template in templatesEnumerable)
{
var activeVersion = ((IEnumerable)template.versions).Cast<dynamic>().Where(v => v.active).SingleOrDefault();
if (activeVersion == null)
continue; //skip this one
results.Add((string)activeVersion.name, (string)template.id);
}
return results;
}
示例结果:
reactivated_121519737023655 -> “03e2b62f-51ed-43d0-b140-42bc98a448f6”
deactivated_11519736715430 -> "fb3e781b-5e67-45de-a958-bf0cd2682004"
给定大量 SendGrid 模板库,如何一键将它们移动到不同的 SendGrid 帐户,比如用于测试环境?
似乎不可能开箱即用。以下 C# 代码将通过 API.
完成工作代码使用以下块:NewtonSoft Json.NET、SendGrid API 客户端。
一个重要警告:迁移后模板 ID 会有所不同。似乎不是保存它们的方法。
public ActionResult MigrateSendGridTemplates() {
//https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/templates.html
//https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/versions.html
var fromClient = new SendGridClient("full access api key"); //full access key
var toClient = new SendGridClient("full access api key"); //full access key - assume blank slate
//fetch all existing templates
var templatesRaw = fromClient.RequestAsync(SendGridClient.Method.GET, null, null, "templates").Result;
var templates = templatesRaw.DeserializeResponseBody(templatesRaw.Body);
var templatesEnumerable = ((IEnumerable)templates.First().Value).Cast<dynamic>().Reverse();
foreach (var template in templatesEnumerable)
{
//fetch template with versions attached
var templateWithVerisonRaw = fromClient.RequestAsync(SendGridClient.Method.GET, null, null, $"templates/{template.id}").Result;
var templateWithVersion = templateWithVerisonRaw.DeserializeResponseBody(templateWithVerisonRaw.Body);
//create template on the new account
var templateNewRaw = toClient.RequestAsync(SendGridClient.Method.POST, templateWithVerisonRaw.Body.ReadAsStringAsync().Result, null, "templates").Result;
var activeVersion = ((IEnumerable)templateWithVersion["versions"]).Cast<dynamic>().Where(v => v.active).SingleOrDefault();
if (activeVersion == null)
continue; //this template does not have any versions to migrate
//create template version on new account
var templateNewId = templateNewRaw.DeserializeResponseBody(templateNewRaw.Body)["id"];
var templateSerialized = JsonConvert.SerializeObject(activeVersion, Formatting.None);
var templateVersionNewRaw = toClient.RequestAsync(SendGridClient.Method.POST, templateSerialized, null, $"templates/{templateNewId}/versions").Result;
}
return Content($"Processed {templatesEnumerable.Count()} templates.");
}
由此得出的结论是,如果您希望您的代码适用于不同的 sendgrid 帐户,则您的代码不应依赖于模板 ID。相反,您可以构建一个查找字典并通过友好名称引用您的模板,如下所示:
public static Dictionary<string, string> GetSendGridTemplates()
{
var templatesRaw = Persistent.ConfiguredSendGridClient.RequestAsync(SendGridClient.Method.GET, null, null, "templates").Result;
var templates = templatesRaw.DeserializeResponseBody(templatesRaw.Body);
var templatesEnumerable = ((IEnumerable)templates.First().Value).Cast<dynamic>();
var results = new Dictionary<string, string>();
foreach (dynamic template in templatesEnumerable)
{
var activeVersion = ((IEnumerable)template.versions).Cast<dynamic>().Where(v => v.active).SingleOrDefault();
if (activeVersion == null)
continue; //skip this one
results.Add((string)activeVersion.name, (string)template.id);
}
return results;
}
示例结果:
reactivated_121519737023655 -> “03e2b62f-51ed-43d0-b140-42bc98a448f6” deactivated_11519736715430 -> "fb3e781b-5e67-45de-a958-bf0cd2682004"