public RestMethodInfo(Type targetInterface, MethodInfo methodInfo)
        {
            Type       = targetInterface;
            Name       = methodInfo.Name;
            MethodInfo = methodInfo;

            var hma = methodInfo.GetCustomAttributes(true)
                      .OfType <HttpMethodAttribute>()
                      .First();

            HttpMethod   = hma.Method;
            RelativePath = hma.Path;

            verifyUrlPathIsSane(RelativePath);
            determineReturnTypeInfo(methodInfo);

            var parameterList = methodInfo.GetParameters().ToList();

            ParameterMap      = buildParameterMap(RelativePath, parameterList);
            BodyParameterInfo = findBodyParameter(parameterList);

            Headers            = parseHeaders(methodInfo);
            HeaderParameterMap = buildHeaderParameterMap(parameterList);

            QueryParameterMap = new Dictionary <int, string>();
            for (int i = 0; i < parameterList.Count; i++)
            {
                if (ParameterMap.ContainsKey(i) || HeaderParameterMap.ContainsKey(i) || (BodyParameterInfo != null && BodyParameterInfo.Item2 == i))
                {
                    continue;
                }

                QueryParameterMap[i] = getUrlNameForParameter(parameterList[i]);
            }
        }
        public RestMethodInfo(Type targetInterface, MethodInfo methodInfo, RefitSettings refitSettings = null)
        {
            RefitSettings = refitSettings ?? new RefitSettings();
            Type          = targetInterface;
            Name          = methodInfo.Name;
            MethodInfo    = methodInfo;

            var hma = methodInfo.GetCustomAttributes(true)
                      .OfType <HttpMethodAttribute>()
                      .First();

            HttpMethod   = hma.Method;
            RelativePath = hma.Path;

            IsMultipart = methodInfo.GetCustomAttributes(true)
                          .OfType <MultipartAttribute>()
                          .Any();

            verifyUrlPathIsSane(RelativePath);
            determineReturnTypeInfo(methodInfo);

            // Exclude cancellation token parameters from this list
            var parameterList = methodInfo.GetParameters().Where(p => p.ParameterType != typeof(CancellationToken)).ToList();

            ParameterInfoMap = parameterList
                               .Select((parameter, index) => new { index, parameter })
                               .ToDictionary(x => x.index, x => x.parameter);
            ParameterMap      = buildParameterMap(RelativePath, parameterList);
            BodyParameterInfo = findBodyParameter(parameterList, IsMultipart, hma.Method);

            Headers            = parseHeaders(methodInfo);
            HeaderParameterMap = buildHeaderParameterMap(parameterList);

            // get names for multipart attachments
            AttachmentNameMap = new Dictionary <int, Tuple <string, string> >();
            if (IsMultipart)
            {
                for (int i = 0; i < parameterList.Count; i++)
                {
                    if (ParameterMap.ContainsKey(i) || HeaderParameterMap.ContainsKey(i))
                    {
                        continue;
                    }

                    var attachmentName = getAttachmentNameForParameter(parameterList[i]);
                    if (attachmentName == null)
                    {
                        continue;
                    }

                    AttachmentNameMap[i] = Tuple.Create(attachmentName, getUrlNameForParameter(parameterList[i]).ToLowerInvariant());
                }
            }

            QueryParameterMap = new Dictionary <int, string>();
            for (int i = 0; i < parameterList.Count; i++)
            {
                if (ParameterMap.ContainsKey(i) || HeaderParameterMap.ContainsKey(i) || (BodyParameterInfo != null && BodyParameterInfo.Item2 == i) || AttachmentNameMap.ContainsKey(i))
                {
                    continue;
                }

                QueryParameterMap[i] = getUrlNameForParameter(parameterList[i]);
            }

            var ctParams = methodInfo.GetParameters().Where(p => p.ParameterType == typeof(CancellationToken)).ToList();

            if (ctParams.Count > 1)
            {
                throw new ArgumentException("Argument list can only contain a single CancellationToken");
            }

            CancellationToken = ctParams.FirstOrDefault();
        }
