Example #1
0
        /// <summary>
        /// Fetches an object by providing and id.
        /// </summary>
        /// <typeparam name="T">The object type being retrieved.</typeparam>
        /// <param name="id">The id of the resource to retrieve.</param>
        /// <exception cref="RestClientException">
        /// Any Exception from the controller is logged and thrown as RestClientException with InnerException being the caught Exception.
        /// A RestClientException is also thrown when the controller could not be found supporting type T.
        /// </exception>
        /// <returns>An object of the type of resource that was requested.</returns>
        public T Get <T>(string id) where T : HalJsonResource
        {
            this.EnsureAuthenticated();

            IRestQueryController controller = this.GetControllerByType(typeof(T));

            if (controller == null)
            {
                string err = string.Format(ControllerNotFoundForTypeException, typeof(T));
                this.log.Error(err, null);

                throw new RestClientException(err);
            }

            try
            {
                return(controller.Get <T>(id));
            }
            catch (Exception ex)
            {
                this.log.Error(ControllerExceptionText, ex);

                if (ex is RestClientException)
                {
                    throw;
                }

                throw new RestClientException(ControllerExceptionText, ex);
            }
        }
Example #2
0
        /// <summary>
        /// Fetches a list of objects by a given link (url).
        /// </summary>
        /// <typeparam name="T">The type that is expected to be found at the url.</typeparam>
        /// <param name="url">The url to the specific resource to get.</param>
        /// <exception cref="RestClientException">
        /// Any Exception from the controller is logged and thrown as RestClientException with InnerException being the caught Exception.
        /// A RestClientException is also thrown when the controller could not be found supporting type T.
        /// </exception>
        /// <returns>A list of elements of the specified type.</returns>
        public IList <T> GetByLink <T>(string url) where T : HalJsonResource
        {
            this.EnsureAuthenticated();

            IRestQueryController controller = this.GetControllerByUrl(url);

            if (controller == null)
            {
                string err = string.Format(ControllerNotFoundForUrl, url);
                this.log.Error(err, null);

                throw new RestClientException(err);
            }

            try
            {
                return(controller.GetByLink <T>(url));
            }
            catch (Exception ex)
            {
                this.log.Error(ControllerExceptionText, ex);

                if (ex is RestClientException)
                {
                    throw;
                }

                throw new RestClientException(ControllerExceptionText, ex);
            }
        }
Example #3
0
        /// <summary>
        /// Fetches the controller which supports Type t, meaning the controller which is registered with [RestQueryController(SupportedType = t)]
        /// </summary>
        /// <param name="t">The type matching SupportedType</param>
        /// <returns>The found query controller or null if not found</returns>
        private IRestQueryController GetControllerByType(Type t)
        {
            IRestQueryController controller = null;

            foreach (RestQueryControllerAttribute item in this.controllers)
            {
                if (item.SupportedType == t)
                {
                    controller         = (IRestQueryController)Activator.CreateInstance(item.ControllerType);
                    controller.Context = this.GetControllerContext(item);
                    break;
                }
            }

            return(controller);
        }
Example #4
0
        /// <summary>
        /// Fetches the controller by its URL and object type. The url controller part contains the name.
        /// The controller part is the first word after the base address.
        /// It takes into account that the url may contain the base address.
        /// </summary>
        /// <param name="url">Either a full address including base address or a url part starting with controller name.</param>
        /// <returns>The controller, initiated with context</returns>
        private IRestQueryController GetControllerByUrl(string url)
        {
            IRestQueryController controller = null;
            string u = url;

            if (u.StartsWith(this.restQueryConfig.BaseAddress, StringComparison.InvariantCultureIgnoreCase))
            {
                u = u.Substring(this.restQueryConfig.BaseAddress.Length);
            }

            // It should start with controller name, but if it wrongly starts with / that / is removed
            if (u.StartsWith("/") && u.Length > 1)
            {
                u = u.Substring(1);
            }

            string name  = u;
            int    index = u.IndexOfAny(new[] { '/', '?', '$' });

            if (index > 0)
            {
                name = u.Substring(0, index);
            }

            foreach (RestQueryControllerAttribute item in this.controllers)
            {
                if (!item.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }

                controller         = (IRestQueryController)Activator.CreateInstance(item.ControllerType);
                controller.Context = this.GetControllerContext(item);
                break;
            }

            return(controller);
        }