Skip to content

Latest commit

 

History

History
79 lines (58 loc) · 1.78 KB

apache_http_client_usage.md

File metadata and controls

79 lines (58 loc) · 1.78 KB

RequestLog ApacheHttpClient Configuration

Common Configuration (Logging, Persistence, Retry, etc.)

Maven Dependency

<dependency>
    <groupId>io.github.requestlog</groupId>
    <artifactId>request-log-apache-http-client-starter</artifactId>
    <version>${last stable version}</version>
</dependency>

Enhance HttpClient

1.Enhance through Annotation

@RequestLogEnhanced
@Bean
public HttpClient httpClient() {
    return HttpClients.createDefault();
}

2.Enhance through Enhancer

@Autowired
private ApacheHttpClientRequestLogEnhancer enhancer;

// Use the returned object
HttpClient enhancedHttpClient = enhancer.enhance(httpClient);

3.Enhance all HttpClient Beans through Configuration

request-log.apache-http-client.enhance-all=true

Usage

import org.apache.http.client.HttpClient;
        
/**
 * Original Request
 *  {@link HttpClient#execute} declares checked exceptions `IOException`, `ClientProtocolException`
 */
HttpResponse response = httpClient.execute(new HttpGet("url"));

/**
 * Wrap the request using LogContext
 * Since checked exceptions are declared, use `executeWithExp` instead of `execute`
 */
HttpResponse response = LogContext.log().executeWithExp(() -> {
    return httpClient.execute(new HttpGet("url"));
});

Retry

import io.github.requestlog.apachehttpclient.context.retry.ApacheHttpClientRetryClient;

RetryResult retryResult = RetryContext.create(RequestLogObj, @Nullable RequestRetryJobObj)
        .successWhenResponse((requestContext) -> requestContext.getResponseCode() == 200))
        // Specify using HttpClient as the Retry client
        .with(ApacheHttpClientRetryClient.class, httpClient)
        .execute();