private static HttpResponseMessage ToHttpResponseMessage <T>( this IScimResponse <T> scimResponse, HttpRequestMessage httpRequestMessage, Action <T, HttpResponseMessage> responseBuilder, HttpStatusCode statusCode = HttpStatusCode.OK, bool setResponseContent = true) { if (scimResponse == null) { throw new ArgumentNullException("scimResponse"); } if (httpRequestMessage == null) { throw new ArgumentNullException("httpRequestMessage"); } HttpResponseMessage response; HttpStatusCode responseStatusCode = scimResponse.IsLeft ? GetStatusCode(scimResponse.GetLeft()) : statusCode; bool shouldSetResponseContent = setResponseContent && ShouldSetResponseContent(httpRequestMessage, responseStatusCode); if (shouldSetResponseContent) { /* * if (httpRequestMessage.Headers.Accept.Count == 0) * { * MediaTypeFormatter mediaTypeFormatter = httpRequestMessage.GetConfiguration().Formatters.JsonFormatter; * MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue("application/scim+json") { CharSet = "utf-8" }; * response = scimResponse.IsLeft * ? httpRequestMessage.CreateResponse(responseStatusCode, scimResponse.GetLeft(), mediaTypeFormatter, mediaType) * : httpRequestMessage.CreateResponse(responseStatusCode, scimResponse.GetRight(), mediaTypeFormatter, mediaType); * } * else */ { response = scimResponse.IsLeft ? httpRequestMessage.CreateResponse(responseStatusCode, scimResponse.GetLeft()) : httpRequestMessage.CreateResponse(responseStatusCode, scimResponse.GetRight()); } } else { response = httpRequestMessage.CreateResponse(responseStatusCode); } if (scimResponse.IsRight && responseBuilder != null) { responseBuilder.Invoke(scimResponse.GetRight(), response); } return(response); }
/// <summary> /// Invokes the specified <paramref name="responseBuilder" /> action if <paramref name="scimResponse" /> does not contain an error - returning the configured <see cref="HttpResponseMessage" />. /// If <paramref name="scimResponse" /> contains errors, the returned response with contain the error content and will attempt to parse the <see cref="Error.Code" /> as an /// <see cref="HttpStatusCode" /> and assign it to the response message. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="scimResponse">The <see cref="IScimResponse{t}" /> used to build the <see cref="HttpResponseMessage" />.</param> /// <param name="httpRequestMessage">The active <see cref="HttpRequestMessage" />.</param> /// <param name="responseBuilder">The response builder method to invoke when no errors exist.</param> /// <param name="setResponseContent">Determines whether to set the <param name="scimResponse.Data"></param> as the response content.</param> /// <returns>HttpResponseMessage instance.</returns> public static HttpResponseMessage ToHttpResponseMessage <T>( this IScimResponse <T> scimResponse, HttpRequestMessage httpRequestMessage, Action <T, HttpResponseMessage> responseBuilder, Boolean setResponseContent = true) { if (scimResponse == null) { throw new ArgumentNullException("scimResponse"); } if (httpRequestMessage == null) { throw new ArgumentNullException("httpRequestMessage"); } var responseStatusCode = HttpStatusCode.OK; if (scimResponse.IsLeft) { responseStatusCode = GetStatusCode(scimResponse.GetLeft()); } var response = setResponseContent && ShouldSetResponseContent(httpRequestMessage, responseStatusCode) ? httpRequestMessage.CreateResponse(responseStatusCode, scimResponse.GetContent()) : httpRequestMessage.CreateResponse(responseStatusCode); if (scimResponse.IsRight && responseBuilder != null) { responseBuilder.Invoke(scimResponse.GetRight(), response); } return(response); }
public static IScimResponse <TRight> Let <TRight>(this IScimResponse <TRight> scimResponse, Action <TRight> action) { if (scimResponse.IsRight) { action.Invoke(scimResponse.GetRight()); } return(scimResponse); }
public static IScimResponse <T2> Bind <T, T2>(this IScimResponse <T> scimResponse, Func <T, IScimResponse <T2> > bindingFunction) { if (scimResponse.IsLeft) { return(CreateGenericErrorResponse <T, T2>(scimResponse, scimResponse.GetLeft())); } return(bindingFunction.Invoke(scimResponse.GetRight())); }
public static Task <IScimResponse <TRight2> > BindAsync <TRight, TRight2>( this IScimResponse <TRight> scimResponse, Func <TRight, Task <IScimResponse <TRight2> > > bindFunc) { if (scimResponse.IsLeft) { var tcs = new TaskCompletionSource <IScimResponse <TRight2> >(); tcs.SetResult(new ScimErrorResponse <TRight2>(scimResponse.GetLeft())); return(tcs.Task); } return(bindFunc(scimResponse.GetRight())); }
private static object GetContent <T>(this IScimResponse <T> response) { return(response.IsLeft ? (object)response.GetLeft() : response.GetRight()); }