Example #1
0
 /// <summary>
 /// Specifies the <paramref name="statusCode"/>, <paramref name="content"/>, <paramref name="encoding"/> and <paramref name="mediaType"/> to respond with for a request.
 /// </summary>
 /// <param name="responds"></param>
 /// <param name="statusCode">The status code response for given request.</param>
 /// <param name="content">The response content.</param>
 /// <param name="encoding">The encoding.</param>
 /// <param name="mediaType">The media type.</param>
 public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, string content, Encoding encoding, string mediaType)
     where TResult : IResponseResult
 {
     return(responds.Respond(statusCode, content, new MediaTypeHeaderValue(mediaType ?? "text/plain")
     {
         CharSet = (encoding ?? Encoding.UTF8).WebName
     }));
 }
Example #2
0
        /// <summary>
        /// Specifies a function that returns the response for a request.
        /// </summary>
        /// <param name="responds"></param>
        /// <param name="response">The function that provides the response message to return for given request.</param>
        public static TResult Respond <TResult>(this IResponds <TResult> responds, Func <HttpRequestMessage, HttpResponseMessage> response)
            where TResult : IResponseResult
        {
            if (responds is null)
            {
                throw new ArgumentNullException(nameof(responds));
            }

            if (response is null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            return(responds.Respond((request, _) => Task.FromResult(response(request))));
        }
Example #3
0
        /// <summary>
        /// Specifies the <paramref name="statusCode"/> and <paramref name="content"/> to respond with for a request.
        /// </summary>
        /// <param name="responds"></param>
        /// <param name="statusCode">The status code response for given request.</param>
        /// <param name="content">The response content.</param>
        public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, HttpContent content)
            where TResult : IResponseResult
        {
            if (responds is null)
            {
                throw new ArgumentNullException(nameof(responds));
            }

            if (content is null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            return(responds.Respond(async() => new HttpResponseMessage(statusCode)
            {
                Content = await content.CloneAsByteArrayContentAsync().ConfigureAwait(false)
            }));
        }
Example #4
0
        /// <summary>
        /// Specifies the <paramref name="statusCode"/>, <paramref name="content"/> and <paramref name="mediaType"/> to respond with for a request.
        /// </summary>
        /// <param name="responds"></param>
        /// <param name="statusCode">The status code response for given request.</param>
        /// <param name="content">The response content.</param>
        /// <param name="mediaType">The media type.</param>
        public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, string content, MediaTypeHeaderValue mediaType)
            where TResult : IResponseResult
        {
            if (content is null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            return(responds.Respond(() => new HttpResponseMessage(statusCode)
            {
                Content = new StringContent(content)
                {
                    Headers =
                    {
                        ContentType = mediaType
                    }
                }
            }));
        }
Example #5
0
        /// <summary>
        /// Specifies the <paramref name="statusCode"/> and <paramref name="streamContent"/> to respond with for a request.
        /// </summary>
        /// <param name="responds"></param>
        /// <param name="statusCode">The status code response for given request.</param>
        /// <param name="streamContent">The response stream.</param>
        /// <param name="mediaType">The media type.</param>
        public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, Stream streamContent, MediaTypeHeaderValue mediaType)
            where TResult : IResponseResult
        {
            if (streamContent is null)
            {
                throw new ArgumentNullException(nameof(streamContent));
            }

            if (!streamContent.CanRead)
            {
                throw new ArgumentException("Cannot read from stream.", nameof(streamContent));
            }

            byte[] buffer;
            using (var ms = new MemoryStream())
            {
                streamContent.CopyTo(ms);
                buffer = ms.ToArray();
            }

            return(responds.Respond(statusCode, () => new MemoryStream(buffer), mediaType));
        }
Example #6
0
 /// <summary>
 /// Specifies the <see cref="HttpStatusCode.OK"/> and <paramref name="content"/> to respond with for a request.
 /// </summary>
 /// <param name="responds"></param>
 /// <param name="content">The response content.</param>
 public static TResult Respond <TResult>(this IResponds <TResult> responds, string content)
     where TResult : IResponseResult
 {
     return(responds.Respond(HttpStatusCode.OK, content));
 }
