Esempio n. 1
0
        public void InitializeClient(Type interfaceType)
        {
            ConcurrentDictionary <string, ActionWrapper> wrapper;

            if (cache.TryGetValue(interfaceType, out wrapper))
            {
                return;
            }
            var newWrapper     = new ConcurrentDictionary <string, ActionWrapper>();
            var templatePrefix = interfaceType.GetCustomAttribute <IRoutePrefixAttribute>();

            foreach (var methodInfo in interfaceType.GetMethods())
            {
                var template   = methodInfo.GetCustomAttribute <RouteAttribute>();
                var actionName = GetActionName(methodInfo);
                var action     = new ActionWrapper {
                    Name = actionName, ReturnType = methodInfo.ReturnType, RouteTemplate = ExtensionsFactory.GetRouteTemplate(templatePrefix, template, methodInfo), Parameters = new List <ParameterWrapper>()
                };
                var actions  = methodInfo.GetCustomAttributes(true).OfType <IActionHttpMethodProvider>().ToList();
                var methods  = ExtensionsFactory.GetHttpMethods(actions, methodInfo);
                var handlers = ExtensionsFactory.GetHeaderInspectors(methodInfo);
                action.CustomHandlers = handlers;
                action.Actions        = methods;
                ExtensionsFactory.BuildParameterInfo(methodInfo, action);
                newWrapper.TryAdd(action.Name, action);
            }
            if (cache.TryGetValue(interfaceType, out wrapper))
            {
                return;
            }
            cache.TryAdd(interfaceType, newWrapper);
        }
Esempio n. 2
0
 internal static void BuildParameterInfo(MethodInfo methodInfo, ActionWrapper action)
 {
     var parameterHandler = GetService<IServiceParameterResolver>();
     if (parameterHandler != null)
     {
         action.Parameters.AddRange(parameterHandler.ResolveParameters(methodInfo));
     }
     foreach (var parameterInfo in methodInfo.GetParameters())
     {
         var @in = parameterInfo.GetCustomAttribute<InAttribute>(true);
         if (@in == null)
         {
             var fromBody = parameterInfo.GetCustomAttribute<FromBodyAttribute>(true);
             if (fromBody != null)
                 @in = new InAttribute(InclutionTypes.Body);
             if (@in == null)
             {
                 var fromUri = parameterInfo.GetCustomAttribute<FromUriAttribute>(true);
                 if (fromUri != null)
                     @in = new InAttribute(InclutionTypes.Path);
             }
         }
         action.Parameters.Add(new ParameterWrapper { Name = parameterInfo.Name, Type = parameterInfo.ParameterType, In = @in?.InclutionType ?? InclutionTypes.Body });
     }
 }
Esempio n. 3
0
        private static object TryGetErrorBody(ActionWrapper action, HttpWebResponse resp)
        {
            object result = null;

            try
            {
                using (var reader = new JsonTextReader(new StreamReader(resp.GetResponseStream())))
                {
                    var serializer = new JsonSerializer();
                    result = serializer.Deserialize(reader, action.ReturnType);
                }
            }
            catch
            {
                // ignored
            }
            return(result);
        }
Esempio n. 4
0
        private static ResultWrapper CreateResult(ActionWrapper action, HttpWebResponse response)
        {
            var type = typeof(Task).IsAssignableFrom(action.ReturnType) ? action.ReturnType.GetGenericArguments().First() : action.ReturnType;

            if (type == typeof(void))
            {
                return(new ResultWrapper {
                    Type = typeof(void), IsVoid = true, Value = null, Status = response.StatusCode, StatusMessage = response.StatusDescription
                });
            }
            using (var reader = new JsonTextReader(new StreamReader(response.GetResponseStream())))
            {
                var serializer = new JsonSerializer();
                var result     = serializer.Deserialize(reader, type);
                return(new ResultWrapper {
                    Type = type, IsVoid = false, Value = result, Status = response.StatusCode, StatusMessage = response.StatusDescription
                });
            }
        }
Esempio n. 5
0
        private static ResultWrapper HandleWebException(WebException webError, ActionWrapper action)
        {
            var resp = webError.Response as HttpWebResponse;

            if (resp != null)
            {
                var result = TryGetErrorBody(action, resp);
                return(new ResultWrapper
                {
                    Status = resp.StatusCode,

                    StatusMessage = resp.StatusDescription,
                    Error = webError,
                    Value = result
                });
            }
            return(new ResultWrapper {
                Status = HttpStatusCode.BadGateway, StatusMessage = webError.Message, Error = webError
            });
        }
Esempio n. 6
0
 private void AppendHeaders(ParameterWrapper[] parameters, HttpWebRequest req, ActionWrapper action)
 {
     if (headerHandlers == null)
     {
         return;
     }
     foreach (var headerHandler in headerHandlers)
     {
         headerHandler.SetHeader(req);
     }
     if (action.CustomHandlers != null)
     {
         foreach (var customHandler in action.CustomHandlers)
         {
             customHandler.SetHeader(req);
         }
     }
     foreach (var source in parameters.Where(p => p.In == InclutionTypes.Header))
     {
         req.Headers.Add(string.Format("x-{0}", source.Name), source.value.ToString());
     }
 }