Skip to content

Commit dd433b8

Browse files
committed
Update Readme for OkHttp
1 parent a5fafbc commit dd433b8

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

README.md

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 💎 CleverClient
22

3-
Library that makes it easy to use the Java HttpClient to perform http operations through interfaces.
3+
A Java library for making http client requests easily.
44

55
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=sashirestela_cleverclient&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=sashirestela_cleverclient)
66
[![codecov](https://codecov.io/gh/sashirestela/cleverclient/graph/badge.svg?token=PEYAFW3EWD)](https://codecov.io/gh/sashirestela/cleverclient)
@@ -12,17 +12,18 @@ Library that makes it easy to use the Java HttpClient to perform http operations
1212
- [Installation](#-installation)
1313
- [Features](#-features)
1414
- [CleverClient Builder](#cleverclient-builder)
15+
- [Http Client](#http-client) **NEW**
1516
- [Interface Annotations](#interface-annotations)
1617
- [Supported Response Types](#supported-response-types)
1718
- [Interface Default Methods](#interface-default-methods)
18-
- [Exception Handling](#exception-handling) **NEW**
19+
- [Exception Handling](#exception-handling)
1920
- [Examples](#-examples)
2021
- [Contributing](#-contributing)
2122
- [License](#-license)
2223

2324
## 💡 Description
2425

25-
CleverClient is a Java 11+ library that makes it easy to use the standard [HttpClient](https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html) component to call http services by using annotated interfaces.
26+
CleverClient is a Java library that simplifies requesting client-side Http services using annotated interfaces and methods. CleverClient uses behind the scenes the well-known Http client libraries: [Java's HttpClient](https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html) (by default) or [Square's OkHttp](https://square.github.io/okhttp/).
2627

2728
For example, if we want to use the public API [JsonPlaceHolder](https://jsonplaceholder.typicode.com/) and call its endpoint ```/posts```, we just have to create an entity ```Post```, an interface ```PostService``` with special annotatons, and call the API through ```CleverClient```:
2829

@@ -54,7 +55,7 @@ public interface PostService {
5455

5556
// Use CleverClient to call the API
5657
var cleverClient = CleverClient.builder()
57-
.baseUrl("https://jsonplaceholder.typicode.com/")
58+
.baseUrl("https://jsonplaceholder.typicode.com")
5859
.build();
5960

6061
var postService = cleverClient.create(PostService.class);
@@ -83,21 +84,28 @@ System.out.println(newPost);
8384

8485
## ⚙ Installation
8586

86-
You can install CleverClient by adding the following dependency to your Maven project:
87-
87+
You can install CleverClient by adding the following dependencies to your Maven project:
8888
```xml
8989
<dependency>
9090
<groupId>io.github.sashirestela</groupId>
9191
<artifactId>cleverclient</artifactId>
92-
<version>[latest version]</version>
92+
<version>[cleverclient_latest_version]</version>
93+
</dependency>
94+
<!-- OkHttp dependency is optional if you decide to use it with CleverClient -->
95+
<dependency>
96+
<groupId>com.squareup.okhttp3</groupId>
97+
<artifactId>okhttp</artifactId>
98+
<version>[okhttp_latest_version]</version>
9399
</dependency>
94100
```
95101

96102
Or alternatively using Gradle:
97103

98104
```groovy
99105
dependencies {
100-
implementation 'io.github.sashirestela:cleverclient:[latest version]'
106+
implementation("io.github.sashirestela:cleverclient:[cleverclient_latest_version]")
107+
/* OkHttp dependency is optional if you decide to use it with CleverClient */
108+
implementation("com.squareup.okhttp3:okhttp:[okhttp_latest_version]")
101109
}
102110
```
103111

@@ -109,19 +117,19 @@ Take in account that you need to use **Java 11 or greater**.
109117

110118
We have the following attributes to create a CleverClient object:
111119

112-
| Attribute | Description | Required |
113-
| -------------------|---------------------------------------------------|-----------|
114-
| baseUrl | Api's url | mandatory |
115-
| headers | Map of headers (name/value) | optional |
116-
| header | Single header as a name and a value | optional |
117-
| httpClient | Java HttpClient object | optional |
118-
| requestInterceptor | Function to modify the request once is built | optional |
119-
| bodyInspector | Function to inspect the `@Body` request parameter | optional |
120-
| endsOfStream | List of texts used to mark the end of streams | optional |
121-
| endOfStream | Text used to mark the end of streams | optional |
122-
| objectMapper | Provides Json conversions either to/from objects | optional |
120+
| Attribute | Description | Required |
121+
| -------------------|------------------------------------------------------------|-----------|
122+
| baseUrl | Api's url | mandatory |
123+
| headers | Map of headers (name/value) | optional |
124+
| header | Single header as a name and a value | optional |
125+
| clientAdapter | Object of one of the specialized HttpClientAdapter classes | optional |
126+
| requestInterceptor | Function to modify the request once is built | optional |
127+
| bodyInspector | Function to inspect the `@Body` request parameter | optional |
128+
| endsOfStream | List of texts used to mark the end of streams | optional |
129+
| endOfStream | Text used to mark the end of streams | optional |
130+
| objectMapper | Provides Json conversions either to/from objects | optional |
123131

124-
The attribute ```end(s)OfStream``` is required when you have endpoints sending back streams of data (Server Sent Events - SSE).
132+
```end(s)OfStream``` is required when you have endpoints sending back streams of data (Server Sent Events - SSE).
125133

126134
Example:
127135

@@ -145,18 +153,36 @@ var objectMapper = new ObjectMapper()
145153
var cleverClient = CleverClient.builder()
146154
.baseUrl(BASE_URL)
147155
.header(HEADER_NAME, HEADER_VALUE)
148-
.httpClient(httpClient)
156+
.clientAdapter(new JavaHttpClientAdapter(httpClient))
149157
.requestInterceptor(request -> {
150158
var url = request.getUrl();
151159
url + (url.contains("?") ? "&" : "?") + "env=testing";
152160
request.setUrl(url);
153161
return request;
154162
})
163+
.bodyInspector(body -> {
164+
var validator = new Validator();
165+
var violations = validator.validate(body);
166+
if (!violations.isEmpty()) {
167+
throw new ConstraintViolationException(violations);
168+
}
169+
})
155170
.endOfStream(END_OF_STREAM)
156171
.objectMapper(objectMapper)
157172
.build();
158173
```
159174

175+
### Http Client
176+
177+
With CleverClient you have two Http client alternatives: Java's HttpClient or OkHttp. The Builder attribute ```clientAdapter``` is used to indicate which to use. If you don't indicate any Http client, the Java's HttpClient will be used by default:
178+
179+
| clientAdapter's value | Description |
180+
|-------------------------------------------------|-------------------------------------|
181+
| new JavaHttpClientAdapter() | Uses a default Java's HttpClient |
182+
| new JavaHttpClientAdapter(customJavaHttpClient) | Uses a custom Java's HttpClient |
183+
| new OkHttpClientAdapter() | Uses a default OkHttpClient |
184+
| new OkHttpClientAdapter(customOkHttpClient) | Uses a custom OkHttpClient |
185+
160186
### Interface Annotations
161187

162188
| Annotation | Target | Attributes | Required Attrs | Mult |
@@ -337,12 +363,9 @@ Some examples have been created in the folder [example](https://github.com/sashi
337363

338364
Where:
339365

340-
* ```<className>``` is mandatory and must be one of the values:
341-
* BasicExample
342-
* FileDownloadExample
343-
* HeaderExample
344-
* MultiServiceExample
345-
* StreamExample (This requires you have an OpenAI account and set the env variable OPENAI_API_TOKEN)
366+
* ```<className>``` is mandatory and must be one of the Java files in the folder example: BasicExample, BasicExampleOkHttp, StreamExample, StreamExampleOkHttp, etc.
367+
368+
Some examples require you have an OpenAI account and set the env variable OPENAI_API_TOKEN, such as Multipart*, Stream*.
346369

347370
* ```[logOptions]``` are optional and you can you use them to set:
348371
* Logger lever: ```-Dorg.slf4j.simpleLogger.defaultLogLevel=<logLevel>```

0 commit comments

Comments
 (0)