加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何检查移动设备是否已注册

发布时间:2020-12-16 19:19:21 所属栏目:百科 来源:网络整理
导读:我正在使用适用于Amazon SNS的Amazon AWS Ruby SDK,但我在使用已经注册的设备时遇到了一些问题.有时当设备再次注册时,我收到类似AWS :: SNS :: Errors :: InvalidParameter的错误无效参数:令牌原因:端点arn:aws:sns:us-east-1:****已经存在且具有相同
我正在使用适用于Amazon SNS的Amazon AWS Ruby SDK,但我在使用已经注册的设备时遇到了一些问题.有时当设备再次注册时,我收到类似AWS :: SNS :: Errors :: InvalidParameter的错误无效参数:令牌原因:端点arn:aws:sns:us-east-1:****已经存在且具有相同的令牌,但不同的属性..如何检查端点是否已存在,更重要的是,如何获取给定令牌的端点?

解决方法

感谢BvdBijl的想法,我做了一个扩展方法,删除现有的一个,如果找到,然后添加它.
using System;
using System.Text.RegularExpressions;
using Amazon.SimpleNotificationService;
using Amazon.SimpleNotificationService.Model;

namespace Amazon.SimpleNotificationService
{
    public static class AmazonSimpleNotificationServiceClientExtensions
    {
        private const string existingEndpointRegexString = "Reason: Endpoint (.+) already exists with the same Token";
        private static Regex existingEndpointRegex = new Regex(existingEndpointRegexString);
        public static CreatePlatformEndpointResponse CreatePlatformEndpointIdempotent(
            this AmazonSimpleNotificationServiceClient client,CreatePlatformEndpointRequest request)
        {
            try
            {
                var result = client.CreatePlatformEndpoint(request);
                return result;
            }
            catch (AmazonSimpleNotificationServiceException e)
            {
                if (e.ErrorCode == "InvalidParameter")
                {
                    var match = existingEndpointRegex.Match(e.Message);
                    if (match.Success) {
                        string arn = match.Groups[1].Value;
                        client.DeleteEndpoint(new DeleteEndpointRequest
                        {
                             EndpointArn = arn,});
                        return client.CreatePlatformEndpoint(request);
                    }
                }
                throw;
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读