Example #7
0
 /// <summary>
 /// Specifies the <paramref name="statusCode"/> response for a request.
 /// </summary>
 /// <param name="responds"></param>
 /// <param name="statusCode">The status code response for given request.</param>
 public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode)
     where TResult : IResponseResult
 {
     return(responds.Respond(() => new HttpResponseMessage(statusCode)));
 }
Example #8
0
 /// <summary>
 /// Specifies the <see cref="HttpStatusCode.OK"/> and <paramref name="content"/> to respond with for a request.
 /// </summary>
 /// <param name="responds"></param>
 /// <param name="content">The response content.</param>
 public static T Respond <T>(this IResponds <T> responds, HttpContent content)
     where T : IResponseResult
 {
     return(responds.Respond(HttpStatusCode.OK, content));
 }
Example #9
0
 /// <summary>
 /// Specifies the <see cref="HttpStatusCode.OK"/> and <paramref name="streamContent"/> to respond with for a request.
 /// </summary>
 /// <param name="responds"></param>
 /// <param name="streamContent">The response stream.</param>
 /// <param name="mediaType">The media type.</param>
 public static TResult Respond <TResult>(this IResponds <TResult> responds, Stream streamContent, MediaTypeHeaderValue mediaType)
     where TResult : IResponseResult
 {
     return(responds.Respond(HttpStatusCode.OK, streamContent, mediaType));
 }
Example #10
0
 /// <summary>
 /// Specifies the <paramref name="statusCode"/> and <paramref name="streamContent"/> to respond with for a request.
 /// </summary>
 /// <param name="responds"></param>
 /// <param name="statusCode">The status code response for given request.</param>
 /// <param name="streamContent">The response stream.</param>
 /// <param name="mediaType">The media type.</param>
 public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, Stream streamContent, string mediaType)
     where TResult : IResponseResult
 {
     return(responds.Respond(statusCode, streamContent, mediaType is null ? null : MediaTypeHeaderValue.Parse(mediaType)));
 }
Example #11
0
 /// <summary>
 /// Specifies the <paramref name="statusCode"/> and <paramref name="streamContent"/> to respond with for a request.
 /// </summary>
 /// <param name="responds"></param>
 /// <param name="statusCode">The status code response for given request.</param>
 /// <param name="streamContent">The response stream.</param>
 public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, Stream streamContent)
     where TResult : IResponseResult
 {
     return(responds.Respond(statusCode, streamContent, (MediaTypeHeaderValue)null));
 }
Example #12
0
 /// <summary>
 /// Specifies the <see cref="HttpStatusCode.OK"/>, <paramref name="content"/>, <paramref name="encoding"/> and <paramref name="mediaType"/> to respond with for a request.
 /// </summary>
 /// <param name="responds"></param>
 /// <param name="content">The response content.</param>
 /// <param name="encoding">The encoding.</param>
 /// <param name="mediaType">The media type.</param>
 public static TResult Respond <TResult>(this IResponds <TResult> responds, string content, Encoding encoding, string mediaType)
     where TResult : IResponseResult
 {
     return(responds.Respond(HttpStatusCode.OK, content, encoding, mediaType));
 }
Example #13
0
 /// <summary>
 /// Specifies the <paramref name="statusCode"/>, <paramref name="content"/> and <paramref name="mediaType"/> to respond with for a request.
 /// </summary>
 /// <param name="responds"></param>
 /// <param name="statusCode">The status code response for given request.</param>
 /// <param name="content">The response content.</param>
 /// <param name="mediaType">The media type.</param>
 public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, string content, string mediaType)
     where TResult : IResponseResult
 {
     return(responds.Respond(statusCode, content, null, mediaType));
 }
Example #14
0
 /// <summary>
 /// Specifies the <paramref name="statusCode"/> and <paramref name="content"/> to respond with for a request.
 /// </summary>
 /// <param name="responds"></param>
 /// <param name="statusCode">The status code response for given request.</param>
 /// <param name="content">The response content.</param>
 public static TResult Respond <TResult>(this IResponds <TResult> responds, HttpStatusCode statusCode, string content)
     where TResult : IResponseResult
 {
     return(responds.Respond(statusCode, content, (string)null));
 }