Skip to content

Commit 2a42de1

Browse files
authored
Add Azure Core OkHttp Javadocs (#38014)
1 parent 9425815 commit 2a42de1

File tree

4 files changed

+166
-3
lines changed

4 files changed

+166
-3
lines changed

sdk/core/azure-core-http-okhttp/src/main/java/com/azure/core/http/okhttp/OkHttpAsyncClientProvider.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
import java.util.concurrent.TimeUnit;
1313

1414
/**
15-
* An {@link HttpClientProvider} that provides an implementation of HttpClient based on OkHttp.
15+
* <p>This class provides an OkHttp-based implementation for the {@link HttpClientProvider} interface.
16+
* It is designed to create instances of {@link HttpClient} using OkHttp as the underlying client.</p>
17+
*
18+
* @see com.azure.core.http.okhttp
19+
* @see HttpClientProvider
20+
* @see HttpClient
21+
* @see OkHttpAsyncHttpClientBuilder
1622
*/
1723
public final class OkHttpAsyncClientProvider implements HttpClientProvider {
1824
private static final boolean AZURE_ENABLE_HTTP_CLIENT_SHARING =

sdk/core/azure-core-http-okhttp/src/main/java/com/azure/core/http/okhttp/OkHttpAsyncHttpClient.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,26 @@
3737
import java.io.UncheckedIOException;
3838

3939
/**
40-
* HttpClient implementation for OkHttp.
40+
* This class provides a OkHttp-based implementation for the {@link HttpClient} interface. Creating an instance of this
41+
* class can be achieved by using the {@link OkHttpAsyncHttpClientBuilder} class, which offers OkHttp-specific API for
42+
* features such as {@link OkHttpAsyncHttpClientBuilder#proxy(ProxyOptions) setProxy configuration}, and much more.
43+
*
44+
* <p><strong>Sample: Construct OkHttpAsyncHttpClient with Default Configuration</strong></p>
45+
*
46+
* <p>The following code sample demonstrates the creation of a OkHttp HttpClient that uses port 80 and has no proxy.</p>
47+
*
48+
* <!-- src_embed com.azure.core.http.okhttp.instantiation-simple -->
49+
* <pre>
50+
* HttpClient client = new OkHttpAsyncHttpClientBuilder&#40;&#41;
51+
* .build&#40;&#41;;
52+
* </pre>
53+
* <!-- end com.azure.core.http.okhttp.instantiation-simple -->
54+
*
55+
* <p>For more ways to instantiate OkHttpAsyncHttpClient, refer to {@link OkHttpAsyncHttpClientBuilder}.</p>
56+
*
57+
* @see com.azure.core.http.okhttp
58+
* @see OkHttpAsyncHttpClientBuilder
59+
* @see HttpClient
4160
*/
4261
class OkHttpAsyncHttpClient implements HttpClient {
4362

sdk/core/azure-core-http-okhttp/src/main/java/com/azure/core/http/okhttp/OkHttpAsyncHttpClientBuilder.java

+67
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,73 @@
2626

2727
/**
2828
* Builder class responsible for creating instances of {@link com.azure.core.http.HttpClient} backed by OkHttp.
29+
* The client built from this builder can support sending requests synchronously and asynchronously.
30+
* Use {@link com.azure.core.http.HttpClient#sendSync(HttpRequest, Context)} to send the provided request
31+
* synchronously with contextual information.
32+
*
33+
* <p><strong>Building a new HttpClient instance</strong></p>
34+
*
35+
* <!-- src_embed com.azure.core.http.okhttp.instantiation-simple -->
36+
* <pre>
37+
* HttpClient client = new OkHttpAsyncHttpClientBuilder&#40;&#41;
38+
* .build&#40;&#41;;
39+
* </pre>
40+
* <!-- end com.azure.core.http.okhttp.instantiation-simple -->
41+
*
42+
* <p><strong>Building a new HttpClient instance using http proxy.</strong></p>
43+
*
44+
* <p>Configuring the OkHttp client with a proxy is relevant when your application needs to communicate with Azure
45+
* services through a proxy server.</p>
46+
*
47+
* <!-- src_embed com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder.proxy#ProxyOptions -->
48+
* <pre>
49+
* final String proxyHost = &quot;&lt;proxy-host&gt;&quot;; &#47;&#47; e.g. localhost
50+
* final int proxyPort = 9999; &#47;&#47; Proxy port
51+
* ProxyOptions proxyOptions = new ProxyOptions&#40;ProxyOptions.Type.HTTP,
52+
* new InetSocketAddress&#40;proxyHost, proxyPort&#41;&#41;;
53+
* HttpClient client = new OkHttpAsyncHttpClientBuilder&#40;&#41;
54+
* .proxy&#40;proxyOptions&#41;
55+
* .build&#40;&#41;;
56+
* </pre>
57+
* <!-- end com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder.proxy#ProxyOptions -->
58+
*
59+
* <p><strong>Building a new HttpClient instance with connection timeout.</strong></p>
60+
*
61+
* <p>Setting a reasonable connection timeout is particularly important in scenarios where network conditions might
62+
* be unpredictable or where the server may not be responsive.</p>
63+
*
64+
* <!-- src_embed com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder#connectionTimeout -->
65+
* <pre>
66+
* final Duration connectionTimeout = Duration.ofSeconds&#40;250&#41;; &#47;&#47; connection timeout of 250 seconds
67+
* HttpClient client = new OkHttpAsyncHttpClientBuilder&#40;&#41;
68+
* .connectionTimeout&#40;connectionTimeout&#41;
69+
* .build&#40;&#41;;
70+
* </pre>
71+
* <!-- end com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder#connectionTimeout -->
72+
*
73+
* <p><strong>Building a new HttpClient instance with HTTP/2 Support.</strong></p>
74+
*
75+
* <!-- src_embed com.azure.core.http.okhttp.instantiation-simple -->
76+
* <pre>
77+
* HttpClient client = new OkHttpAsyncHttpClientBuilder&#40;&#41;
78+
* .build&#40;&#41;;
79+
* </pre>
80+
* <!-- end com.azure.core.http.okhttp.instantiation-simple -->
81+
*
82+
* <p>It is also possible to create a OkHttp HttpClient that only supports HTTP/2.</p>
83+
*
84+
* <!-- src_embed readme-sample-useHttp2OnlyWithConfiguredOkHttpClient -->
85+
* <pre>
86+
* &#47;&#47; Constructs an HttpClient that only supports HTTP&#47;2.
87+
* HttpClient client = new OkHttpAsyncHttpClientBuilder&#40;new OkHttpClient.Builder&#40;&#41;
88+
* .protocols&#40;Collections.singletonList&#40;Protocol.H2_PRIOR_KNOWLEDGE&#41;&#41;
89+
* .build&#40;&#41;&#41;
90+
* .build&#40;&#41;;
91+
* </pre>
92+
* <!-- end readme-sample-useHttp2OnlyWithConfiguredOkHttpClient -->
93+
*
94+
* @see HttpClient
95+
* @see OkHttpAsyncHttpClient
2996
*/
3097
public class OkHttpAsyncHttpClientBuilder {
3198

sdk/core/azure-core-http-okhttp/src/main/java/com/azure/core/http/okhttp/package-info.java

+72-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,77 @@
22
// Licensed under the MIT License.
33

44
/**
5-
* Package containing OkHttp HTTP client plugin for azure-core.
5+
* <p><a href="https://learn.microsoft.com/en-us/java/api/overview/azure/core-http-okhttp-readme?view=azure-java-stable">
6+
* Azure Core Http OkHttp</a> client library is a plugin for the azure-core HTTP client API. It allows you to use OkHttp
7+
* as the underlying HTTP client for communicating with Azure services. OkHttp is a popular and efficient HTTP client
8+
* that supports features such as HTTP/2, connection pooling, compression, and caching. To use the OkHttp client library,
9+
* you need to include the dependency in your project and configure it when creating a service client.
10+
* For more details refer to our <a href="https://learn.microsoft.com/azure/developer/java/sdk/http-client-pipeline#http-clients">conceptual documentation</a>.</p>
11+
*
12+
* <p><strong>Sample: Construct OkHttpAsyncHttpClient with Default Configuration</strong></p>
13+
*
14+
* <p>The following code sample demonstrates the creation of a OkHttp HttpClient that uses port 80 and has no proxy.</p>
15+
*
16+
* <!-- src_embed readme-sample-createBasicClient -->
17+
* <pre>
18+
* HttpClient client = new OkHttpAsyncHttpClientBuilder&#40;&#41;.build&#40;&#41;;
19+
* </pre>
20+
* <!-- end readme-sample-createBasicClient -->
21+
*
22+
* <hr>
23+
*
24+
* <h2><strong>Using OkHttpAsyncHttpClient with Http Proxy</strong></h2>
25+
*
26+
* <p>Configuring the OkHttp client with a proxy in the context of Azure Java SDK is relevant when your application needs
27+
* to communicate with Azure services through a proxy server. For more details refer to our
28+
* <a href="https://learn.microsoft.com/azure/developer/java/sdk/proxying#http-proxy-configuration">conceptual documentation</a>.</p>
29+
*
30+
* <p>The following code sample demonstrates the creation of a OkHttp HttpClient that is using a proxy.</p>
31+
*
32+
* <!-- src_embed com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder.proxy#ProxyOptions -->
33+
* <pre>
34+
* final String proxyHost = &quot;&lt;proxy-host&gt;&quot;; &#47;&#47; e.g. localhost
35+
* final int proxyPort = 9999; &#47;&#47; Proxy port
36+
* ProxyOptions proxyOptions = new ProxyOptions&#40;ProxyOptions.Type.HTTP,
37+
* new InetSocketAddress&#40;proxyHost, proxyPort&#41;&#41;;
38+
* HttpClient client = new OkHttpAsyncHttpClientBuilder&#40;&#41;
39+
* .proxy&#40;proxyOptions&#41;
40+
* .build&#40;&#41;;
41+
* </pre>
42+
* <!-- end com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder.proxy#ProxyOptions -->
43+
*
44+
* <hr>
45+
*
46+
* <h2><strong>Using OkHttpAsyncHttpClient with HTTP/2 Support</strong></h2>
47+
*
48+
* <p>The following code sample demonstrates the creation of a OkHttp HttpClient that supports both the HTTP/1.1 and
49+
* HTTP/2 protocols, with HTTP/2 being the preferred protocol.</p>
50+
*
51+
* <!-- src_embed readme-sample-useHttp2WithConfiguredOkHttpClient -->
52+
* <pre>
53+
* &#47;&#47; Constructs an HttpClient that supports both HTTP&#47;1.1 and HTTP&#47;2 with HTTP&#47;2 being the preferred protocol.
54+
* &#47;&#47; This is the default handling for OkHttp.
55+
* HttpClient client = new OkHttpAsyncHttpClientBuilder&#40;new OkHttpClient.Builder&#40;&#41;
56+
* .protocols&#40;Arrays.asList&#40;Protocol.HTTP_2, Protocol.HTTP_1_1&#41;&#41;
57+
* .build&#40;&#41;&#41;
58+
* .build&#40;&#41;;
59+
* </pre>
60+
* <!-- end readme-sample-useHttp2WithConfiguredOkHttpClient -->
61+
*
62+
* <p>It is also possible to create a OkHttp HttpClient that only supports HTTP/2.</p>
63+
*
64+
* <!-- src_embed readme-sample-useHttp2OnlyWithConfiguredOkHttpClient -->
65+
* <pre>
66+
* &#47;&#47; Constructs an HttpClient that only supports HTTP&#47;2.
67+
* HttpClient client = new OkHttpAsyncHttpClientBuilder&#40;new OkHttpClient.Builder&#40;&#41;
68+
* .protocols&#40;Collections.singletonList&#40;Protocol.H2_PRIOR_KNOWLEDGE&#41;&#41;
69+
* .build&#40;&#41;&#41;
70+
* .build&#40;&#41;;
71+
* </pre>
72+
* <!-- end readme-sample-useHttp2OnlyWithConfiguredOkHttpClient -->
73+
*
74+
* @see com.azure.core.http.okhttp.OkHttpAsyncHttpClient
75+
* @see com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder
76+
* @see okhttp3.OkHttpClient
677
*/
778
package com.azure.core.http.okhttp;

0 commit comments

Comments
 (0)