首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将所有事务模板从一个SendGrid帐户迁移到另一个帐户?

如何将所有事务模板从一个SendGrid帐户迁移到另一个帐户?
EN

Stack Overflow用户
提问于 2018-06-08 04:39:19
回答 1查看 969关注 0票数 3

给定一个庞大的SendGrid模板库,如何一次点击将它们移动到不同的SendGrid帐户,比如在测试环境中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-08 04:39:19

似乎不可能开箱即用。下面的C#代码将通过该接口完成这项工作。

代码使用以下代码块: NewtonSoft Json.NET,SendGrid应用编程接口客户端。

一个重要的警告:模板ID在迁移后会有所不同。似乎不是一种保存它们的方法。

代码语言:javascript
复制
    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账户上工作,你的代码不应该依赖于模板is。相反,您可以构建一个查找字典,并通过它们的友好名称来引用模板,如下所示:

代码语言:javascript
复制
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”

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50749841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档