001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.reactive.function.client; 018 019import reactor.core.publisher.Mono; 020 021/** 022 * Represents a function that exchanges a {@linkplain ClientRequest request} for a (delayed) 023 * {@linkplain ClientResponse}. Can be used as an alternative to {@link WebClient}. 024 * 025 * <p>For example: 026 * <pre class="code"> 027 * ExchangeFunction exchangeFunction = ExchangeFunctions.create(new ReactorClientHttpConnector()); 028 * ClientRequest<Void> request = ClientRequest.method(HttpMethod.GET, "https://example.com/resource").build(); 029 * 030 * Mono<String> result = exchangeFunction 031 * .exchange(request) 032 * .then(response -> response.bodyToMono(String.class)); 033 * </pre> 034 * 035 * @author Arjen Poutsma 036 * @since 5.0 037 */ 038@FunctionalInterface 039public interface ExchangeFunction { 040 041 /** 042 * Exchange the given request for a response mono. 043 * @param request the request to exchange 044 * @return the delayed response 045 */ 046 Mono<ClientResponse> exchange(ClientRequest request); 047 048 /** 049 * Filters this exchange function with the given {@code ExchangeFilterFunction}, resulting in a 050 * filtered {@code ExchangeFunction}. 051 * @param filter the filter to apply to this exchange 052 * @return the filtered exchange 053 * @see ExchangeFilterFunction#apply(ExchangeFunction) 054 */ 055 default ExchangeFunction filter(ExchangeFilterFunction filter) { 056 return filter.apply(this); 057 } 058 059}