Exemple #1
0
        /// <summary>
        /// Gets a response object from the API response.
        /// </summary>
        /// <typeparam name="T">Type of result</typeparam>
        /// <param name="httpResponseMessage">HttpResponseMessage</param>
        /// <returns>Response</returns>
        private async Task <Response <T> > GetResponse <T>(HttpResponseMessage httpResponseMessage)
        {
            Response <T> response       = new Response <T>();
            Type         resultType     = typeof(T);
            TypeInfo     resultTypeInfo = resultType.GetTypeInfo();

            response.StatusCode   = (int)httpResponseMessage.StatusCode;
            response.ReasonPhrase = httpResponseMessage.ReasonPhrase;

            if (response.StatusCode >= 200 && response.StatusCode < 300)
            {
                response.Success = true;
                string responseContent = await httpResponseMessage.Content.ReadAsStringAsync();

                if (resultType == typeof(JObject) || resultType == typeof(JArray) || resultType.IsArray || (resultTypeInfo.IsGenericType && resultType.Name.Equals(typeof(List <>).Name)))
                {
                    response.Result = JsonConvert.DeserializeObject <T>(responseContent);
                }
                else
                {
                    dynamic         data        = JsonConvert.DeserializeObject(responseContent);
                    ConstructorInfo constructor = Helper.GetConstructorWithDataParameter(resultType);

                    if (constructor != null)
                    {
                        Helper.ObjectActivator <T> activator = Helper.GetActivator <T>(constructor);
                        response.Result = activator(data);
                    }
                }
            }
            else
            {
                JObject data = JsonConvert.DeserializeObject <JObject>(await httpResponseMessage.Content.ReadAsStringAsync());

                response.Success = false;
                response.Errors  = new List <ErrorMessage>();

                if (data != null && (data["errors"] != null))
                {
                    foreach (JObject error in data["errors"])
                    {
                        if (error.HasValues)
                        {
                            string code    = error.Value <string>("code");
                            string message = error.Value <string>("message");
                            response.Errors.Add(new ErrorMessage(code, message));
                        }
                    }
                }
            }

            return(response);
        }
Exemple #2
0
        public CustomObject(dynamic data)
        {
            this.Id             = data.id;
            this.Version        = data.version;
            this.Container      = data.container;
            this.Key            = data.key;
            this.CreatedAt      = data.createdAt;
            this.LastModifiedAt = data.lastModifiedAt;

            ConstructorInfo constructor = Helper.GetConstructorWithDataParameter(typeof(T));

            if (constructor != null)
            {
                Helper.ObjectActivator <T> activator = Helper.GetActivator <T>(constructor);
                this.Value = activator(data.value);
            }
            else if (data.value is JValue jValue)
            {
                this.Value = jValue.ToObject <T>();
            }
        }
Exemple #3
0
        /// <summary>
        /// Gets a list from an array of JSON objects.
        /// </summary>
        /// <remarks>
        /// For instances of T to be created where T is not a primitive type (or a DateTime, or a decimal, or a string), T must have a constructor that accepts only one parameter: "data" of type System.Object
        /// </remarks>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="jArray">Array of JSON objects</param>
        /// <returns>List of T, or null</returns>
        public static List <T> GetListFromJsonArray <T>(JArray jArray)
        {
            if (jArray == null || jArray.Count < 1)
            {
                return(new List <T>());
            }

            List <T> list     = new List <T>();
            var      type     = typeof(T);
            var      typeInfo = type.GetTypeInfo();

            if (typeInfo.IsPrimitive || type == typeof(DateTime) || type == typeof(decimal) || type == typeof(string))
            {
                foreach (dynamic data in jArray)
                {
                    T listItem = (T)data;
                    list.Add(listItem);
                }
            }
            else
            {
                ConstructorInfo constructor = Helper.GetConstructorWithDataParameter(type);

                if (constructor != null)
                {
                    Helper.ObjectActivator <T> activator = Helper.GetActivator <T>(constructor);

                    foreach (dynamic data in jArray)
                    {
                        T listItem = activator(data);
                        list.Add(listItem);
                    }
                }
            }

            return(list);
        }