From 7f5747e788821a932baae48896222bebcda68bde Mon Sep 17 00:00:00 2001 From: Sashir Estela Date: Tue, 19 Mar 2024 02:08:06 +0000 Subject: [PATCH 1/5] Setup for code formatting --- codestyle/spotless_java.importorder | 6 + codestyle/spotless_java_eclipseformat.xml | 399 ++++++++++++++++++++++ pom.xml | 35 +- 3 files changed, 439 insertions(+), 1 deletion(-) create mode 100644 codestyle/spotless_java.importorder create mode 100644 codestyle/spotless_java_eclipseformat.xml diff --git a/codestyle/spotless_java.importorder b/codestyle/spotless_java.importorder new file mode 100644 index 0000000..809d5e1 --- /dev/null +++ b/codestyle/spotless_java.importorder @@ -0,0 +1,6 @@ +#Organize Import Order +#Mon Feb 26 16:38:48 PET 2024 +0= +1=javax +2=java +3=\# \ No newline at end of file diff --git a/codestyle/spotless_java_eclipseformat.xml b/codestyle/spotless_java_eclipseformat.xml new file mode 100644 index 0000000..bbe7987 --- /dev/null +++ b/codestyle/spotless_java_eclipseformat.xml @@ -0,0 +1,399 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 147ac1a..33227df 100644 --- a/pom.xml +++ b/pom.xml @@ -60,7 +60,7 @@ 3.12.1 3.4.1 3.2.5 - 3.1.1 + 3.2.0 0.8.11 3.5.0 2.16.2 @@ -68,6 +68,7 @@ 3.6.3 3.1.0 1.6.13 + 2.43.0 @@ -160,6 +161,12 @@ sign + + + --pinentry-mode + loopback + + @@ -340,6 +347,32 @@ + + + com.diffplug.spotless + spotless-maven-plugin + ${spotless.version} + + origin/main + + + + src/*/java/**/*.java + + + + ${project.basedir}/codestyle/spotless_java_eclipseformat.xml + + + false + ${project.basedir}/codestyle/spotless_java.importorder + + + cleanthat-javaparser-unnecessaryimport + + + + From 135377dc0570a76952df0604b05cfb07317a1047 Mon Sep 17 00:00:00 2001 From: Sashir Estela Date: Tue, 19 Mar 2024 02:09:09 +0000 Subject: [PATCH 2/5] Improve Github workflows --- .../{maven.yml => build_java_maven.yml} | 13 +++++--- .github/workflows/release_maven_central.yml | 32 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) rename .github/workflows/{maven.yml => build_java_maven.yml} (68%) create mode 100644 .github/workflows/release_maven_central.yml diff --git a/.github/workflows/maven.yml b/.github/workflows/build_java_maven.yml similarity index 68% rename from .github/workflows/maven.yml rename to .github/workflows/build_java_maven.yml index bdd77db..ebbaa27 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/build_java_maven.yml @@ -14,16 +14,21 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 - name: Set up JDK 11 - uses: actions/setup-java@v3 + uses: actions/setup-java@v4 with: java-version: "11" distribution: "temurin" cache: maven + - name: Check code format with Spotless + run: mvn spotless:check - name: Build with Maven run: mvn -B package --file pom.xml - name: Upload coverage reports to Codecov - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v4 env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/release_maven_central.yml b/.github/workflows/release_maven_central.yml new file mode 100644 index 0000000..ef83c83 --- /dev/null +++ b/.github/workflows/release_maven_central.yml @@ -0,0 +1,32 @@ +name: Release to Maven Central +run-name: Release run ${{ github.run_number }} +on: + workflow_dispatch: + +jobs: + release: + name: Release + runs-on: ubuntu-latest + environment: production + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Setup Java + uses: actions/setup-java@v4 + with: + java-version: "11" + distribution: "temurin" + java-package: "jdk" + check-latest: true + server-id: "ossrh" + server-username: OSSRH_USERNAME + server-password: OSSRH_PASSWORD + gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} + gpg-passphrase: GPG_PASSPHRASE + cache: "maven" + - name: Build & Deploy + run: mvn -U -B clean deploy -P release + env: + OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} + OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} + GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} \ No newline at end of file From ecded63153831de32ba7deca4b8a0c6105e81bee Mon Sep 17 00:00:00 2001 From: Sashir Estela Date: Tue, 19 Mar 2024 02:09:34 +0000 Subject: [PATCH 3/5] Improve documentation --- CONTRIBUTING.md | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 17 +++++++- 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..35cbce5 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,103 @@ +# Contributing +Thank you very much for your interest in contributing to the improvement of this project. Please, follow the next guidelines. + +## General Guidelines +1. Keep the code compatible with Java 11. +1. Follow existing code styles present in the project. +1. Avoid adding new dependencies as much as possible. +1. Ensure to add Javadoc where necessary. +1. Provide unit tests for your code. +1. Large features should be discussed with maintainers before implementation. Use the [Discussion](https://github.com/sashirestela/cleverclient/discussions) section for this. + +## Code Formatting +This project is configured with [Spotless](https://github.com/diffplug/spotless/tree/main/plugin-maven) to automatically check code formatting against a [custom code format](codestyle/spotless_java_eclipseformat.xml) specification of modern Java, as well as [import order](codestyle/spotless_java.importorder). + +Spotless comes with two Maven commands that you can run at any moment, preferably before committing your changes: + +- `mvn spotless:check`, to verify if code is formatted correctly. +- `mvn spotless:apply`, to rewrite the source code in place. + +The remote build will check the code formatting, so the build will fail if the code isn't formatted correctly. + + +## Configure IDE (Optional) +You could set your favorite IDE to be aligned to the custom code format. See details below. + +
+ +IntelliJ + +1. Import the custom code format: + + - Settings > Editor > Code Style > Java > Scheme > ⚙ > Import Scheme > Eclipse XML Profile + +2. Select the file on _codestyle/spotless_java_eclipseformat.xml_ + +3. In the _Imports_ tab for the previous scheme, change the values: + - In _General_ section, mark the option _Use single class import_ + - In the field _Class count to use import with '*'_ put the value 99 + - In the field _Names count to use static import with '*'_ put the value 99 + +4. At the bottom part in the _Imports_ section, change the import statement order as: + - **import** all other imports + - **import** javax.* + - **import** java.* + - **import static** all other imports + +
+ +
+ +VSCode + +1. Install the extension _Language Support for Java by Red Hat_ + +2. Set the custom code format: + + - Settings > Workspace > Extensions > Language Support for Java(TM) by Red Hat > Formatting + - Choose the box _Java/Format/Settings:Url_ and put the value _codestyle/spotless_java_eclipseformat.xml_ + +3. Set the import order: + + - Settings > Workspace > Extensions > Language Support for Java(TM) by Red Hat > Code Completion + - Choose the box _Java/Completion:ImportOrder_ and click the link _Edit in settings.json_ + - In the editor, make sure to add the following entries: + + ```json + "java.completion.importOrder": [ + "", + "javax", + "java", + "*" + ] + ``` + +
+ +
+ +Eclipse + +1. Import the custom code format: + + - Preferences > Java > Code Style > Formatter > Import... + +2. Select the file on _codestyle/spotless_java_eclipseformat.xml_ + +3. Import the custom import statement order: + + - Preferences > Java > Code Style > Organize Imports > Import... + +4. Select the file on _codestyle/spotless_java.importorder_ + +5. In the _Organize Imports_ section, make sure the values are set as: + + - Sorting order: + - \* - all unmatched type imports + - javax + - java + - \* - all unmatched static imports + - Number of imports needed for .*: 99 + - Number of static imports needed for .*: 99 + +
\ No newline at end of file diff --git a/README.md b/README.md index 9ddd4af..8832860 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,23 @@ Java library that makes it easier to use the Java's HttpClient to perform http operations, using interfaces. +[![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) [![codecov](https://codecov.io/gh/sashirestela/cleverclient/graph/badge.svg?token=PEYAFW3EWD)](https://codecov.io/gh/sashirestela/cleverclient) ![Maven Central](https://img.shields.io/maven-central/v/io.github.sashirestela/cleverclient) ![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/sashirestela/cleverclient/maven.yml) +### Table of Contents +- [Description](#-description) +- [Installation](#-installation) +- [Features](#-features) + - [CleverClient Builder](#cleverclient-builder) + - [Interface Annotations](#interface-annotations) + - [Supported Response Types](#supported-response-types) + - [Interface Default Methods](#interface-default-methods) +- [Examples](#-examples) +- [Contributing](#-contributing) +- [License](#-license) + ## 💡 Description 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. @@ -253,8 +266,10 @@ Some examples have been created in the folder [example](https://github.com/sashi * For example, to run the BasicExample with all the log options: * ```mvn exec:java -Dexec.mainClass=io.github.sashirestela.cleverclient.example.BasicExample -Dorg.slf4j.simpleLogger.defaultLogLevel=debug -Dorg.slf4j.simpleLogger.logFile=example.log``` -## 📄 License +## 💼 Contributing +Please read our [Contributing](CONTRIBUTING.md) guide to learn and understand how to contribute to this project. +## 📄 License CleverClient is licensed under the MIT License. See the [LICENSE](https://github.com/sashirestela/cleverclient/blob/main/LICENSE) file for more information. From c72c2aab9c20d70b4f45d44103929a7dbb23f4e1 Mon Sep 17 00:00:00 2001 From: Sashir Estela Date: Tue, 19 Mar 2024 02:11:34 +0000 Subject: [PATCH 4/5] Improve Sonar quality gate --- .../cleverclient/http/HttpConnector.java | 39 ++++++++----------- .../cleverclient/http/HttpProcessor.java | 11 ++---- .../metadata/InterfaceMetadata.java | 26 ++++++------- .../metadata/InterfaceMetadataStore.java | 10 ++--- .../sender/HttpAsyncStreamSender.java | 10 +++-- .../cleverclient/sender/HttpSender.java | 2 +- .../sender/HttpSyncStreamSender.java | 10 +++-- .../cleverclient/support/CleverClientSSE.java | 31 +++++---------- .../cleverclient/support/Configurator.java | 28 ++++++++++--- .../cleverclient/util/CommonUtil.java | 12 +++--- .../cleverclient/util/Constant.java | 4 +- .../metadata/InterfaceMetadataStoreTest.java | 2 +- .../support/CleverClientSSETest.java | 2 +- .../util/UnixTimestampDeserializerTest.java | 2 +- 14 files changed, 95 insertions(+), 94 deletions(-) diff --git a/src/main/java/io/github/sashirestela/cleverclient/http/HttpConnector.java b/src/main/java/io/github/sashirestela/cleverclient/http/HttpConnector.java index b3b5825..ccf4719 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/http/HttpConnector.java +++ b/src/main/java/io/github/sashirestela/cleverclient/http/HttpConnector.java @@ -48,8 +48,9 @@ public Object sendRequest() { if (requestInterceptor != null) { interceptRequest(); } + var formattedHeaders = printHeaders(headers); logger.debug("Http Call : {} {}", httpMethod, url); - logger.debug("Request Headers : {}", printHeaders(headers)); + logger.debug("Request Headers : {}", formattedHeaders); var bodyPublisher = createBodyPublisher(bodyObject, contentType); var responseClass = returnType.getBaseClass(); @@ -94,34 +95,28 @@ private BodyPublisher createBodyPublisher(Object bodyObject, ContentType content if (contentType == null) { logger.debug("Request Body : (Empty)"); bodyPublisher = BodyPublishers.noBody(); - } else { - switch (contentType) { - case MULTIPART_FORMDATA: - logger.debug("Request Body : {}", (Map) bodyObject); - var bodyBytes = HttpMultipart.toByteArrays((Map) bodyObject); - bodyPublisher = BodyPublishers.ofByteArrays(bodyBytes); - break; - - case APPLICATION_JSON: - logger.debug("Request Body : {}", (String) bodyObject); - bodyPublisher = BodyPublishers.ofString((String) bodyObject); - break; - } + } else if (contentType == ContentType.MULTIPART_FORMDATA) { + logger.debug("Request Body : {}", bodyObject); + var bodyBytes = HttpMultipart.toByteArrays((Map) bodyObject); + bodyPublisher = BodyPublishers.ofByteArrays(bodyBytes); + } else if (contentType == ContentType.APPLICATION_JSON) { + logger.debug("Request Body : {}", bodyObject); + bodyPublisher = BodyPublishers.ofString((String) bodyObject); } return bodyPublisher; } private String printHeaders(List headers) { - var print = "{"; - for (var i = 0; i < headers.size(); i++) { + var print = new StringBuilder("{"); + for (var i = 0; i < headers.size(); i += 2) { if (i > 1) { - print += ", "; + print.append(", "); } - var headerKey = headers.get(i++); - var headerVal = headerKey.equals("Authorization") ? "*".repeat(10) : headers.get(i); - print += headerKey + " = " + headerVal; + var headerKey = headers.get(i); + var headerVal = headerKey.equals("Authorization") ? "*".repeat(10) : headers.get(i + 1); + print.append(headerKey + " = " + headerVal); } - print += "}"; - return print; + print.append("}"); + return print.toString(); } } \ No newline at end of file diff --git a/src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java b/src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java index 0469a52..914ece9 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java +++ b/src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java @@ -123,13 +123,10 @@ private Object calculateBodyObject(MethodMetadata methodMetadata, Object[] argum var bodyIndex = methodMetadata.getBodyIndex(); var bodyObject = bodyIndex >= 0 ? arguments[bodyIndex] : null; if (bodyObject != null) { - switch (methodMetadata.getContentType()) { - case MULTIPART_FORMDATA: - bodyObject = JsonUtil.objectToMap(bodyObject); - break; - case APPLICATION_JSON: - bodyObject = JsonUtil.objectToJson(bodyObject); - break; + if (methodMetadata.getContentType() == ContentType.MULTIPART_FORMDATA) { + bodyObject = JsonUtil.objectToMap(bodyObject); + } else if (methodMetadata.getContentType() == ContentType.APPLICATION_JSON) { + bodyObject = JsonUtil.objectToJson(bodyObject); } } return bodyObject; diff --git a/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadata.java b/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadata.java index a972822..d1b200c 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadata.java +++ b/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadata.java @@ -13,14 +13,14 @@ @Value @Builder public class InterfaceMetadata { - private final static String ANNOT_RESOURCE = "Resource"; - private final static String ANNOT_HEADER = "Header"; - private final static String ANNOT_MULTIPART = "Multipart"; - private final static String ANNOT_PARAM_BODY = "Body"; - private final static String ANNOT_PARAM_PATH = "Path"; - private final static String ANNOT_PARAM_QUERY = "Query"; - private final static String ANNOT_FIELD_NAME = "name"; - private final static String ANNOT_FIELD_VALUE = "value"; + private static final String ANNOT_RESOURCE = "Resource"; + private static final String ANNOT_HEADER = "Header"; + private static final String ANNOT_MULTIPART = "Multipart"; + private static final String ANNOT_PARAM_BODY = "Body"; + private static final String ANNOT_PARAM_PATH = "Path"; + private static final String ANNOT_PARAM_QUERY = "Query"; + private static final String ANNOT_FIELD_NAME = "name"; + private static final String ANNOT_FIELD_VALUE = "value"; String name; List annotations; @@ -77,11 +77,11 @@ public String getHttpAnnotationName() { } public ContentType getContentType() { - return getBodyIndex() == -1 - ? null - : isMultipart() - ? ContentType.MULTIPART_FORMDATA - : ContentType.APPLICATION_JSON; + if (getBodyIndex() == -1) { + return null; + } else { + return isMultipart() ? ContentType.MULTIPART_FORMDATA : ContentType.APPLICATION_JSON; + } } private boolean isMultipart() { diff --git a/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java b/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java index 36d00a0..5f58791 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java +++ b/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java @@ -114,7 +114,7 @@ private List getParameters(Parameter[] javaParameters) { var annotations = getAnnotations(javaParameter.getDeclaredAnnotations()); var parameterMetadata = ParameterMetadata.builder() .index(index++) - .annotation(annotations.size() > 0 ? annotations.get(0) : null) + .annotation(!annotations.isEmpty() ? annotations.get(0) : null) .build(); parameters.add(parameterMetadata); } @@ -123,11 +123,9 @@ private List getParameters(Parameter[] javaParameters) { private void validate(InterfaceMetadata interfaceMetadata) { interfaceMetadata.getMethodBySignature().forEach((methodSignature, methodMetadata) -> { - if (!methodMetadata.isDefault()) { - if (!methodMetadata.hasHttpAnnotation()) { - throw new CleverClientException("Missing HTTP annotation for the method {0}.", - methodMetadata.getName(), null); - } + if (!methodMetadata.isDefault() && !methodMetadata.hasHttpAnnotation()) { + throw new CleverClientException("Missing HTTP annotation for the method {0}.", + methodMetadata.getName(), null); } var url = interfaceMetadata.getFullUrlByMethod(methodMetadata); var listPathParams = CommonUtil.findFullMatches(url, Constant.REGEX_PATH_PARAM_URL); diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncStreamSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncStreamSender.java index e363cf2..6ee8a21 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncStreamSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncStreamSender.java @@ -20,12 +20,14 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, throwExceptionIfErrorIsPresent(response, Stream.class); - final var record = new CleverClientSSE.LineRecord(); + final var lineRecord = new CleverClientSSE.LineRecord(); return response.body() - .peek(line -> logger.debug("Response : {}", line)) - .peek(line -> record.updateWith(line)) - .map(line -> new CleverClientSSE(record)) + .peek(line -> { + logger.debug("Response : {}", line); + lineRecord.updateWith(line); + }) + .map(line -> new CleverClientSSE(lineRecord)) .filter(CleverClientSSE::isActualData) .map(item -> JsonUtil.jsonToObject(item.getActualData(), responseClass)); }); diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSender.java index e396bf9..45dd080 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSender.java @@ -57,7 +57,7 @@ protected void throwExceptionIfErrorIsPresent(HttpResponse response, Class try { data = new String(((InputStream) response.body()).readAllBytes(), StandardCharsets.UTF_8); } catch (IOException e) { - e.printStackTrace(); + logger.error("Cannot read input stream. {}", e.getMessage()); } } else { data = (String) response.body(); diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncStreamSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncStreamSender.java index 5c30f24..7b9d6e1 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncStreamSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncStreamSender.java @@ -21,12 +21,14 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, throwExceptionIfErrorIsPresent(httpResponse, Stream.class); - final var record = new CleverClientSSE.LineRecord(); + final var lineRecord = new CleverClientSSE.LineRecord(); return httpResponse.body() - .peek(line -> logger.debug("Response : {}", line)) - .peek(line -> record.updateWith(line)) - .map(line -> new CleverClientSSE(record)) + .peek(line -> { + logger.debug("Response : {}", line); + lineRecord.updateWith(line); + }) + .map(line -> new CleverClientSSE(lineRecord)) .filter(CleverClientSSE::isActualData) .map(item -> JsonUtil.jsonToObject(item.getActualData(), responseClass)); diff --git a/src/main/java/io/github/sashirestela/cleverclient/support/CleverClientSSE.java b/src/main/java/io/github/sashirestela/cleverclient/support/CleverClientSSE.java index 7b44c11..b82c993 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/support/CleverClientSSE.java +++ b/src/main/java/io/github/sashirestela/cleverclient/support/CleverClientSSE.java @@ -1,43 +1,32 @@ package io.github.sashirestela.cleverclient.support; import java.util.List; -import java.util.stream.Collectors; public class CleverClientSSE { - private static final String EVENT_HEADER = "event: "; private static final String DATA_HEADER = "data: "; - private static final String SEPARATOR = ""; - private static List linesToCheck = null; - - private LineRecord record; - private List eventsToRead; + private LineRecord lineRecord; private List endsOfStream; + private List linesToCheck; - public CleverClientSSE(LineRecord record) { - this.record = record; - this.eventsToRead = Configurator.one().getEventsToRead(); + public CleverClientSSE(LineRecord lineRecord) { + this.lineRecord = lineRecord; + this.linesToCheck = Configurator.one().getLinesToCheck(); this.endsOfStream = Configurator.one().getEndsOfStream(); - - if (linesToCheck == null) { - linesToCheck = this.eventsToRead.stream().filter(etr -> !etr.isEmpty()).map(etr -> (EVENT_HEADER + etr)) - .collect(Collectors.toList()); - linesToCheck.add(SEPARATOR); - } } - public LineRecord getRecord() { - return record; + public LineRecord getLineRecord() { + return lineRecord; } public boolean isActualData() { - return linesToCheck.contains(record.previous()) && record.current().startsWith(DATA_HEADER) - && endsOfStream.stream().anyMatch(eos -> !record.current().contains(eos)); + return linesToCheck.contains(lineRecord.previous()) && lineRecord.current().startsWith(DATA_HEADER) + && endsOfStream.stream().anyMatch(eos -> !lineRecord.current().contains(eos)); } public String getActualData() { - return record.current().replace(DATA_HEADER, "").strip(); + return lineRecord.current().replace(DATA_HEADER, "").strip(); } public static class LineRecord { diff --git a/src/main/java/io/github/sashirestela/cleverclient/support/Configurator.java b/src/main/java/io/github/sashirestela/cleverclient/support/Configurator.java index f5266d4..b6e9e48 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/support/Configurator.java +++ b/src/main/java/io/github/sashirestela/cleverclient/support/Configurator.java @@ -1,37 +1,55 @@ package io.github.sashirestela.cleverclient.support; import java.util.List; +import java.util.stream.Collectors; import lombok.Builder; import lombok.Getter; import lombok.Singular; -@Getter public class Configurator { - private static Configurator configurator; + private static final String EVENT_HEADER = "event: "; + private static final String SEPARATOR = ""; + private static Configurator configurator = new Configurator(); + + @Getter private List eventsToRead; + @Getter private List endsOfStream; + private List linesToCheck; + private boolean wasBuilt = false; + private Configurator() { } @Builder public Configurator(@Singular("eventToRead") List eventsToRead, @Singular("endOfStream") List endsOfStream) { - if (configurator != null) { + if (configurator.wasBuilt) { return; } - configurator = new Configurator(); configurator.eventsToRead = eventsToRead; configurator.endsOfStream = endsOfStream; + configurator.wasBuilt = true; } public static Configurator one() { - if (configurator == null) { + if (!configurator.wasBuilt) { throw new CleverClientException("You have to call Configurator.builder() first."); } return configurator; } + + public List getLinesToCheck() { + if (linesToCheck == null) { + linesToCheck = eventsToRead.stream().filter(etr -> !etr.isEmpty()).map(etr -> (EVENT_HEADER + etr)) + .collect(Collectors.toList()); + linesToCheck.add(SEPARATOR); + } + return linesToCheck; + } + } diff --git a/src/main/java/io/github/sashirestela/cleverclient/util/CommonUtil.java b/src/main/java/io/github/sashirestela/cleverclient/util/CommonUtil.java index ae302b8..9e287ef 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/util/CommonUtil.java +++ b/src/main/java/io/github/sashirestela/cleverclient/util/CommonUtil.java @@ -49,12 +49,12 @@ public static Map createMapString(String... keyValPairs) { throw new IllegalArgumentException("It is expected an even number of elements."); } Map map = new HashMap<>(); - for (var i = 0; i < keyValPairs.length; i++) { + for (var i = 0; i < keyValPairs.length; i += 2) { var key = keyValPairs[i]; if (key == null) { throw new IllegalArgumentException("Unexpected null element for key in position " + i + "."); } - var val = keyValPairs[++i]; + var val = keyValPairs[i + 1]; map.put(key, val); } return map; @@ -62,10 +62,10 @@ public static Map createMapString(String... keyValPairs) { public static List mapToListOfString(Map map) { List list = new ArrayList<>(); - map.entrySet().stream().peek(e -> { - list.add(e.getKey()); - list.add(e.getValue()); - }).collect(Collectors.counting()); + for (var entry : map.entrySet()) { + list.add(entry.getKey()); + list.add(entry.getValue()); + } return list; } diff --git a/src/main/java/io/github/sashirestela/cleverclient/util/Constant.java b/src/main/java/io/github/sashirestela/cleverclient/util/Constant.java index 0a21655..82c09c9 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/util/Constant.java +++ b/src/main/java/io/github/sashirestela/cleverclient/util/Constant.java @@ -1,13 +1,13 @@ package io.github.sashirestela.cleverclient.util; import java.math.BigInteger; -import java.util.Random; +import java.security.SecureRandom; public class Constant { private Constant() { } - public static final String BOUNDARY_VALUE = new BigInteger(256, new Random()).toString(); + public static final String BOUNDARY_VALUE = new BigInteger(256, new SecureRandom()).toString(); public static final String REGEX_PATH_PARAM_URL = "\\{(.*?)\\}"; diff --git a/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java b/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java index f347665..c67e45b 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java @@ -16,7 +16,7 @@ import io.github.sashirestela.cleverclient.support.CleverClientException; import io.github.sashirestela.cleverclient.support.ReturnType;; -public class InterfaceMetadataStoreTest { +class InterfaceMetadataStoreTest { InterfaceMetadataStore store = InterfaceMetadataStore.one(); diff --git a/src/test/java/io/github/sashirestela/cleverclient/support/CleverClientSSETest.java b/src/test/java/io/github/sashirestela/cleverclient/support/CleverClientSSETest.java index 3b027f4..662e56f 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/support/CleverClientSSETest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/support/CleverClientSSETest.java @@ -42,7 +42,7 @@ void shouldReturnExpectedValueWhenRawDataHasDifferentValues() { @SuppressWarnings("unused") void shouldReturnTheActualDataWhenRawDataMeetsConditions() { CleverClientSSE event = new CleverClientSSE(new LineRecord("event: process", "data: This is the actual data. ")); - var rawData = event.getRecord(); + var rawData = event.getLineRecord(); var actualData = event.getActualData(); var expectedData = "This is the actual data."; assertEquals(expectedData, actualData); diff --git a/src/test/java/io/github/sashirestela/cleverclient/util/UnixTimestampDeserializerTest.java b/src/test/java/io/github/sashirestela/cleverclient/util/UnixTimestampDeserializerTest.java index 67f97d5..86282f7 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/util/UnixTimestampDeserializerTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/util/UnixTimestampDeserializerTest.java @@ -14,7 +14,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; -public class UnixTimestampDeserializerTest { +class UnixTimestampDeserializerTest { @Test void testDeserialize() { From 30e76fb383024e3887ff12e9512a60bd6f5a0bb7 Mon Sep 17 00:00:00 2001 From: Sashir Estela Date: Tue, 19 Mar 2024 02:16:04 +0000 Subject: [PATCH 5/5] Full code reformatting --- .../cleverclient/example/BasicExample.java | 3 +- .../example/FileDownloadExample.java | 10 +-- .../cleverclient/example/HeaderExample.java | 5 +- .../example/MultiServiceExample.java | 3 +- .../cleverclient/example/StreamExample.java | 11 ++-- .../example/jsonplaceholder/Album.java | 4 +- .../example/jsonplaceholder/AlbumService.java | 4 +- .../example/jsonplaceholder/Post.java | 5 +- .../example/jsonplaceholder/PostService.java | 6 +- .../example/openai/ChatRequest.java | 8 ++- .../example/openai/ChatResponse.java | 8 ++- .../example/openai/ChatService.java | 8 +-- .../cleverclient/example/openai/Choice.java | 5 +- .../cleverclient/example/openai/Message.java | 4 +- .../cleverclient/example/openai/Usage.java | 5 +- .../cleverclient/CleverClient.java | 47 +++++++------- .../cleverclient/annotation/Body.java | 2 +- .../cleverclient/annotation/DELETE.java | 4 +- .../cleverclient/annotation/GET.java | 4 +- .../cleverclient/annotation/Header.java | 2 +- .../cleverclient/annotation/Headers.java | 2 +- .../cleverclient/annotation/HttpMethod.java | 2 +- .../cleverclient/annotation/Multipart.java | 2 +- .../cleverclient/annotation/PATCH.java | 4 +- .../cleverclient/annotation/POST.java | 4 +- .../cleverclient/annotation/PUT.java | 4 +- .../cleverclient/annotation/Path.java | 4 +- .../cleverclient/annotation/Query.java | 4 +- .../cleverclient/annotation/Resource.java | 4 +- .../cleverclient/http/HttpConnector.java | 33 +++++----- .../cleverclient/http/HttpProcessor.java | 61 +++++++++---------- .../cleverclient/http/HttpRequestData.java | 15 +++-- .../cleverclient/http/URLBuilder.java | 11 ++-- .../metadata/InterfaceMetadata.java | 26 +++++--- .../metadata/InterfaceMetadataStore.java | 29 +++++---- .../sender/HttpAsyncBinarySender.java | 2 +- .../sender/HttpAsyncGenericSender.java | 6 +- .../sender/HttpAsyncListSender.java | 6 +- .../sender/HttpAsyncObjectSender.java | 6 +- .../sender/HttpAsyncPlainTextSender.java | 2 +- .../sender/HttpAsyncStreamSender.java | 8 +-- .../cleverclient/sender/HttpSender.java | 24 ++++---- .../sender/HttpSenderFactory.java | 15 ++--- .../sender/HttpSyncBinarySender.java | 6 +- .../sender/HttpSyncGenericSender.java | 8 +-- .../sender/HttpSyncListSender.java | 8 +-- .../sender/HttpSyncObjectSender.java | 8 +-- .../sender/HttpSyncPlainTextSender.java | 6 +- .../sender/HttpSyncStreamSender.java | 10 +-- .../support/CleverClientException.java | 2 +- .../cleverclient/support/Configurator.java | 10 +-- .../cleverclient/support/ContentType.java | 2 + .../cleverclient/support/HttpMultipart.java | 8 ++- .../cleverclient/support/ReturnType.java | 4 +- .../cleverclient/util/CommonUtil.java | 4 +- .../cleverclient/util/Constant.java | 3 +- .../cleverclient/util/JsonUtil.java | 17 +++--- .../cleverclient/util/ReflectUtil.java | 4 +- .../util/UnixTimestampDeserializer.java | 3 +- .../cleverclient/CleverClientTest.java | 43 +++++++------ .../cleverclient/http/HttpProcessorTest.java | 22 +++---- .../sashirestela/cleverclient/http/ITest.java | 18 +++--- .../cleverclient/http/URLBuilderTest.java | 22 ++++--- .../metadata/InterfaceMetadataStoreTest.java | 20 +++--- .../support/CleverClientExceptionTest.java | 6 +- .../support/CleverClientSSETest.java | 11 ++-- .../cleverclient/support/ReturnTypeTest.java | 10 +-- .../cleverclient/util/CommonUtilTest.java | 7 ++- .../util/HttpRequestBodyTestUtility.java | 3 + .../cleverclient/util/JsonUtilTest.java | 24 ++++---- .../cleverclient/util/ReflectUtilTest.java | 16 +++-- .../util/UnixTimestampDeserializerTest.java | 17 +++--- 72 files changed, 416 insertions(+), 328 deletions(-) diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/BasicExample.java b/src/example/java/io/github/sashirestela/cleverclient/example/BasicExample.java index 6407feb..edc8995 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/BasicExample.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/BasicExample.java @@ -60,4 +60,5 @@ private static void showTitle(String title) { System.out.println(title); System.out.println("-".repeat(times)); } -} \ No newline at end of file + +} diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/FileDownloadExample.java b/src/example/java/io/github/sashirestela/cleverclient/example/FileDownloadExample.java index 42ded38..0a79c15 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/FileDownloadExample.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/FileDownloadExample.java @@ -1,19 +1,20 @@ package io.github.sashirestela.cleverclient.example; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; - import io.github.sashirestela.cleverclient.CleverClient; import io.github.sashirestela.cleverclient.annotation.GET; import io.github.sashirestela.cleverclient.annotation.Path; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + public class FileDownloadExample { static interface ImageService { @GET("/150/{id}") InputStream getImage(@Path("id") String id); + } public static void main(String[] args) throws IOException { @@ -29,4 +30,5 @@ public static void main(String[] args) throws IOException { file.write(binaryData.readAllBytes()); file.close(); } + } diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/HeaderExample.java b/src/example/java/io/github/sashirestela/cleverclient/example/HeaderExample.java index 3960a01..81dc9ab 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/HeaderExample.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/HeaderExample.java @@ -16,6 +16,7 @@ static interface ClassHeaderService { @GET("/headers") String getClassHeaders(); + } static interface MethodHeaderService { @@ -24,6 +25,7 @@ static interface MethodHeaderService { @Header(name = "Fourth-Header", value = "fourthValue") @Header(name = "Fith-Header", value = "fithValue") String getHeaders(); + } public static void main(String[] args) { @@ -40,4 +42,5 @@ public static void main(String[] args) { var methodHeaderService = cleverClient.create(MethodHeaderService.class); System.out.println(methodHeaderService.getHeaders()); } -} \ No newline at end of file + +} diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/MultiServiceExample.java b/src/example/java/io/github/sashirestela/cleverclient/example/MultiServiceExample.java index e16bbc4..d4a4de2 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/MultiServiceExample.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/MultiServiceExample.java @@ -38,4 +38,5 @@ private static void showTitle(String title) { System.out.println(title); System.out.println("-".repeat(times)); } -} \ No newline at end of file + +} diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/StreamExample.java b/src/example/java/io/github/sashirestela/cleverclient/example/StreamExample.java index cedf30c..f421f4f 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/StreamExample.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/StreamExample.java @@ -1,16 +1,16 @@ package io.github.sashirestela.cleverclient.example; -import java.util.Arrays; - import io.github.sashirestela.cleverclient.CleverClient; import io.github.sashirestela.cleverclient.example.openai.ChatRequest; import io.github.sashirestela.cleverclient.example.openai.ChatResponse; import io.github.sashirestela.cleverclient.example.openai.ChatService; import io.github.sashirestela.cleverclient.example.openai.Message; +import java.util.Arrays; + /** - * Before running this example you must have an OpenAI account and keep your Api - * Key in an environment variable called OPENAI_API_KEY. + * Before running this example you must have an OpenAI account and keep your Api Key in an + * environment variable called OPENAI_API_KEY. * * @see https://platform.openai.com/docs/api-reference/authentication */ @@ -59,4 +59,5 @@ private static void showTitle(String title) { System.out.println(title); System.out.println("-".repeat(times)); } -} \ No newline at end of file + +} diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/Album.java b/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/Album.java index 9f013f6..d9666d7 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/Album.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/Album.java @@ -10,7 +10,9 @@ @Builder @Data public class Album { + private Integer id; private String title; private Integer userId; -} \ No newline at end of file + +} diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/AlbumService.java b/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/AlbumService.java index fc8bd31..5c973cb 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/AlbumService.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/AlbumService.java @@ -1,7 +1,5 @@ package io.github.sashirestela.cleverclient.example.jsonplaceholder; -import java.util.List; - import io.github.sashirestela.cleverclient.annotation.Body; import io.github.sashirestela.cleverclient.annotation.DELETE; import io.github.sashirestela.cleverclient.annotation.GET; @@ -11,6 +9,8 @@ import io.github.sashirestela.cleverclient.annotation.Query; import io.github.sashirestela.cleverclient.annotation.Resource; +import java.util.List; + @Resource("/albums") public interface AlbumService { diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/Post.java b/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/Post.java index d636256..a15c0a3 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/Post.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/Post.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; - import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -14,8 +13,10 @@ @Data @JsonInclude(Include.NON_NULL) public class Post { + private Integer id; private String title; private String body; private Integer userId; -} \ No newline at end of file + +} diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/PostService.java b/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/PostService.java index 17acaab..44c3ae7 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/PostService.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/jsonplaceholder/PostService.java @@ -1,7 +1,5 @@ package io.github.sashirestela.cleverclient.example.jsonplaceholder; -import java.util.List; - import io.github.sashirestela.cleverclient.annotation.Body; import io.github.sashirestela.cleverclient.annotation.DELETE; import io.github.sashirestela.cleverclient.annotation.GET; @@ -12,6 +10,8 @@ import io.github.sashirestela.cleverclient.annotation.Query; import io.github.sashirestela.cleverclient.annotation.Resource; +import java.util.List; + @Resource("/posts") public interface PostService { @@ -33,4 +33,4 @@ public interface PostService { @DELETE("/{postId}") Post deletePost(@Path("postId") Integer postId); -} \ No newline at end of file +} diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/openai/ChatRequest.java b/src/example/java/io/github/sashirestela/cleverclient/example/openai/ChatRequest.java index b068a63..c6d865c 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/openai/ChatRequest.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/openai/ChatRequest.java @@ -1,17 +1,19 @@ package io.github.sashirestela.cleverclient.example.openai; -import java.util.List; - import lombok.Builder; import lombok.Getter; import lombok.With; +import java.util.List; + @Getter @Builder public class ChatRequest { + private String model; private List messages; private Double temperature; @With private Boolean stream; -} \ No newline at end of file + +} diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/openai/ChatResponse.java b/src/example/java/io/github/sashirestela/cleverclient/example/openai/ChatResponse.java index 147f2fa..217e7f5 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/openai/ChatResponse.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/openai/ChatResponse.java @@ -1,15 +1,16 @@ package io.github.sashirestela.cleverclient.example.openai; -import java.util.List; - import lombok.Getter; import lombok.NoArgsConstructor; import lombok.ToString; +import java.util.List; + @NoArgsConstructor @Getter @ToString public class ChatResponse { + private String id; private String object; private Long created; @@ -20,4 +21,5 @@ public class ChatResponse { public String firstContent() { return getChoices().get(0).getMessage().getContent(); } -} \ No newline at end of file + +} diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/openai/ChatService.java b/src/example/java/io/github/sashirestela/cleverclient/example/openai/ChatService.java index 6e3546b..5cd892b 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/openai/ChatService.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/openai/ChatService.java @@ -1,12 +1,12 @@ package io.github.sashirestela.cleverclient.example.openai; -import java.util.concurrent.CompletableFuture; -import java.util.stream.Stream; - import io.github.sashirestela.cleverclient.annotation.Body; import io.github.sashirestela.cleverclient.annotation.POST; import io.github.sashirestela.cleverclient.annotation.Resource; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Stream; + @Resource("/v1/chat/completions") public interface ChatService { @@ -26,4 +26,4 @@ default CompletableFuture> createAsyncStream(ChatRequest ch return __createAsyncStream(request); } -} \ No newline at end of file +} diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/openai/Choice.java b/src/example/java/io/github/sashirestela/cleverclient/example/openai/Choice.java index 8c6da59..714db63 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/openai/Choice.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/openai/Choice.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.annotation.JsonAlias; import com.fasterxml.jackson.annotation.JsonProperty; - import lombok.Getter; import lombok.NoArgsConstructor; import lombok.ToString; @@ -11,6 +10,7 @@ @Getter @ToString public class Choice { + private Integer index; @JsonAlias({ "delta" }) @@ -18,4 +18,5 @@ public class Choice { @JsonProperty("finish_reason") private String finishReason; -} \ No newline at end of file + +} diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/openai/Message.java b/src/example/java/io/github/sashirestela/cleverclient/example/openai/Message.java index 5a4a3dc..8f3f867 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/openai/Message.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/openai/Message.java @@ -10,6 +10,8 @@ @Getter @ToString public class Message { + private String role; private String content; -} \ No newline at end of file + +} diff --git a/src/example/java/io/github/sashirestela/cleverclient/example/openai/Usage.java b/src/example/java/io/github/sashirestela/cleverclient/example/openai/Usage.java index 75ad87c..45f1066 100644 --- a/src/example/java/io/github/sashirestela/cleverclient/example/openai/Usage.java +++ b/src/example/java/io/github/sashirestela/cleverclient/example/openai/Usage.java @@ -1,7 +1,6 @@ package io.github.sashirestela.cleverclient.example.openai; import com.fasterxml.jackson.annotation.JsonProperty; - import lombok.Getter; import lombok.NoArgsConstructor; import lombok.ToString; @@ -10,6 +9,7 @@ @Getter @ToString public class Usage { + @JsonProperty("prompt_tokens") private Integer promptTokens; @@ -18,4 +18,5 @@ public class Usage { @JsonProperty("total_tokens") private Integer totalTokens; -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java index e31d7e5..0c1cd77 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java +++ b/src/main/java/io/github/sashirestela/cleverclient/CleverClient.java @@ -1,15 +1,5 @@ package io.github.sashirestela.cleverclient; -import java.net.http.HttpClient; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.function.UnaryOperator; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import io.github.sashirestela.cleverclient.http.HttpProcessor; import io.github.sashirestela.cleverclient.http.HttpRequestData; import io.github.sashirestela.cleverclient.support.Configurator; @@ -18,14 +8,23 @@ import lombok.Getter; import lombok.NonNull; import lombok.Singular; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.http.HttpClient; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.UnaryOperator; /** - * Main class and entry point to use this library. This is a kind of wrapper - * that makes it easier to use the Java's HttpClient component to call http - * services by using annotated interfaces. + * Main class and entry point to use this library. This is a kind of wrapper that makes it easier to + * use the Java's HttpClient component to call http services by using annotated interfaces. */ @Getter public class CleverClient { + private static final Logger logger = LoggerFactory.getLogger(CleverClient.class); private final String baseUrl; @@ -37,15 +36,13 @@ public class CleverClient { /** * Constructor to create an instance of CleverClient. * - * @param baseUrl Root of the url of the API service to call. - * Mandatory. + * @param baseUrl Root of the url of the API service to call. Mandatory. * @param headers Http headers for all the API service. Optional. - * @param httpClient Custom Java's HttpClient component. One is created - * by default if none is passed. Optional. - * @param requestInterceptor Function to modify the request once it has been - * built. - * @param endOfStream Text used to mark the final of streams when - * handling server sent events (SSE). Optional. + * @param httpClient Custom Java's HttpClient component. One is created by default if none + * is passed. Optional. + * @param requestInterceptor Function to modify the request once it has been built. + * @param endOfStream Text used to mark the final of streams when handling server sent events + * (SSE). Optional. */ @Builder public CleverClient(@NonNull String baseUrl, @Singular Map headers, HttpClient httpClient, @@ -69,9 +66,8 @@ public CleverClient(@NonNull String baseUrl, @Singular Map heade } /** - * Creates an instance of an annotated interface that represents a resource of - * the API service and its methods represent the endpoints that we can call: - * Get, Post, Put, Patch, Delete. + * Creates an instance of an annotated interface that represents a resource of the API service and + * its methods represent the endpoints that we can call: Get, Post, Put, Patch, Delete. * * @param Type of the interface. * @param interfaceClass The interface to be instanced. @@ -80,4 +76,5 @@ public CleverClient(@NonNull String baseUrl, @Singular Map heade public T create(Class interfaceClass) { return this.httpProcessor.createProxy(interfaceClass); } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/Body.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/Body.java index 520eef9..284a417 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/Body.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/Body.java @@ -10,4 +10,4 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface Body { -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/DELETE.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/DELETE.java index cd23037..1e872fc 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/DELETE.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/DELETE.java @@ -11,5 +11,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface DELETE { + String value() default ""; -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/GET.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/GET.java index e663332..9e38018 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/GET.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/GET.java @@ -11,5 +11,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface GET { + String value() default ""; -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/Header.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/Header.java index 8b37653..42392b4 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/Header.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/Header.java @@ -17,4 +17,4 @@ String value(); -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/Headers.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/Headers.java index b714062..d031c2d 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/Headers.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/Headers.java @@ -13,4 +13,4 @@ Header[] value(); -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/HttpMethod.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/HttpMethod.java index cd7b317..d474985 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/HttpMethod.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/HttpMethod.java @@ -10,4 +10,4 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface HttpMethod { -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/Multipart.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/Multipart.java index ec8fb47..1d52930 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/Multipart.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/Multipart.java @@ -10,4 +10,4 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Multipart { -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/PATCH.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/PATCH.java index ffa46ed..0dcdaf0 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/PATCH.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/PATCH.java @@ -11,5 +11,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface PATCH { + String value() default ""; -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/POST.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/POST.java index a5fdc40..171f830 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/POST.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/POST.java @@ -11,5 +11,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface POST { + String value() default ""; -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/PUT.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/PUT.java index 2ba714a..6b94940 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/PUT.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/PUT.java @@ -11,5 +11,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface PUT { + String value() default ""; -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/Path.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/Path.java index 616c9ac..3492187 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/Path.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/Path.java @@ -10,5 +10,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface Path { + String value(); -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/Query.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/Query.java index 9dc16d3..93ef4a7 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/Query.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/Query.java @@ -10,5 +10,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface Query { + String value() default ""; -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/annotation/Resource.java b/src/main/java/io/github/sashirestela/cleverclient/annotation/Resource.java index 7bcae8b..9d66ae1 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/annotation/Resource.java +++ b/src/main/java/io/github/sashirestela/cleverclient/annotation/Resource.java @@ -10,5 +10,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Resource { + String value(); -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/http/HttpConnector.java b/src/main/java/io/github/sashirestela/cleverclient/http/HttpConnector.java index ccf4719..3586cd9 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/http/HttpConnector.java +++ b/src/main/java/io/github/sashirestela/cleverclient/http/HttpConnector.java @@ -1,5 +1,15 @@ package io.github.sashirestela.cleverclient.http; +import io.github.sashirestela.cleverclient.sender.HttpSenderFactory; +import io.github.sashirestela.cleverclient.support.ContentType; +import io.github.sashirestela.cleverclient.support.HttpMultipart; +import io.github.sashirestela.cleverclient.support.ReturnType; +import io.github.sashirestela.cleverclient.util.CommonUtil; +import lombok.AllArgsConstructor; +import lombok.Builder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; @@ -9,24 +19,14 @@ import java.util.Map; import java.util.function.UnaryOperator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import io.github.sashirestela.cleverclient.sender.HttpSenderFactory; -import io.github.sashirestela.cleverclient.support.ContentType; -import io.github.sashirestela.cleverclient.support.HttpMultipart; -import io.github.sashirestela.cleverclient.support.ReturnType; -import io.github.sashirestela.cleverclient.util.CommonUtil; -import lombok.AllArgsConstructor; -import lombok.Builder; - /** - * HttpConnector prepares the request and receives the response to/from the - * Java's HttpClient component. + * HttpConnector prepares the request and receives the response to/from the Java's HttpClient + * component. */ @AllArgsConstructor @Builder public class HttpConnector { + private static final Logger logger = LoggerFactory.getLogger(HttpConnector.class); private HttpClient httpClient; @@ -39,8 +39,8 @@ public class HttpConnector { private UnaryOperator requestInterceptor; /** - * Prepares the request to call Java's HttpClient and delegates it to a - * specialized HttpSender based on the method's return type. + * Prepares the request to call Java's HttpClient and delegates it to a specialized HttpSender based + * on the method's return type. * * @return The response coming from the HttpSender's sendRequest method. */ @@ -119,4 +119,5 @@ private String printHeaders(List headers) { print.append("}"); return print.toString(); } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java b/src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java index 914ece9..c99a03c 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java +++ b/src/main/java/io/github/sashirestela/cleverclient/http/HttpProcessor.java @@ -1,5 +1,14 @@ package io.github.sashirestela.cleverclient.http; +import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.MethodMetadata; +import io.github.sashirestela.cleverclient.metadata.InterfaceMetadataStore; +import io.github.sashirestela.cleverclient.support.ContentType; +import io.github.sashirestela.cleverclient.util.JsonUtil; +import io.github.sashirestela.cleverclient.util.ReflectUtil; +import lombok.Builder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.lang.reflect.InvocationHandler; @@ -9,21 +18,12 @@ import java.util.List; import java.util.function.UnaryOperator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.MethodMetadata; -import io.github.sashirestela.cleverclient.support.ContentType; -import io.github.sashirestela.cleverclient.metadata.InterfaceMetadataStore; -import io.github.sashirestela.cleverclient.util.JsonUtil; -import io.github.sashirestela.cleverclient.util.ReflectUtil; -import lombok.Builder; - /** * HttpProcessor orchestrates all the http interaction. */ @Builder public class HttpProcessor implements InvocationHandler { + private static final Logger logger = LoggerFactory.getLogger(HttpProcessor.class); private final String baseUrl; @@ -32,10 +32,9 @@ public class HttpProcessor implements InvocationHandler { private final UnaryOperator requestInterceptor; /** - * Creates a generic dynamic proxy with this HttpProcessor object acting as an - * InvocationHandler to resolve the requests arriving to the proxy. Previously, - * the interface metadata is collected and stored in memory to be used later and - * avoid to use Reflection calls. + * Creates a generic dynamic proxy with this HttpProcessor object acting as an InvocationHandler to + * resolve the requests arriving to the proxy. Previously, the interface metadata is collected and + * stored in memory to be used later and avoid to use Reflection calls. * * @param Type of the interface. * @param interfaceClass The interface to be instanced. @@ -49,16 +48,15 @@ public T createProxy(Class interfaceClass) { } /** - * Method automatically called whenever an interface's method is called. It - * handles default methods directly. Non-default methods are solved by calling - * HttpConnector. + * Method automatically called whenever an interface's method is called. It handles default methods + * directly. Non-default methods are solved by calling HttpConnector. * * @param proxy The proxy instance that the method was invoked on. - * @param method The Method instance corresponding to the interface method - * invoked on the proxy instance. - * @param arguments An array of objects containing the values of the arguments - * passed in the method invocation on the proxy instance, or - * null if interface method takes no arguments. + * @param method The Method instance corresponding to the interface method invoked on the proxy + * instance. + * @param arguments An array of objects containing the values of the arguments passed in the method + * invocation on the proxy instance, or null if interface method takes no + * arguments. * @return The value to return from the method invocation on the proxy instance. */ @Override @@ -83,15 +81,15 @@ public Object invoke(Object proxy, Method method, Object[] arguments) throws Thr } /** - * Reads the interface method metadata from memory and uses them to prepare an - * HttpConnector object that will resend the request to the Java's HttpClient - * and will receive the response. This method is called from the invoke method. + * Reads the interface method metadata from memory and uses them to prepare an HttpConnector object + * that will resend the request to the Java's HttpClient and will receive the response. This method + * is called from the invoke method. * - * @param method The Method instance corresponding to the interface method - * invoked on the proxy instance. - * @param arguments An array of objects containing the values of the arguments - * passed in the method invocation on the proxy instance, or - * null if interface method takes no arguments. + * @param method The Method instance corresponding to the interface method invoked on the proxy + * instance. + * @param arguments An array of objects containing the values of the arguments passed in the method + * invocation on the proxy instance, or null if interface method takes no + * arguments. * @return The response coming from the HttpConnector's sendRequest method. */ private Object resolve(Method method, Object[] arguments) { @@ -141,4 +139,5 @@ private List calculateHeaderContentType(ContentType contentType) { } return headerContentType; } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/http/HttpRequestData.java b/src/main/java/io/github/sashirestela/cleverclient/http/HttpRequestData.java index eb4808a..b895703 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/http/HttpRequestData.java +++ b/src/main/java/io/github/sashirestela/cleverclient/http/HttpRequestData.java @@ -1,18 +1,23 @@ package io.github.sashirestela.cleverclient.http; -import java.util.Map; - import io.github.sashirestela.cleverclient.support.ContentType; import lombok.Builder; import lombok.Getter; import lombok.Setter; +import java.util.Map; + @Builder @Getter public class HttpRequestData { - @Setter private String url; - @Setter private Object body; - @Setter private Map headers; + + @Setter + private String url; + @Setter + private Object body; + @Setter + private Map headers; private String httpMethod; private ContentType contentType; + } diff --git a/src/main/java/io/github/sashirestela/cleverclient/http/URLBuilder.java b/src/main/java/io/github/sashirestela/cleverclient/http/URLBuilder.java index 3e492df..47dda4f 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/http/URLBuilder.java +++ b/src/main/java/io/github/sashirestela/cleverclient/http/URLBuilder.java @@ -1,14 +1,14 @@ package io.github.sashirestela.cleverclient.http; +import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.MethodMetadata; +import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.ParameterMetadata; +import io.github.sashirestela.cleverclient.util.JsonUtil; + import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; -import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.MethodMetadata; -import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.ParameterMetadata; -import io.github.sashirestela.cleverclient.util.JsonUtil; - public class URLBuilder { private static URLBuilder urlBuilder = null; @@ -73,4 +73,5 @@ private void appendQueryParam(String name, Object value, StringBuilder queryPara .append(URLEncoder.encode(value.toString(), StandardCharsets.UTF_8)); } } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadata.java b/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadata.java index d1b200c..ca0d7da 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadata.java +++ b/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadata.java @@ -1,18 +1,19 @@ package io.github.sashirestela.cleverclient.metadata; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - import io.github.sashirestela.cleverclient.support.ContentType; import io.github.sashirestela.cleverclient.support.ReturnType; import lombok.Builder; import lombok.Value; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + @Value @Builder public class InterfaceMetadata { + private static final String ANNOT_RESOURCE = "Resource"; private static final String ANNOT_HEADER = "Header"; private static final String ANNOT_MULTIPART = "Multipart"; @@ -31,7 +32,8 @@ public String getFullUrlByMethod(MethodMetadata methodMetadata) { .filter(annot -> annot.getName().equals(ANNOT_RESOURCE)) .findFirst(); var resourceUrl = resourceAnnot.isPresent() ? resourceAnnot.get().getValue() : ""; - var httpMethodAnnot = methodMetadata.getAnnotations().stream() + var httpMethodAnnot = methodMetadata.getAnnotations() + .stream() .filter(AnnotationMetadata::isHttpMethod) .findFirst(); var httpMethodUrl = httpMethodAnnot.isPresent() ? httpMethodAnnot.get().getValue() : ""; @@ -42,7 +44,8 @@ public List getFullHeadersByMethod(MethodMetadata methodMetadata) { var fullHeaderAnnots = annotations.stream() .filter(annot -> annot.getName().equals(ANNOT_HEADER)) .collect(Collectors.toList()); - var methodHeaderAnnots = methodMetadata.getAnnotations().stream() + var methodHeaderAnnots = methodMetadata.getAnnotations() + .stream() .filter(annot -> annot.getName().equals(ANNOT_HEADER)) .collect(Collectors.toList()); fullHeaderAnnots.addAll(methodHeaderAnnots); @@ -57,6 +60,7 @@ public List getFullHeadersByMethod(MethodMetadata methodMetadata) { @Value @Builder public static class MethodMetadata { + String name; ReturnType returnType; boolean isDefault; @@ -110,18 +114,22 @@ private List getParametersFilteredBy(String annotationName) { .filter(param -> param.getAnnotation().getName().equals(annotationName)) .collect(Collectors.toList()); } + } @Value @Builder public static class ParameterMetadata { + int index; AnnotationMetadata annotation; + } @Value @Builder public static class AnnotationMetadata { + String name; boolean isHttpMethod; Map valueByField; @@ -129,5 +137,7 @@ public static class AnnotationMetadata { public String getValue() { return valueByField.get(ANNOT_FIELD_VALUE); } + } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java b/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java index 5f58791..e3d92a0 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java +++ b/src/main/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStore.java @@ -1,15 +1,5 @@ package io.github.sashirestela.cleverclient.metadata; -import java.lang.annotation.Annotation; -import java.lang.reflect.Parameter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import io.github.sashirestela.cleverclient.annotation.HttpMethod; import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.AnnotationMetadata; import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.MethodMetadata; @@ -18,8 +8,18 @@ import io.github.sashirestela.cleverclient.support.ReturnType; import io.github.sashirestela.cleverclient.util.CommonUtil; import io.github.sashirestela.cleverclient.util.Constant; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Parameter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class InterfaceMetadataStore { + private static Logger logger = LoggerFactory.getLogger(InterfaceMetadataStore.class); private static InterfaceMetadataStore store = null; @@ -130,13 +130,16 @@ private void validate(InterfaceMetadata interfaceMetadata) { var url = interfaceMetadata.getFullUrlByMethod(methodMetadata); var listPathParams = CommonUtil.findFullMatches(url, Constant.REGEX_PATH_PARAM_URL); if (!CommonUtil.isNullOrEmpty(listPathParams)) { - listPathParams.forEach(pathParam -> methodMetadata.getPathParameters().stream() + listPathParams.forEach(pathParam -> methodMetadata.getPathParameters() + .stream() .map(parameter -> parameter.getAnnotation().getValue()) - .filter(paramAnnotValue -> pathParam.equals(paramAnnotValue)).findFirst() + .filter(paramAnnotValue -> pathParam.equals(paramAnnotValue)) + .findFirst() .orElseThrow(() -> new CleverClientException( "Path param {0} in the url cannot find an annotated argument in the method {1}.", pathParam, methodMetadata.getName(), null))); } }); } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncBinarySender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncBinarySender.java index 7b8dd65..8046a4e 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncBinarySender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncBinarySender.java @@ -24,4 +24,4 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, }); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncGenericSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncGenericSender.java index afff47f..ffc7b84 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncGenericSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncGenericSender.java @@ -1,11 +1,11 @@ package io.github.sashirestela.cleverclient.sender; +import io.github.sashirestela.cleverclient.util.JsonUtil; + import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse.BodyHandlers; -import io.github.sashirestela.cleverclient.util.JsonUtil; - public class HttpAsyncGenericSender extends HttpSender { @Override @@ -24,4 +24,4 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, }); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncListSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncListSender.java index acff1d0..9bef7ee 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncListSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncListSender.java @@ -1,11 +1,11 @@ package io.github.sashirestela.cleverclient.sender; +import io.github.sashirestela.cleverclient.util.JsonUtil; + import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse.BodyHandlers; -import io.github.sashirestela.cleverclient.util.JsonUtil; - public class HttpAsyncListSender extends HttpSender { @Override @@ -24,4 +24,4 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, }); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncObjectSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncObjectSender.java index f0521f2..d80e3cb 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncObjectSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncObjectSender.java @@ -1,11 +1,11 @@ package io.github.sashirestela.cleverclient.sender; +import io.github.sashirestela.cleverclient.util.JsonUtil; + import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse.BodyHandlers; -import io.github.sashirestela.cleverclient.util.JsonUtil; - public class HttpAsyncObjectSender extends HttpSender { @Override @@ -24,4 +24,4 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, }); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncPlainTextSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncPlainTextSender.java index 90c125f..44bd44f 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncPlainTextSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncPlainTextSender.java @@ -23,4 +23,4 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, }); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncStreamSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncStreamSender.java index 6ee8a21..ddc4ad4 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncStreamSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpAsyncStreamSender.java @@ -1,13 +1,13 @@ package io.github.sashirestela.cleverclient.sender; +import io.github.sashirestela.cleverclient.support.CleverClientSSE; +import io.github.sashirestela.cleverclient.util.JsonUtil; + import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse.BodyHandlers; import java.util.stream.Stream; -import io.github.sashirestela.cleverclient.support.CleverClientSSE; -import io.github.sashirestela.cleverclient.util.JsonUtil; - public class HttpAsyncStreamSender extends HttpSender { @Override @@ -33,4 +33,4 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, }); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSender.java index 45dd080..3c87e57 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSender.java @@ -1,5 +1,10 @@ package io.github.sashirestela.cleverclient.sender; +import io.github.sashirestela.cleverclient.support.CleverClientException; +import io.github.sashirestela.cleverclient.util.CommonUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; @@ -10,23 +15,17 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import io.github.sashirestela.cleverclient.support.CleverClientException; -import io.github.sashirestela.cleverclient.util.CommonUtil; - /** - * HttpSender is an abstract class for a set of concrete classes that implement - * different interactions with the Java's HttpClient based on the method's - * return type. + * HttpSender is an abstract class for a set of concrete classes that implement different + * interactions with the Java's HttpClient based on the method's return type. */ public abstract class HttpSender { + protected static Logger logger = LoggerFactory.getLogger(HttpSender.class); /** - * Method to be implementd for concrete classes to send request to the Java's - * HttpClient and receive response. + * Method to be implementd for concrete classes to send request to the Java's HttpClient and receive + * response. * * @param Type of a generic class if exists. * @param Type of the response. @@ -66,4 +65,5 @@ protected void throwExceptionIfErrorIsPresent(HttpResponse response, Class throw new CleverClientException("ERROR : {0}", data, null); } } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSenderFactory.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSenderFactory.java index 42c577d..ab966d9 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSenderFactory.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSenderFactory.java @@ -1,19 +1,19 @@ package io.github.sashirestela.cleverclient.sender; -import java.util.EnumMap; -import java.util.function.Supplier; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import io.github.sashirestela.cleverclient.support.CleverClientException; import io.github.sashirestela.cleverclient.support.ReturnType; import io.github.sashirestela.cleverclient.support.ReturnType.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.EnumMap; +import java.util.function.Supplier; /** * Factory for the abstrac class HttpSender. */ public class HttpSenderFactory { + private static Logger logger = LoggerFactory.getLogger(HttpSenderFactory.class); private static HttpSenderFactory factory = null; @@ -60,4 +60,5 @@ public HttpSender createSender(ReturnType returnType) { } return sender; } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncBinarySender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncBinarySender.java index d44575a..71600e8 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncBinarySender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncBinarySender.java @@ -1,13 +1,13 @@ package io.github.sashirestela.cleverclient.sender; +import io.github.sashirestela.cleverclient.support.CleverClientException; + import java.io.IOException; import java.io.InputStream; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse.BodyHandlers; -import io.github.sashirestela.cleverclient.support.CleverClientException; - public class HttpSyncBinarySender extends HttpSender { @Override @@ -31,4 +31,4 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncGenericSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncGenericSender.java index a71edaf..b733031 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncGenericSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncGenericSender.java @@ -1,13 +1,13 @@ package io.github.sashirestela.cleverclient.sender; +import io.github.sashirestela.cleverclient.support.CleverClientException; +import io.github.sashirestela.cleverclient.util.JsonUtil; + import java.io.IOException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse.BodyHandlers; -import io.github.sashirestela.cleverclient.support.CleverClientException; -import io.github.sashirestela.cleverclient.util.JsonUtil; - public class HttpSyncGenericSender extends HttpSender { @Override @@ -31,4 +31,4 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncListSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncListSender.java index 1760985..b1bee1f 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncListSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncListSender.java @@ -1,13 +1,13 @@ package io.github.sashirestela.cleverclient.sender; +import io.github.sashirestela.cleverclient.support.CleverClientException; +import io.github.sashirestela.cleverclient.util.JsonUtil; + import java.io.IOException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse.BodyHandlers; -import io.github.sashirestela.cleverclient.support.CleverClientException; -import io.github.sashirestela.cleverclient.util.JsonUtil; - public class HttpSyncListSender extends HttpSender { @Override @@ -31,4 +31,4 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncObjectSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncObjectSender.java index d6a0195..9e9e934 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncObjectSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncObjectSender.java @@ -1,13 +1,13 @@ package io.github.sashirestela.cleverclient.sender; +import io.github.sashirestela.cleverclient.support.CleverClientException; +import io.github.sashirestela.cleverclient.util.JsonUtil; + import java.io.IOException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse.BodyHandlers; -import io.github.sashirestela.cleverclient.support.CleverClientException; -import io.github.sashirestela.cleverclient.util.JsonUtil; - public class HttpSyncObjectSender extends HttpSender { @Override @@ -31,4 +31,4 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncPlainTextSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncPlainTextSender.java index e5a516f..d87cdf8 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncPlainTextSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncPlainTextSender.java @@ -1,12 +1,12 @@ package io.github.sashirestela.cleverclient.sender; +import io.github.sashirestela.cleverclient.support.CleverClientException; + import java.io.IOException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse.BodyHandlers; -import io.github.sashirestela.cleverclient.support.CleverClientException; - public class HttpSyncPlainTextSender extends HttpSender { @Override @@ -30,4 +30,4 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncStreamSender.java b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncStreamSender.java index 7b9d6e1..9173513 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncStreamSender.java +++ b/src/main/java/io/github/sashirestela/cleverclient/sender/HttpSyncStreamSender.java @@ -1,15 +1,15 @@ package io.github.sashirestela.cleverclient.sender; +import io.github.sashirestela.cleverclient.support.CleverClientException; +import io.github.sashirestela.cleverclient.support.CleverClientSSE; +import io.github.sashirestela.cleverclient.util.JsonUtil; + import java.io.IOException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse.BodyHandlers; import java.util.stream.Stream; -import io.github.sashirestela.cleverclient.support.CleverClientException; -import io.github.sashirestela.cleverclient.support.CleverClientSSE; -import io.github.sashirestela.cleverclient.util.JsonUtil; - public class HttpSyncStreamSender extends HttpSender { @Override @@ -38,4 +38,4 @@ public Object sendRequest(HttpClient httpClient, HttpRequest httpRequest, } } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/support/CleverClientException.java b/src/main/java/io/github/sashirestela/cleverclient/support/CleverClientException.java index ca08458..7f4999b 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/support/CleverClientException.java +++ b/src/main/java/io/github/sashirestela/cleverclient/support/CleverClientException.java @@ -14,4 +14,4 @@ public CleverClientException(String message, Object... parameters) { (Throwable) parameters[parameters.length - 1]); } -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/support/Configurator.java b/src/main/java/io/github/sashirestela/cleverclient/support/Configurator.java index b6e9e48..65e9f77 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/support/Configurator.java +++ b/src/main/java/io/github/sashirestela/cleverclient/support/Configurator.java @@ -1,12 +1,12 @@ package io.github.sashirestela.cleverclient.support; -import java.util.List; -import java.util.stream.Collectors; - import lombok.Builder; import lombok.Getter; import lombok.Singular; +import java.util.List; +import java.util.stream.Collectors; + public class Configurator { private static final String EVENT_HEADER = "event: "; @@ -45,7 +45,9 @@ public static Configurator one() { public List getLinesToCheck() { if (linesToCheck == null) { - linesToCheck = eventsToRead.stream().filter(etr -> !etr.isEmpty()).map(etr -> (EVENT_HEADER + etr)) + linesToCheck = eventsToRead.stream() + .filter(etr -> !etr.isEmpty()) + .map(etr -> (EVENT_HEADER + etr)) .collect(Collectors.toList()); linesToCheck.add(SEPARATOR); } diff --git a/src/main/java/io/github/sashirestela/cleverclient/support/ContentType.java b/src/main/java/io/github/sashirestela/cleverclient/support/ContentType.java index c8c735e..9b5c95b 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/support/ContentType.java +++ b/src/main/java/io/github/sashirestela/cleverclient/support/ContentType.java @@ -3,6 +3,7 @@ import io.github.sashirestela.cleverclient.util.Constant; public enum ContentType { + MULTIPART_FORMDATA( "multipart/form-data", "; boundary=\"" + Constant.BOUNDARY_VALUE + "\""), @@ -25,4 +26,5 @@ public String getMimeType() { public String getDetails() { return this.details; } + } diff --git a/src/main/java/io/github/sashirestela/cleverclient/support/HttpMultipart.java b/src/main/java/io/github/sashirestela/cleverclient/support/HttpMultipart.java index 885d3c0..add7c35 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/support/HttpMultipart.java +++ b/src/main/java/io/github/sashirestela/cleverclient/support/HttpMultipart.java @@ -1,5 +1,7 @@ package io.github.sashirestela.cleverclient.support; +import io.github.sashirestela.cleverclient.util.Constant; + import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -9,9 +11,8 @@ import java.util.List; import java.util.Map; -import io.github.sashirestela.cleverclient.util.Constant; - public class HttpMultipart { + private static final String DASH = "--"; private static final String DQ = "\""; private static final String NL = "\r\n"; @@ -66,4 +67,5 @@ private static boolean isFile(Object value) { return value instanceof String && ((String) value).startsWith(FILE_PROTOCOL); } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/support/ReturnType.java b/src/main/java/io/github/sashirestela/cleverclient/support/ReturnType.java index 5237ae1..3b1961f 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/support/ReturnType.java +++ b/src/main/java/io/github/sashirestela/cleverclient/support/ReturnType.java @@ -3,6 +3,7 @@ import java.lang.reflect.Method; public class ReturnType { + private static final String ASYNC = "java.util.concurrent.CompletableFuture"; private static final String STREAM = "java.util.stream.Stream"; private static final String LIST = "java.util.List"; @@ -153,4 +154,5 @@ public enum Category { SYNC_BINARY, SYNC_PLAIN_TEXT; } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/util/CommonUtil.java b/src/main/java/io/github/sashirestela/cleverclient/util/CommonUtil.java index 9e287ef..e9f124b 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/util/CommonUtil.java +++ b/src/main/java/io/github/sashirestela/cleverclient/util/CommonUtil.java @@ -11,6 +11,7 @@ import java.util.stream.Collectors; public class CommonUtil { + private CommonUtil() { } @@ -73,4 +74,5 @@ public static Map listToMapOfString(List list) { var array = list.toArray(new String[0]); return createMapString(array); } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/util/Constant.java b/src/main/java/io/github/sashirestela/cleverclient/util/Constant.java index 82c09c9..f589cae 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/util/Constant.java +++ b/src/main/java/io/github/sashirestela/cleverclient/util/Constant.java @@ -7,8 +7,9 @@ public class Constant { private Constant() { } + public static final String BOUNDARY_VALUE = new BigInteger(256, new SecureRandom()).toString(); public static final String REGEX_PATH_PARAM_URL = "\\{(.*?)\\}"; -} \ No newline at end of file +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/util/JsonUtil.java b/src/main/java/io/github/sashirestela/cleverclient/util/JsonUtil.java index edfa185..6bff2da 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/util/JsonUtil.java +++ b/src/main/java/io/github/sashirestela/cleverclient/util/JsonUtil.java @@ -1,10 +1,5 @@ package io.github.sashirestela.cleverclient.util; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -12,9 +7,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.type.CollectionType; - import io.github.sashirestela.cleverclient.support.CleverClientException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + public class JsonUtil { private static final ObjectMapper objectMapperStrict = new ObjectMapper(); @@ -27,7 +26,8 @@ private JsonUtil() { public static Map objectToMap(T object) { try { - return objectMapperStrict.convertValue(object, new TypeReference<>() {}); + return objectMapperStrict.convertValue(object, new TypeReference<>() { + }); } catch (IllegalArgumentException e) { throw new CleverClientException("Cannot convert object {0} to Map.", object, e); } @@ -68,4 +68,5 @@ public static T jsonToParametricObject(String json, Class clazzT, Clas throw new CleverClientException("Cannot convert the Json {0} to class of {1}.", json, clazzT.getName(), e); } } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/util/ReflectUtil.java b/src/main/java/io/github/sashirestela/cleverclient/util/ReflectUtil.java index 14924f0..3f5fdc8 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/util/ReflectUtil.java +++ b/src/main/java/io/github/sashirestela/cleverclient/util/ReflectUtil.java @@ -4,6 +4,7 @@ import java.lang.reflect.Proxy; public class ReflectUtil { + private ReflectUtil() { } @@ -14,4 +15,5 @@ public static T createProxy(Class interfaceClass, InvocationHandler handl new Class[] { interfaceClass }, handler); } -} \ No newline at end of file + +} diff --git a/src/main/java/io/github/sashirestela/cleverclient/util/UnixTimestampDeserializer.java b/src/main/java/io/github/sashirestela/cleverclient/util/UnixTimestampDeserializer.java index 42b54fe..49a5726 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/util/UnixTimestampDeserializer.java +++ b/src/main/java/io/github/sashirestela/cleverclient/util/UnixTimestampDeserializer.java @@ -16,4 +16,5 @@ public ZonedDateTime deserialize(JsonParser jp, DeserializationContext ctx) thro long timestamp = jp.getLongValue(); return ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault()); } -} \ No newline at end of file + +} diff --git a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java index 957d422..66c868a 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java @@ -1,25 +1,5 @@ package io.github.sashirestela.cleverclient; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.function.UnaryOperator; - -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; - import io.github.sashirestela.cleverclient.annotation.Body; import io.github.sashirestela.cleverclient.annotation.GET; import io.github.sashirestela.cleverclient.annotation.Query; @@ -29,6 +9,25 @@ import io.github.sashirestela.cleverclient.util.HttpRequestBodyTestUtility; import lombok.Builder; import lombok.Value; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.function.UnaryOperator; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; class CleverClientTest { @@ -126,9 +125,11 @@ void shouldModifyRequestWhenPassingInterceptorFunction() { @Value @Builder static class Sample { + String id; String model; String description; + } @Resource("/v1.2/api") @@ -136,5 +137,7 @@ interface TestCleverClient { @GET("/text") CompletableFuture getText(@Body Sample sample, @Query("prefix") String prefix); + } + } diff --git a/src/test/java/io/github/sashirestela/cleverclient/http/HttpProcessorTest.java b/src/test/java/io/github/sashirestela/cleverclient/http/HttpProcessorTest.java index 230676f..f515b79 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/http/HttpProcessorTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/http/HttpProcessorTest.java @@ -1,11 +1,8 @@ package io.github.sashirestela.cleverclient.http; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import io.github.sashirestela.cleverclient.support.CleverClientException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.File; @@ -23,10 +20,12 @@ import java.util.concurrent.CompletionException; import java.util.stream.Stream; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import io.github.sashirestela.cleverclient.support.CleverClientException; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @SuppressWarnings("unchecked") class HttpProcessorTest { @@ -358,4 +357,5 @@ void shouldExecuteDefaultMethodWhenItIsCalled() { assertEquals(expectedValue, actualValue); } -} \ No newline at end of file + +} diff --git a/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java b/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java index e37c591..a926dae 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/http/ITest.java @@ -1,11 +1,5 @@ package io.github.sashirestela.cleverclient.http; -import java.io.InputStream; -import java.util.List; -import java.util.Set; -import java.util.concurrent.CompletableFuture; -import java.util.stream.Stream; - import io.github.sashirestela.cleverclient.annotation.Body; import io.github.sashirestela.cleverclient.annotation.GET; import io.github.sashirestela.cleverclient.annotation.Header; @@ -20,6 +14,12 @@ import lombok.NoArgsConstructor; import lombok.ToString; +import java.io.InputStream; +import java.util.List; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Stream; + public interface ITest { interface NotAnnotatedService { @@ -89,6 +89,7 @@ interface AsyncService { default String defaultMethod(String name) { return "Hello " + name; } + } @Resource("/demos") @@ -112,6 +113,7 @@ interface SyncService { @POST Stream getDemoStream(@Body RequestDemo request); + } interface NotSavedService { @@ -155,5 +157,7 @@ static class Generic { private Integer id; private List listDemo; + } -} \ No newline at end of file + +} diff --git a/src/test/java/io/github/sashirestela/cleverclient/http/URLBuilderTest.java b/src/test/java/io/github/sashirestela/cleverclient/http/URLBuilderTest.java index 45581af..7efab61 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/http/URLBuilderTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/http/URLBuilderTest.java @@ -1,19 +1,18 @@ package io.github.sashirestela.cleverclient.http; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.List; -import java.util.Map; - -import org.junit.jupiter.api.Test; - import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.AnnotationMetadata; import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.MethodMetadata; import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.ParameterMetadata; import lombok.AllArgsConstructor; import lombok.Data; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class URLBuilderTest { @@ -130,7 +129,10 @@ void shouldReturnReplacedUrlWithQueryParamsWhenMethodContainsQueryParamsForPojos @Data @AllArgsConstructor static class Pagination { + private Integer size; private Integer page; + } -} \ No newline at end of file + +} diff --git a/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java b/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java index c67e45b..d2f1dd9 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/metadata/InterfaceMetadataStoreTest.java @@ -1,20 +1,19 @@ package io.github.sashirestela.cleverclient.metadata; -import static io.github.sashirestela.cleverclient.util.CommonUtil.createMapString; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.util.Arrays; -import java.util.HashMap; - -import org.junit.jupiter.api.Test; - import io.github.sashirestela.cleverclient.http.ITest; import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.AnnotationMetadata; import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.MethodMetadata; import io.github.sashirestela.cleverclient.metadata.InterfaceMetadata.ParameterMetadata; import io.github.sashirestela.cleverclient.support.CleverClientException; -import io.github.sashirestela.cleverclient.support.ReturnType;; +import io.github.sashirestela.cleverclient.support.ReturnType; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.HashMap; + +import static io.github.sashirestela.cleverclient.util.CommonUtil.createMapString; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; class InterfaceMetadataStoreTest { @@ -151,4 +150,5 @@ void shouldThrownExceptionWhenUrlPathParamAtMethodUnmatchesAnnotatedArguments() "Path param demoId in the url cannot find an annotated argument in the method unmatchedPathParamMethod.", exception.getMessage()); } + } diff --git a/src/test/java/io/github/sashirestela/cleverclient/support/CleverClientExceptionTest.java b/src/test/java/io/github/sashirestela/cleverclient/support/CleverClientExceptionTest.java index 241dde3..95c0060 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/support/CleverClientExceptionTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/support/CleverClientExceptionTest.java @@ -1,10 +1,10 @@ package io.github.sashirestela.cleverclient.support; +import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import org.junit.jupiter.api.Test; - class CleverClientExceptionTest { @Test @@ -21,4 +21,4 @@ void shouldSetCauseInErrorWhenItIsPassedAsLastArgumentAtExceptionCreation() { assertNotNull(exception.getCause()); } -} \ No newline at end of file +} diff --git a/src/test/java/io/github/sashirestela/cleverclient/support/CleverClientSSETest.java b/src/test/java/io/github/sashirestela/cleverclient/support/CleverClientSSETest.java index 662e56f..46bb40c 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/support/CleverClientSSETest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/support/CleverClientSSETest.java @@ -1,11 +1,10 @@ package io.github.sashirestela.cleverclient.support; -import static org.junit.jupiter.api.Assertions.assertEquals; - +import io.github.sashirestela.cleverclient.support.CleverClientSSE.LineRecord; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import io.github.sashirestela.cleverclient.support.CleverClientSSE.LineRecord; +import static org.junit.jupiter.api.Assertions.assertEquals; class CleverClientSSETest { @@ -41,10 +40,12 @@ void shouldReturnExpectedValueWhenRawDataHasDifferentValues() { @Test @SuppressWarnings("unused") void shouldReturnTheActualDataWhenRawDataMeetsConditions() { - CleverClientSSE event = new CleverClientSSE(new LineRecord("event: process", "data: This is the actual data. ")); + CleverClientSSE event = new CleverClientSSE( + new LineRecord("event: process", "data: This is the actual data. ")); var rawData = event.getLineRecord(); var actualData = event.getActualData(); var expectedData = "This is the actual data."; assertEquals(expectedData, actualData); } -} \ No newline at end of file + +} diff --git a/src/test/java/io/github/sashirestela/cleverclient/support/ReturnTypeTest.java b/src/test/java/io/github/sashirestela/cleverclient/support/ReturnTypeTest.java index a90e84e..2a0c347 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/support/ReturnTypeTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/support/ReturnTypeTest.java @@ -1,7 +1,6 @@ package io.github.sashirestela.cleverclient.support; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import org.junit.jupiter.api.Test; import java.io.InputStream; import java.util.List; @@ -10,7 +9,8 @@ import java.util.concurrent.CompletableFuture; import java.util.stream.Stream; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; class ReturnTypeTest { @@ -86,9 +86,11 @@ static interface TestInterface { String syncStringMethod(); Set syncSetMethod(); + } static interface Generic { } -} \ No newline at end of file + +} diff --git a/src/test/java/io/github/sashirestela/cleverclient/util/CommonUtilTest.java b/src/test/java/io/github/sashirestela/cleverclient/util/CommonUtilTest.java index b318fec..cc6659c 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/util/CommonUtilTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/util/CommonUtilTest.java @@ -1,5 +1,7 @@ package io.github.sashirestela.cleverclient.util; +import org.junit.jupiter.api.Test; + import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -7,8 +9,6 @@ import java.util.List; import java.util.Map; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -154,4 +154,5 @@ void shouldConvertListToMapOfStringWhenAListIsPassed() { Map actualMap = CommonUtil.listToMapOfString(list); assertEquals(expectedMap, actualMap); } -} \ No newline at end of file + +} diff --git a/src/test/java/io/github/sashirestela/cleverclient/util/HttpRequestBodyTestUtility.java b/src/test/java/io/github/sashirestela/cleverclient/util/HttpRequestBodyTestUtility.java index 7ec8c44..557dabe 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/util/HttpRequestBodyTestUtility.java +++ b/src/test/java/io/github/sashirestela/cleverclient/util/HttpRequestBodyTestUtility.java @@ -19,6 +19,7 @@ public static String extractBody(HttpRequest httpRequest) { } static final class StringSubscriber implements Flow.Subscriber { + final HttpResponse.BodySubscriber wrapped; StringSubscriber(HttpResponse.BodySubscriber wrapped) { @@ -44,5 +45,7 @@ public void onError(Throwable throwable) { public void onComplete() { wrapped.onComplete(); } + } + } diff --git a/src/test/java/io/github/sashirestela/cleverclient/util/JsonUtilTest.java b/src/test/java/io/github/sashirestela/cleverclient/util/JsonUtilTest.java index 6d3fae3..961375a 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/util/JsonUtilTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/util/JsonUtilTest.java @@ -1,22 +1,20 @@ package io.github.sashirestela.cleverclient.util; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import org.junit.jupiter.api.Test; - import com.fasterxml.jackson.annotation.JsonProperty; - import io.github.sashirestela.cleverclient.support.CleverClientException; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.experimental.SuperBuilder; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; class JsonUtilTest { @@ -167,12 +165,14 @@ static enum TestEnum { @SuperBuilder @Getter static class SuperTestClass { + @JsonProperty("integer") protected Integer integerField; @JsonProperty("string") protected String stringField; @JsonProperty("real") protected Double doubleField; + } @NoArgsConstructor @@ -181,9 +181,11 @@ static class SuperTestClass { @Getter @Setter static class ChildTestClass extends SuperTestClass { + private Integer property; @JsonProperty("enumerator") private TestEnum testEnumField; + } @NoArgsConstructor @@ -197,4 +199,4 @@ static class FailClass { } -} \ No newline at end of file +} diff --git a/src/test/java/io/github/sashirestela/cleverclient/util/ReflectUtilTest.java b/src/test/java/io/github/sashirestela/cleverclient/util/ReflectUtilTest.java index e1ca934..af5c259 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/util/ReflectUtilTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/util/ReflectUtilTest.java @@ -1,24 +1,25 @@ package io.github.sashirestela.cleverclient.util; -import static org.junit.jupiter.api.Assertions.assertEquals; +import io.github.sashirestela.cleverclient.annotation.GET; +import io.github.sashirestela.cleverclient.annotation.Path; +import org.junit.jupiter.api.Test; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; -import org.junit.jupiter.api.Test; - -import io.github.sashirestela.cleverclient.annotation.GET; -import io.github.sashirestela.cleverclient.annotation.Path; +import static org.junit.jupiter.api.Assertions.assertEquals; class ReflectUtilTest { @Test void shouldExecuteHandlerWhenInterfaceMethodIsCalled() { TestInterface test = ReflectUtil.createProxy(TestInterface.class, new InvocationHandler() { + @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return "Text from handler."; } + }); String actualValue = test.testMethod("example"); String expectedValue = "Text from handler."; @@ -26,7 +27,10 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl } static interface TestInterface { + @GET("/api/test/url") String testMethod(@Path("arg") String argument); + } -} \ No newline at end of file + +} diff --git a/src/test/java/io/github/sashirestela/cleverclient/util/UnixTimestampDeserializerTest.java b/src/test/java/io/github/sashirestela/cleverclient/util/UnixTimestampDeserializerTest.java index 86282f7..1af7a58 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/util/UnixTimestampDeserializerTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/util/UnixTimestampDeserializerTest.java @@ -1,18 +1,16 @@ package io.github.sashirestela.cleverclient.util; -import static org.junit.jupiter.api.Assertions.assertEquals; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.junit.jupiter.api.Test; import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; -import org.junit.jupiter.api.Test; - -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; - -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; +import static org.junit.jupiter.api.Assertions.assertEquals; class UnixTimestampDeserializerTest { @@ -37,4 +35,5 @@ static class SampleClass { private ZonedDateTime time; } -} \ No newline at end of file + +}