/// <summary> /// Map an async handler to a specific http status code /// </summary> /// <param name="request">The ISolidHttpRequest</param> /// <param name="predicate">The predicate</param> /// <param name="handler">The async handler</param> /// <returns>SolidHttpRequest</returns> public static ISolidHttpRequest On(this ISolidHttpRequest request, Func <HttpResponseMessage, bool> predicate, Func <IServiceProvider, HttpResponseMessage, Task> handler) { request.OnResponse(async(services, response) => { if (predicate(response)) { await handler(services, response); } }); return(request); }
/// <summary> /// Expect a success status code /// <para>If a non-success status code is received, an InvalidOperationException is thrown</para> /// </summary> /// <param name="request">The ISolidHttpRequest</param> /// <returns>SolidHttpRequest</returns> public static ISolidHttpRequest ExpectSuccess(this ISolidHttpRequest request) { request.OnResponse(async(services, response) => { if (!response.IsSuccessStatusCode) { var message = await GenerateNonSuccessMessage(response); // TODO: reevaluate this exception type. maybe a seperate type for server error and client error throw new InvalidOperationException(message); } }); return(request); }
/// <summary> /// Add an event to be run the moment the response is received /// </summary> /// <param name="request">The Solid.Http request</param> /// <param name="func">The handler to be run</param> /// <returns>The Solid.Http request object</returns> public static ISolidHttpRequest OnResponse(this ISolidHttpRequest request, Func <HttpResponseMessage, Task> func) => request.OnResponse((_, r) => func(r));
/// <summary> /// Add an event to be run the moment the response is received /// </summary> /// <param name="request">The Solid.Http request</param> /// <param name="action">The handler to be run</param> /// <returns>The Solid.Http request object</returns> public static ISolidHttpRequest OnResponse(this ISolidHttpRequest request, Action <HttpResponseMessage> action) => request.OnResponse((_, r) => action(r));
/// <summary> /// Add an event to be run the moment the response is received /// </summary> /// <param name="request">The Solid.Http request</param> /// <param name="action">The handler to be run</param> /// <returns>The Solid.Http request object</returns> public static ISolidHttpRequest OnResponse(this ISolidHttpRequest request, Action <IServiceProvider, HttpResponseMessage> action) => request.OnResponse(action.ToAsyncFunc());