示例#1
0
        public void RetryMessage(string messageId)
        {
            var url     = string.Format(RetryEndpoint, messageId);
            var request = new RestRequestWithCache(url, Method.POST);

            Execute(request, _ => true);
        }
示例#2
0
        public IEnumerable <Endpoint> GetEndpoints()
        {
            var request  = new RestRequestWithCache(EndpointsEndpoint, RestRequestWithCache.CacheStyle.IfNotModified);
            var messages = GetModel <List <Endpoint> >(request);

            return(messages ?? new List <Endpoint>());
        }
示例#3
0
        public IEnumerable <StoredMessage> GetConversationById(string conversationId)
        {
            var request  = new RestRequestWithCache(String.Format(ConversationEndpoint, conversationId), RestRequestWithCache.CacheStyle.IfNotModified);
            var messages = GetModel <List <StoredMessage> >(request) ?? new List <StoredMessage>();

            return(messages);
        }
示例#4
0
        PagedResult <T> GetPagedResult <T>(RestRequestWithCache request) where T : class, new()
        {
            var result = Execute <PagedResult <T>, List <T> >(request, response => new PagedResult <T>
            {
                Result     = response.Data,
                TotalCount = int.Parse(response.Headers.First(x => x.Name == ServiceControlHeaders.TotalCount).Value.ToString())
            });

            return(result);
        }
示例#5
0
        public void LoadBody(StoredMessage message)
        {
            var request = new RestRequestWithCache(message.BodyUrl, message.Status == MessageStatus.Successful ? RestRequestWithCache.CacheStyle.Immutable : RestRequestWithCache.CacheStyle.IfNotModified);

            var baseUrl = message.BodyUrl;

            if (!baseUrl.StartsWith("http"))
            {
                baseUrl = null; // We use the default
            }
            message.Body = Execute(request, response => CleanupBodyString(response.Content), baseUrl);
        }
示例#6
0
        public string GetVersion()
        {
            var request = new RestRequestWithCache(RestRequestWithCache.CacheStyle.Immutable);

            var header = Execute(request, restResponse => restResponse.Headers.Single(x => x.Name == ServiceControlHeaders.ParticularVersion));

            if (header == null)
            {
                return(null);
            }

            return(header.Value.ToString());
        }
示例#7
0
        void LogRequest(RestRequestWithCache request)
        {
            var resource = request.Resource != null?request.Resource.TrimStart('/') : string.Empty;

            var url = connection.Url != null?connection.Url.TrimEnd('/') : string.Empty;

            LogTo.Information("HTTP {Method} {url:l}/{resource:l} ({CacheSyle})", request.Method, url, resource, request.CacheSyle);

            foreach (var parameter in request.Parameters)
            {
                LogTo.Debug("Request Parameter: {Name} : {Value}",
                            parameter.Name,
                            parameter.Value);
            }
        }
示例#8
0
        public IEnumerable <KeyValuePair <string, string> > GetMessageData(SagaMessage message)
        {
            var request = new RestRequestWithCache(String.Format(MessageBodyEndpoint, message.MessageId), message.Status == MessageStatus.Successful ? RestRequestWithCache.CacheStyle.Immutable : RestRequestWithCache.CacheStyle.IfNotModified);

            var body = Execute(request, response => response.Content);

            if (body == null)
            {
                return(Enumerable.Empty <KeyValuePair <string, string> >());
            }

            return(body.StartsWith("<?xml") ?
                   GetXmlData(body) :
                   JsonPropertiesHelper.ProcessValues(body, CleanupBodyString));
        }
示例#9
0
        T Execute <T, T2>(RestRequestWithCache request, Func <IRestResponse <T2>, T> selector)
            where T : class, new()
            where T2 : class, new()
        {
            var cacheStyle = request.CacheSyle;
            var restClient = CreateClient();

            switch (cacheStyle)
            {
            case RestRequestWithCache.CacheStyle.None:
                break;

            case RestRequestWithCache.CacheStyle.Immutable:
                var item = cache.Get(CacheKey(restClient, request));

                if (item != null)
                {
                    return((T)item);
                }

                break;

            case RestRequestWithCache.CacheStyle.IfNotModified:
                var obj = cache.Get(CacheKey(restClient, request));

                if (obj != null)
                {
                    var tuple = (Tuple <string, T>)obj;
                    request.AddHeader("If-None-Match", tuple.Item1);
                }

                break;
            }

            LogRequest(request);

            var response = restClient.Execute <T2>(request);

            var data = default(T);

            switch (cacheStyle)
            {
            case RestRequestWithCache.CacheStyle.Immutable:
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    data = ProcessResponse(selector, response);
                    cache.Set(CacheKey(restClient, request), data, new CacheItemPolicy());
                }
                break;

            case RestRequestWithCache.CacheStyle.IfNotModified:
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    data = ProcessResponse(selector, response);
                    var etag = response.Headers.FirstOrDefault(h => h.Name == "ETag");
                    if (etag != null)
                    {
                        cache.Set(CacheKey(restClient, request), Tuple.Create(etag.Value.ToString(), data), new CacheItemPolicy());
                    }
                    break;

                case HttpStatusCode.NotModified:
                    LogResponse(response);

                    var obj = cache.Get(CacheKey(restClient, request));

                    if (obj != null)
                    {
                        var tuple = (Tuple <string, T>)obj;
                        data = tuple.Item2;
                    }
                    break;
                }
                break;

            default:
                data = ProcessResponse(selector, response);
                break;
            }

            return(data);
        }
示例#10
0
 T GetModel <T>(RestRequestWithCache request)
     where T : class, new()
 {
     return(Execute <T, T>(request, response => response.Data));
 }