public RestClient(RestClientContext context)
 {
     _type    = typeof(T);
     _context = context;
     HandleAttributes();
     _context.DefaultSerializer = _context.SerializationManager.GetSerializer(_context.DefaultContentTypes);
 }
Exemple #2
0
        public RequestHandler(MethodInfo method, RestClientContext context)
        {
            _returnType        = method.ReturnType;
            _actualReturnType  = GetActualReturnType();
            _parameters        = method.GetParameters();
            _paramConverters   = GetParamConverters(context.ParamConverterManager);
            _converter         = CreateConverter(context);
            _template          = context.BaseTemplate ?? "";
            _serializer        = context.DefaultSerializer;
            _contentTypes      = context.DefaultContentTypes;
            _errorResponseType = context.ErrorResponseType;

            _bodyArgumentIndex = _parameters.Length - 1;

            var templateBuilder = new StringBuilder(context.BaseTemplate ?? "");

            foreach (var attribute in method.GetCustomAttributes(true))
            {
                switch (attribute)
                {
                case HttpMethodAttribute httpMethodAttribute:
                    templateBuilder.Append(httpMethodAttribute.Template);
                    _httpMethod = GetHttpMethod(httpMethodAttribute);
                    break;

                case ErrorResponseAttribute errorResponseAttribute:
                    _errorResponseType = errorResponseAttribute.Type;
                    break;

                case ConsumesAttribute consumesAttribute:
                    _contentTypes = consumesAttribute.ContentTypes;
                    _serializer   = context.SerializationManager.GetSerializer(_contentTypes) ??
                                    throw new InvalidOperationException(
                                              "No serializer for: " + string.Join(",", _contentTypes));
                    break;

                case RouteAttribute routeAttribute:
                    templateBuilder.Append(routeAttribute.Template);
                    break;
                }
            }

            if (context.UseDomainException)
            {
                _domainExceptionMapper = context.DomainExceptionMapperFactory.CreateDomainExceptionMapper(
                    method,
                    _errorResponseType,
                    context.DomainExceptionType);
            }

            _template = templateBuilder.ToString();
            _context  = context;
        }
        public RestClientBuilder(
            IDuckTyping duckTyping,
            IParamConverterManager paramConverterManager,
            ITaskConverterFactory taskConverterFactory)
        {
            _duckTyping = duckTyping ?? throw new ArgumentNullException(nameof(duckTyping));

            _context = new RestClientContext
            {
                TaskConverterFactory = taskConverterFactory ??
                                       throw new ArgumentNullException(nameof(taskConverterFactory)),
                                             ParamConverterManager = paramConverterManager ??
                                                                     throw new ArgumentNullException(nameof(paramConverterManager))
            };
        }
Exemple #4
0
 private ITaskConverter CreateConverter(RestClientContext context)
 {
     return(context.TaskConverterFactory.CreateTaskConverter(_actualReturnType));
 }