public HttpWebRequest GenerateRequest(ICommandRequest request, string userId, HttpMethod requestMethod)
        {
            Validator.ValidateObject(request, new ValidationContext(request, null, null), true);

            var webRequest = (HttpWebRequest) WebRequest.Create(request.Resource);
            webRequest.Method = requestMethod.ToString();
            webRequest.ContentType = "application/octet-stream";

            var requestType = request.GetType();
            var properties = requestType.GetProperties(PropertyFilters)
                .Where(
                    p =>
                    (p.GetValue(request, null) != null) &&
                    (!string.IsNullOrWhiteSpace(p.GetValue(request, null).ToString())))
                .Where(p => p.GetCustomAttributes(false).OfType<HeaderAttribute>().Where(h => h.Serialise).Any())
                .Select(p => new
                                 {
                                     Name = p.GetCustomAttributes(false).OfType<HeaderAttribute>().Select(h => h.Name).First(),
                                     Value = p.GetValue(request, null).ToString()
                                 }).ToList();

            properties.Add(new { Name = "x-emc-date", Value = DateTime.UtcNow.ToString("r") });
            properties.Add(new { Name = "x-emc-uid", Value = userId });

            foreach (var property in properties)
            {
                webRequest.Headers.Add(property.Name, property.Value);
            }

            return webRequest;
        }
Example #2
0
        private IEnumerable <CommandHandlerWrapper> GetWrappedHandlers(ICommandRequest command)
        {
            var handlerType = typeof(ICommandRequestHandler <>).MakeGenericType(command.GetType());
            var wrapperType = typeof(CommandHandlerWrapper <>).MakeGenericType(command.GetType());

            var handlers =
                (IEnumerable)_serviceProvider.GetRequiredService(typeof(IEnumerable <>).MakeGenericType(handlerType));

            var instance = handlers.Cast <object>()
                           .Select(_ => (CommandHandlerWrapper)Activator.CreateInstance(wrapperType));

            var wrappedHandlers = CommandHandlers
                                  .GetOrAdd(command.GetType(), instance);

            return(wrappedHandlers);
        }
        public IRestRequest GenerateRequest(ICommandRequest request, string apiKey)
        {
            Validator.ValidateObject(request, new ValidationContext(request, null, null), true);

            var queryString = new StringBuilder();
            var requestType = request.GetType();

            var properties = requestType.GetProperties(PropertyFilters)
                .Where(p => (p.GetValue(request, null) != null) && (!string.IsNullOrWhiteSpace(p.GetValue(request, null).ToString())))
                .Select(p =>
                    new KeyValuePair<string, string>(p.Name.ToLowerInvariant(), p.GetValue(request, null).ToString()))
                .ToList();
            properties.Add(new KeyValuePair<string, string>("apikey", apiKey));

            var orderedProperties = properties.OrderBy(p => p.Key);

            foreach (var property in orderedProperties)
            {
                queryString.Append(string.Format("{0}={1}&", property.Key, property.Value));
            }
            queryString.Remove(queryString.Length - 1, 1);
            var escapedQueryString = HttpUtility.UrlEncode(queryString.ToString());

            return new RestRequest("?" + escapedQueryString, Method.POST);
        }
Example #4
0
        private CommandHandlerWrapperResponse <TResponse> GetWrappedHandlers <TResponse>(ICommandRequest <TResponse> command)
        {
            var handlerType = typeof(ICommandRequestHandler <,>).MakeGenericType(command.GetType(), typeof(TResponse));
            var wrapperType = typeof(CommandHandlerWrapperResponse <,>).MakeGenericType(command.GetType(), typeof(TResponse));

            var handlers =
                (IEnumerable)_serviceProvider.GetRequiredService(typeof(IEnumerable <>).MakeGenericType(handlerType));

            var wrappedHandlers = (CommandHandlerWrapperResponse <TResponse>)CommandHandlersResponse.GetOrAdd(command.GetType(), handlers.Cast <object>()
                                                                                                              .Select(_ => (CommandHandlerWrapperResponse <TResponse>)Activator.CreateInstance(wrapperType)).FirstOrDefault());

            return(wrappedHandlers);
        }