|
1 | | -# CleverClient |
| 1 | +[](https://codecov.io/gh/sashirestela/cleverclient) |
| 2 | + |
| 3 | + |
2 | 4 |
|
| 5 | +# 💎 CleverClient |
3 | 6 | Java library that makes it easier to use the Java's HttpClient to perform http operations, using interfaces. |
| 7 | + |
| 8 | + |
| 9 | +## 💡 Description |
| 10 | +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. |
| 11 | + |
| 12 | +For example, if we want to call the API [JsonPlaceHolder](https://jsonplaceholder.typicode.com/) with the endpoint ```/posts```, we just have to create an entity ```Post```, an interface ```PostService``` with special annotatons, and call the API through ```CleverClient```: |
| 13 | + |
| 14 | +``` |
| 15 | +// Entity |
| 16 | +public class Post { |
| 17 | + private Integer id; |
| 18 | + private String title; |
| 19 | + private String body; |
| 20 | + private Integer userId; |
| 21 | +
|
| 22 | + // Constructors , getters, setters, etc. |
| 23 | +} |
| 24 | +
|
| 25 | +// Interface |
| 26 | +public interface PostService { |
| 27 | +
|
| 28 | + @GET("/posts") |
| 29 | + List<Post> readPosts(@Query("_page") Integer page, @Query("_limit") Integer limit); |
| 30 | +
|
| 31 | + @GET("/posts/{postId}") |
| 32 | + Post readPost(@Path("postId") Integer postId); |
| 33 | +
|
| 34 | + @POST("/posts") |
| 35 | + Post createPost(@Body Post post); |
| 36 | +
|
| 37 | +} |
| 38 | +
|
| 39 | +// Use CleverClient to call the API |
| 40 | +var cleverClient = CleverClient.builder() |
| 41 | + .urlBase("https://jsonplaceholder.typicode.com/") |
| 42 | + .build(); |
| 43 | +
|
| 44 | +var postService = cleverClient.create(PostService.class); |
| 45 | +
|
| 46 | +var page = 1; |
| 47 | +var limit = 5; |
| 48 | +var postId = 17; |
| 49 | +var userId = 3; |
| 50 | +
|
| 51 | +// Example Read Posts |
| 52 | +var postsList = postService.readPosts(page, limit); |
| 53 | +postsList.forEach(System.out::println); |
| 54 | +
|
| 55 | +// Example Read Post |
| 56 | +var onePost = postService.readPost(postId); |
| 57 | +System.out.println(onePost); |
| 58 | +
|
| 59 | +// Example Create Post |
| 60 | +var newPost = postService.createPost(new Post( |
| 61 | + null, |
| 62 | + "Hello", |
| 63 | + "Hello word, you are very welcome!", |
| 64 | + userId)); |
| 65 | +System.out.println(newPost); |
| 66 | +``` |
| 67 | + |
| 68 | + |
| 69 | +## 🛠️ Installation |
| 70 | +You can install CleverClient by adding the following dependency to your Maven project: |
| 71 | + |
| 72 | +```xml |
| 73 | +<dependency> |
| 74 | + <groupId>io.github.sashirestela</groupId> |
| 75 | + <artifactId>cleverclient</artifactId> |
| 76 | + <version>[latest version]</version> |
| 77 | +</dependency> |
| 78 | +``` |
| 79 | + |
| 80 | +Or alternatively using Gradle: |
| 81 | + |
| 82 | +```groovy |
| 83 | +dependencies { |
| 84 | + implementation 'io.github.sashirestela:cleverclient:[latest version]' |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +Take in account that you need to use **Java 11 or above**. |
| 89 | + |
| 90 | + |
| 91 | +## 📕 Features |
| 92 | + |
| 93 | +### CleverClient Builder |
| 94 | +We have the following attributes to create a CleverClient object: |
| 95 | + |
| 96 | +| Attribute | Description | Required | |
| 97 | +| ----------- |------------------------------------|-----------| |
| 98 | +| urlBase | Api's url | mandatory | |
| 99 | +| headers | Pairs of headers name/value | optional | |
| 100 | +| httpClient | Java HttpClient object | optional | |
| 101 | +| endOfStream | Text used to mark final of streams | optional | |
| 102 | + |
| 103 | +Example: |
| 104 | + |
| 105 | +``` |
| 106 | +final var URL_BASE = "https://api.example.com"; |
| 107 | +final var AUTHORIZATION_HEADER = "Authorization"; |
| 108 | +final var BEARER_AUTHORIZATION = "Bearer qwertyasdfghzxcvb"; |
| 109 | +final var END_OF_STREAM = "[DONE]"; |
| 110 | +
|
| 111 | +var httpClient = HttpClient.newBuilder() |
| 112 | + .version(Version.HTTP_1_1) |
| 113 | + .followRedirects(Redirect.NORMAL) |
| 114 | + .connectTimeout(Duration.ofSeconds(20)) |
| 115 | + .executor(Executors.newFixedThreadPool(3)) |
| 116 | + .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80))) |
| 117 | + .build(); |
| 118 | +
|
| 119 | +var cleverClient = CleverClient.builder() |
| 120 | + .urlBase(URL_BASE) |
| 121 | + .headers(Arrays.asList(AUTHORIZATION_HEADER, BEARER_AUTHORIZATION)) |
| 122 | + .httpClient(httpClient) |
| 123 | + .endOfStream(END_OF_STREAM) |
| 124 | + .build(); |
| 125 | +``` |
| 126 | + |
| 127 | +### Interface Annotations |
| 128 | + |
| 129 | +| Annotation | Target | Value | |
| 130 | +|------------|-----------|--------------------------| |
| 131 | +| Resource | Interface | Resource's url | |
| 132 | +| GET | Method | GET endpoint's url | |
| 133 | +| POST | Method | POST endpoint's url | |
| 134 | +| PUT | Method | PUT endpoint's url | |
| 135 | +| DELETE | Method | DELETE endpoint's url | |
| 136 | +| Multipart | Method | None. Mark for multipart | |
| 137 | +| Path | Parameter | Path parameter name | |
| 138 | +| Query | Parameter | Query parameter name | |
| 139 | + |
| 140 | + |
| 141 | +## ✳ Examples |
| 142 | +Some examples have been created in the folder [example](https://github.com/sashirestela/cleverclient/tree/main/src/example/java/io/github/sashirestela/cleverclient/example) and you can follow the next steps to execute them: |
| 143 | +* Clone this respository: |
| 144 | + ``` |
| 145 | + git clone https://github.com/sashirestela/cleverclient.git |
| 146 | + cd cleverclient |
| 147 | + ``` |
| 148 | +* Build the project: |
| 149 | + ``` |
| 150 | + mvn clean install |
| 151 | + ``` |
| 152 | +* Run example: |
| 153 | + ``` |
| 154 | + mvn exec:java -Dexec.mainClass=io.github.sashirestela.cleverclient.example.<className> [logOptions] |
| 155 | + ``` |
| 156 | + Where: |
| 157 | + |
| 158 | + * ```<className>``` is mandatory and must be one of the values: |
| 159 | + * BasicExample |
| 160 | + * StreamExample (This requires you have an OpenAI account and set the env variable OPENAI_API_TOKEN) |
| 161 | + |
| 162 | + * ```[logOptions]``` are optional and you can you use them to set: |
| 163 | + * Logger lever: ```-Dorg.slf4j.simpleLogger.defaultLogLevel=<logLevel>``` |
| 164 | + * Logger file: ```-Dorg.slf4j.simpleLogger.logFile=<logFile>``` |
| 165 | + * For example, to run the BasicExample with all the log options: |
| 166 | + * ```mvn exec:java -Dexec.mainClass=io.github.sashirestela.cleverclient.example.BasicExample -Dorg.slf4j.simpleLogger.defaultLogLevel=debug -Dorg.slf4j.simpleLogger.logFile=example.log``` |
| 167 | + |
| 168 | + |
| 169 | +## 📄 License |
| 170 | +CleverClient is licensed under the MIT License. See the |
| 171 | +[LICENSE](https://github.com/sashirestela/cleverclient/blob/main/LICENSE) file |
| 172 | +for more information. |
0 commit comments