Пример #1
0
        /// <summary>
        /// Optionally adds the entity tag (ETag) and last modifed date (Last-Modified) to the response headers.
        /// </summary>
        /// <param name="response"></param>
        /// <param name="etag"></param>
        /// <param name="lastModified"></param>
        /// <returns></returns>
        public static ApiResponseInfo AddEntityCaching(this ApiResponseInfo response, string etag = null, DateTimeOffset?lastModified = null)
        {
            if (response == null)
            {
                return(response);
            }

            if (!string.IsNullOrWhiteSpace(etag))
            {
                response.AddHeader(
                    name: "ETag",
                    value: etag,
                    append: false,
                    allowMultiple: false);
            }

            if (lastModified != null)
            {
                response.AddHeader(
                    name: "Last-Modified",
                    value: lastModified.Value.ToString("r"),
                    append: false,
                    allowMultiple: false);
            }

            return(response);
        }
Пример #2
0
        /// <summary>
        /// Determines whether [has success status].
        /// </summary>
        /// <param name="response">The response.</param>
        /// <returns>
        ///   <c>true</c> if [has success status] [the specified response]; otherwise, <c>false</c>.
        /// </returns>
        public static bool HasSuccessStatus(this ApiResponseInfo response)
        {
            if (response == null)
            {
                return(false);
            }

            return(response.StatusCode.IsBetween(200, 299));
        }
Пример #3
0
        /// <summary>Sets the HTTP status.</summary>
        /// <param name="response">The response.</param>
        /// <param name="status">The status.</param>
        /// <returns></returns>
        public static ApiResponseInfo SetHttpStatus(this ApiResponseInfo response, int status)
        {
            if (response == null)
            {
                response = new ApiResponseInfo();
            }

            response.StatusCode = status;
            return(response);
        }
Пример #4
0
        /// <summary>Adds the header.</summary>
        /// <param name="response">The response.</param>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <param name="append">if set to <c>true</c> [append].</param>
        /// <param name="allowMultiple">if set to <c>true</c> [allow multiple].</param>
        /// <returns></returns>
        public static ApiResponseInfo AddHeader(this ApiResponseInfo response, string name, string value, bool append = false, bool allowMultiple = false)
        {
            if (response == null)
            {
                return(response);
            }

            if (!string.IsNullOrWhiteSpace(name))
            {
                var existing = response.Headers.FirstOrDefault(h => string.Equals(name, h.Name, StringComparison.OrdinalIgnoreCase));

                if (append)
                {
                    if (existing != null && !string.IsNullOrWhiteSpace(value))
                    {
                        var newValue = string.IsNullOrWhiteSpace(existing.Value)
                            ? value
                            : $"{existing.Value}, {value}";

                        existing.Value = newValue;
                    }
                    else
                    {
                        response.Headers.Add(new ApiHeader(name, value));
                    }
                }
                else
                {
                    if (existing != null && !allowMultiple)
                    {
                        response.Headers.Remove(existing);
                    }

                    response.Headers.Add(new ApiHeader(name, value));
                }
            }

            return(response);
        }
Пример #5
0
        /// <summary>Gets the header values.</summary>
        /// <param name="response">The response.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static IList <string> GetHeaderValues(this ApiResponseInfo response, string name)
        {
            if (response == null || string.IsNullOrWhiteSpace(name))
            {
                return new string[] { }
            }
            ;

            List <string> values = new List <string>();

            response.Headers.ForEach(h =>
            {
                if (string.Compare(h.Name, name, true) == 0)
                {
                    if (!values.Contains(h.Value))
                    {
                        values.Add(h.Value);
                    }
                }
            });

            return(values);
        }