Example #1
0
        public static dynamic InvokeGenericMethod(GenericInvokerParameters parameters)
        {
            var genericMethodInfo = typeof(GenericInvoker).GetMethod("ReadEntity");

            genericMethodInfo = genericMethodInfo.MakeGenericMethod(parameters.InvokeType);
            return(genericMethodInfo.Invoke(new GenericInvoker(), new[] { parameters }));
        }
Example #2
0
 public T ReadEntity <T>(GenericInvokerParameters parameters)
 {
     return(ResponseHelper.ReadEntity <T>(parameters.ResponseMessage));
 }
Example #3
0
        /// <summary>Converts HttpResponseMessage into SDK response object.</summary>
        /// <param name="responseMessage">An HttpResponseMessage object</param>
        /// <returns>A SDK response object.</returns>
        public static T FromHttpResponseMessage <T>(HttpResponseMessage responseMessage) where T : IOciResponse, new()
        {
            var response = new T();

            PropertyInfo[] props = typeof(T).GetProperties();
            foreach (var prop in props)
            {
                object[] attrs = prop.GetCustomAttributes(false);
                foreach (var attr in attrs)
                {
                    HttpConverterAttribute httpResponseAttr = attr as HttpConverterAttribute;
                    if (httpResponseAttr == null)
                    {
                        continue;
                    }
                    if (httpResponseAttr.Target == TargetEnum.Header)
                    {
                        var propertyType = prop.PropertyType;
                        // Special handling of Dictionary type in header since the Dictionary contents are returned as individual header entries.
                        if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Dictionary <,>))
                        {
                            var value  = new Dictionary <string, string>();
                            var prefix = httpResponseAttr.Name;
                            foreach (var header in responseMessage.Headers)
                            {
                                if (header.Key.StartsWith(prefix))
                                {
                                    // Remove the prefix and add header to dictionary.
                                    value.Add(header.Key.Substring(prefix.Length), header.Value.FirstOrDefault());
                                }
                            }
                            foreach (var header in responseMessage.Content.Headers)
                            {
                                if (header.Key.StartsWith(prefix))
                                {
                                    // Remove the prefix and add header to dictionary.
                                    value.Add(header.Key.Substring(prefix.Length), header.Value.FirstOrDefault());
                                }
                            }
                            if (value.Count > 0)
                            {
                                prop.SetValue(response, value);
                            }
                        }
                        else
                        {
                            if (responseMessage.Headers.TryGetValues(httpResponseAttr.Name, out var headerValues))
                            {
                                logger.Debug($"Setting property value from header {httpResponseAttr.Name}");
                                prop.SetValue(response, HeaderUtils.ToValue(headerValues.FirstOrDefault(), prop.PropertyType));
                            }
                            if (responseMessage.Content.Headers.TryGetValues(httpResponseAttr.Name, out var contentHeaderValues))
                            {
                                logger.Debug($"Setting property value from content header {httpResponseAttr.Name}");
                                prop.SetValue(response, HeaderUtils.ToValue(contentHeaderValues.FirstOrDefault(), prop.PropertyType));
                            }
                        }
                    }
                    else if (httpResponseAttr.Target == TargetEnum.Body)
                    {
                        GenericInvokerParameters genericInvokerParameters = new GenericInvokerParameters
                        {
                            InvokeType      = prop.PropertyType,
                            ResponseMessage = responseMessage
                        };
                        var responseEntity = GenericHelper.InvokeGenericMethod(genericInvokerParameters);
                        prop.SetValue(response, responseEntity);
                    }
                }
            }
            return(response);
        }