Esempio n. 3
0
        public RestMethodInfo(Type targetInterface, MethodInfo methodInfo, RefitSettings refitSettings = null)
        {
            RefitSettings = refitSettings ?? new RefitSettings();
            Type          = targetInterface;
            Name          = methodInfo.Name;
            MethodInfo    = methodInfo;

            var hma = methodInfo.GetCustomAttributes(true)
                      .OfType <HttpMethodAttribute>()
                      .First();

            HttpMethod   = hma.Method;
            RelativePath = hma.Path;

            IsMultipart = methodInfo.GetCustomAttributes(true)
                          .OfType <MultipartAttribute>()
                          .Any();

            MultipartBoundary = IsMultipart ? methodInfo.GetCustomAttribute <MultipartAttribute>(true).BoundaryText : string.Empty;

            VerifyUrlPathIsSane(RelativePath);
            DetermineReturnTypeInfo(methodInfo);

            // Exclude cancellation token parameters from this list
            var parameterList = methodInfo.GetParameters().Where(p => p.ParameterType != typeof(CancellationToken)).ToList();

            ParameterInfoMap = parameterList
                               .Select((parameter, index) => new { index, parameter })
                               .ToDictionary(x => x.index, x => x.parameter);
            ParameterMap      = BuildParameterMap(RelativePath, parameterList);
            BodyParameterInfo = FindBodyParameter(parameterList, IsMultipart, hma.Method);

            Headers            = ParseHeaders(methodInfo);
            HeaderParameterMap = BuildHeaderParameterMap(parameterList);

            // get names for multipart attachments
            AttachmentNameMap = new Dictionary <int, Tuple <string, string> >();
            if (IsMultipart)
            {
                for (var i = 0; i < parameterList.Count; i++)
                {
                    if (ParameterMap.ContainsKey(i) || HeaderParameterMap.ContainsKey(i))
                    {
                        continue;
                    }

                    var attachmentName = GetAttachmentNameForParameter(parameterList[i]);
                    if (attachmentName == null)
                    {
                        continue;
                    }

                    AttachmentNameMap[i] = Tuple.Create(attachmentName, GetUrlNameForParameter(parameterList[i]).ToLowerInvariant());
                }
            }

            QueryParameterMap = new Dictionary <int, string>();
            for (var i = 0; i < parameterList.Count; i++)
            {
                if (ParameterMap.ContainsKey(i) ||
                    HeaderParameterMap.ContainsKey(i) ||
                    (BodyParameterInfo != null && BodyParameterInfo.Item3 == i))
                {
                    continue;
                }

                if (parameterList[i].GetCustomAttribute <QueryAttribute>() != null)
                {
                    var typeInfo    = parameterList[i].ParameterType.GetTypeInfo();
                    var isValueType = typeInfo.IsValueType && !typeInfo.IsPrimitive && !typeInfo.IsEnum;
                    if (typeInfo.IsArray || parameterList[i].ParameterType.GetInterfaces().Contains(typeof(IEnumerable)) || isValueType)
                    {
                        QueryParameterMap.Add(QueryParameterMap.Count, GetUrlNameForParameter(parameterList[i]));
                    }
                    else
                    {
                        foreach (var member in parameterList[i].ParameterType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
                        {
                            QueryParameterMap.Add(QueryParameterMap.Count, GetUrlNameForMember(member));
                        }
                    }
                    continue;
                }

                QueryParameterMap.Add(QueryParameterMap.Count, GetUrlNameForParameter(parameterList[i]));
            }

            var ctParams = methodInfo.GetParameters().Where(p => p.ParameterType == typeof(CancellationToken)).ToList();

            if (ctParams.Count > 1)
            {
                throw new ArgumentException($"Argument list to method \"{methodInfo.Name}\" can only contain a single CancellationToken");
            }

            CancellationToken = ctParams.FirstOrDefault();

            IsApiResponse = SerializedReturnType.GetTypeInfo().IsGenericType&&
                            SerializedReturnType.GetGenericTypeDefinition() == typeof(ApiResponse <>);
        }
Esempio n. 4
0
        public RestMethodInfo(Type targetInterface, MethodInfo methodInfo, RefitSettings refitSettings = null)
        {
            RefitSettings = refitSettings ?? new RefitSettings();
            Type          = targetInterface;
            Name          = methodInfo.Name;
            MethodInfo    = methodInfo;

            var hma = methodInfo.GetCustomAttributes(true)
                      .OfType <HttpMethodAttribute>()
                      .First();

            HttpMethod   = hma.Method;
            RelativePath = hma.Path;

            IsMultipart = methodInfo.GetCustomAttributes(true)
                          .OfType <MultipartAttribute>()
                          .Any();

            verifyUrlPathIsSane(RelativePath);
            determineReturnTypeInfo(methodInfo);

            var parameterList = methodInfo.GetParameters().ToList();

            ParameterInfoMap = parameterList
                               .Select((parameter, index) => new { index, parameter })
                               .ToDictionary(x => x.index, x => x.parameter);
            ParameterMap      = buildParameterMap(RelativePath, parameterList);
            BodyParameterInfo = findBodyParameter(parameterList, IsMultipart);

            Headers            = parseHeaders(methodInfo);
            HeaderParameterMap = buildHeaderParameterMap(parameterList);

            // get names for multipart attachments
            AttachmentNameMap = new Dictionary <int, string>();
            if (IsMultipart)
            {
                for (int i = 0; i < parameterList.Count; i++)
                {
                    if (ParameterMap.ContainsKey(i) || HeaderParameterMap.ContainsKey(i))
                    {
                        continue;
                    }

                    var attachmentName = getAttachmentNameForParameter(parameterList[i]);
                    if (attachmentName == null)
                    {
                        continue;
                    }

                    AttachmentNameMap[i] = attachmentName;
                }
            }

            QueryParameterMap = new Dictionary <int, string>();
            for (int i = 0; i < parameterList.Count; i++)
            {
                if (ParameterMap.ContainsKey(i) || HeaderParameterMap.ContainsKey(i) || (BodyParameterInfo != null && BodyParameterInfo.Item2 == i) || AttachmentNameMap.ContainsKey(i))
                {
                    continue;
                }

                QueryParameterMap[i] = getUrlNameForParameter(parameterList[i]);
            }
        }
Esempio n. 5
0
        public RestMethodInfo(Type targetInterface, MethodInfo methodInfo, RefitSettings refitSettings = null)
        {
            RefitSettings = refitSettings ?? new RefitSettings();
            Type          = targetInterface;
            Name          = methodInfo.Name;
            MethodInfo    = methodInfo;

            var hma = methodInfo.GetCustomAttributes(true)
                      .OfType <HttpMethodAttribute>()
                      .First();

            HttpMethod   = hma.Method;
            RelativePath = hma.Path;

            IsMultipart = methodInfo.GetCustomAttributes(true)
                          .OfType <MultipartAttribute>()
                          .Any();

            MultipartBoundary = IsMultipart ? methodInfo.GetCustomAttribute <MultipartAttribute>(true).BoundaryText : string.Empty;

            VerifyUrlPathIsSane(RelativePath);
            DetermineReturnTypeInfo(methodInfo);
            DetermineIfResponseMustBeDisposed();

            // Exclude cancellation token parameters from this list
            var parameterList = methodInfo.GetParameters().Where(p => p.ParameterType != typeof(CancellationToken)).ToList();

            ParameterInfoMap = parameterList
                               .Select((parameter, index) => new { index, parameter })
                               .ToDictionary(x => x.index, x => x.parameter);
            ParameterMap           = BuildParameterMap(RelativePath, parameterList);
            BodyParameterInfo      = FindBodyParameter(parameterList, IsMultipart, hma.Method);
            AuthorizeParameterInfo = FindAuthorizationParameter(parameterList);

            Headers            = ParseHeaders(methodInfo);
            HeaderParameterMap = BuildHeaderParameterMap(parameterList);

            // get names for multipart attachments
            AttachmentNameMap = new Dictionary <int, Tuple <string, string> >();
            if (IsMultipart)
            {
                for (var i = 0; i < parameterList.Count; i++)
                {
                    if (ParameterMap.ContainsKey(i) || HeaderParameterMap.ContainsKey(i))
                    {
                        continue;
                    }

                    var attachmentName = GetAttachmentNameForParameter(parameterList[i]);
                    if (attachmentName == null)
                    {
                        continue;
                    }

                    AttachmentNameMap[i] = Tuple.Create(attachmentName, GetUrlNameForParameter(parameterList[i]));
                }
            }

            QueryParameterMap = new Dictionary <int, string>();
            for (var i = 0; i < parameterList.Count; i++)
            {
                if (ParameterMap.ContainsKey(i) ||
                    HeaderParameterMap.ContainsKey(i) ||
                    (BodyParameterInfo != null && BodyParameterInfo.Item3 == i) ||
                    (AuthorizeParameterInfo != null && AuthorizeParameterInfo.Item2 == i))
                {
                    continue;
                }

                QueryParameterMap.Add(i, GetUrlNameForParameter(parameterList[i]));
            }

            var ctParams = methodInfo.GetParameters().Where(p => p.ParameterType == typeof(CancellationToken)).ToList();

            if (ctParams.Count > 1)
            {
                throw new ArgumentException($"Argument list to method \"{methodInfo.Name}\" can only contain a single CancellationToken");
            }

            CancellationToken = ctParams.FirstOrDefault();

            IsApiResponse = ReturnResultType.GetTypeInfo().IsGenericType&&
                            (ReturnResultType.GetGenericTypeDefinition() == typeof(ApiResponse <>) ||
                             ReturnResultType.GetGenericTypeDefinition() == typeof(IApiResponse <>) ||
                             ReturnResultType == typeof(IApiResponse));
        }