Skip to content

Commit

Permalink
Merge pull request #1 from clean-arch-enablers-project/migrating-code
Browse files Browse the repository at this point in the history
Migrating code
  • Loading branch information
zeluciojr authored Apr 2, 2024
2 parents cc5a5ea + 8bc3fe4 commit b202e06
Show file tree
Hide file tree
Showing 57 changed files with 1,971 additions and 1 deletion.
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

settings.xml
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Expand Down Expand Up @@ -198,4 +199,4 @@
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
120 changes: 120 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>CAE HTTP Client</name>
<description>Meant for enabling applications to use HTTP at ease, without having to compromise the clean state of the overall architecture.</description>
<url>https://github.com/clean-arch-enablers-project/cae-utils-http-client/blob/main/README.md</url>
<groupId>com.clean-arch-enablers</groupId>
<artifactId>http-client</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<scm>
<url>https://github.com/clean-arch-enablers-project/cae-utils-http-client</url>
</scm>
<developers>
<developer>
<name>Zé Lúcio Jr.</name>
<email>joselucioalmeidajunior@gmail.com</email>
<url>https://github.com/zeluciojr</url>
</developer>
</developers>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.4.0</version>
<extensions>true</extensions>
<configuration>
<publishingServerId>central</publishingServerId>
<tokenAuth>true</tokenAuth>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.clean_arch_enablers.http_client;

@FunctionalInterface
public interface ExceptionHandler {

/**
* Method which will be called when handling an exception
* @param exception the exception to handle
*/
void handle(Exception exception);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.clean_arch_enablers.http_client;

import com.clean_arch_enablers.http_client.commons.HandlersFactory;

import java.util.Map;

public interface HttpExceptionHandlersByExceptionTypeFactory extends HandlersFactory<Map<Class<? extends Exception>, ExceptionHandler>> { }



Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.clean_arch_enablers.http_client;

public interface HttpRequestBuilder extends HttpRequestBuilderForHandlers, HttpRequestBuilderForRetrying{

HttpRequestBuilder headerOf(String key, String value);
HttpRequestBuilder headersFactory(HttpRequestHeaderFactory httpRequestHeaderFactory);
HttpRequestBuilder pathVariableOf(String pathVariablePlaceholder, String pathVariableValue);
HttpRequestBuilder queryParameterOf(String queryParameterName, String queryParameterValue);
HttpRequestBuilder proxyAddress(String host, Integer port);
HttpRequestModel finishBuildingModel();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.clean_arch_enablers.http_client;

/**
* Builder component for attaching handlers to the request model.
*/
public interface HttpRequestBuilderForHandlers {


/**
* Attaches a handler by the number which represents the HTTP status code
* @param statusCode the number of the HTTP status code: 404, 400, 503...
* @param httpResponseHandler an arrow function which implements the Functional Interface of HttpResponseHandler
* @return the builder instance for further attachments
*/
HttpRequestBuilder handlerByHttpStatusCode(Integer statusCode, HttpResponseHandler httpResponseHandler);

/**
* Attaches a group of handlers by the numbers which represent HTTP statuses code from within the factory
* @param httpResponseHandlersByStatusCodeFactory the factory from which the handlers will be made
* @return the builder instance for further attachments
*/
HttpRequestBuilder handlersByHttpStatusCodeFactory(HttpResponseHandlersByStatusCodeFactory httpResponseHandlersByStatusCodeFactory);

/**
* Attaches a handler for any unsuccessful HTTP response
* @param httpResponseHandler an arrow function which implements the Functional Interface of HttpResponseHandler
* @return the builder instance for further attachments
*/
HttpRequestBuilder handlerForAnyUnsuccessfulResponse(HttpResponseHandler httpResponseHandler);

/**
* Attaches a group of handlers by types of exceptions from within the factory
* @param httpExceptionHandlersByExceptionTypeFactory the factory from which the handlers will be made
* @return the builder instance for further attachments
*/
HttpRequestBuilder handlersByExceptionTypeFactory(HttpExceptionHandlersByExceptionTypeFactory httpExceptionHandlersByExceptionTypeFactory);

/**
* Attaches a handler by type of exception
* @param exceptionType the type of the exception which will trigger the execution of the following handler
* @param exceptionHandler an arrow function which implements the Functional Interface of ExceptionHandler
* @return the builder instance for further attachments
*/
HttpRequestBuilder handlerByExceptionType(Class<? extends Exception> exceptionType, ExceptionHandler exceptionHandler);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.clean_arch_enablers.http_client;

import com.clean_arch_enablers.http_client.commons.RetriersByExceptionTypeFactory;

public interface HttpRequestBuilderForRetrying {

HttpRequestBuilder retrierByHttpStatusCode(Integer statusCode, RetrierModel retrierModel);
HttpRequestBuilder retriersByHttpStatusCodeFactory(RetriersByStatusCodeFactory retriersByStatusCodeFactory);
HttpRequestBuilder retriersByExceptionTypeFactory(RetriersByExceptionTypeFactory retriersByExceptionTypeFactory);
HttpRequestBuilder retrierByExceptionType(Class<? extends Exception> exceptionType, RetrierModel retrierModel);

}




Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.clean_arch_enablers.http_client;

import java.util.Map;

/**
* Factory for HTTP Request Headers
*/
public interface HttpRequestHeaderFactory {

/**
* This method will be called under the hood when an instance of this type is passed during the building process of a HttpRequestModel instance.
* @return a map in which the keys are the HTTP Headers and the values are their respective HTTP Header Values
*/
Map<String, String> makeHeaders();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.clean_arch_enablers.http_client;


import com.clean_arch_enablers.http_client.implementations.AbstractHttpRequestModel;
import com.clean_arch_enablers.http_client.implementations.exceptions.RetryNeededOnExceptionThrownException;

public interface HttpRequestMethod {

HttpResponse execute(AbstractHttpRequestModel httpRequestModel) throws RetryNeededOnExceptionThrownException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.clean_arch_enablers.http_client;

import com.fasterxml.jackson.core.type.TypeReference;

public interface HttpRequestModel {

<T> T sendRequestReturning(Class<T> typeOfExpectedResponseBody);

<T> T sendRequestReturning(TypeReference<T> typeReference);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.clean_arch_enablers.http_client;

import java.net.http.HttpRequest;

public interface HttpRequestStarter {

HttpRequestBuilder startGetRequestFor(String url);
HttpRequestBuilder startPostRequestFor(String url, HttpRequest.BodyPublisher body);
HttpRequestBuilder startPutRequestFor(String url, HttpRequest.BodyPublisher body);
HttpRequestBuilder startDeleteRequestFor(String url);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.clean_arch_enablers.http_client;

import com.fasterxml.jackson.core.type.TypeReference;

import java.util.function.Consumer;

public interface HttpResponse{

/**
* Retrieve the status code of the response
* @return a number of some HTTP Status Code: 200, 204, 400, 403...
*/
Integer getStatusCode();

/**
* Gets the request responsible for generating this response
* @return the HTTP request model object
*/
HttpRequestModel getHttpRequest();

/**
* Gets the response body as the parameterized type
* @param bodyType the desired type for the body
* @return the instance of the desired type
* @param <T> the known type to map the body to
*/
<T> T getBodyAs(Class<T> bodyType);

/**
* Gets the response body as the parameterized type
* @param typeOfExpectedResponseBody the desired type for the body
* @return the instance of the desired type
* @param <T> the known type to map the body to
*/
<T> T getBodyAs(TypeReference<T> typeOfExpectedResponseBody);

/**
*
* @return whether the response needs handling
*/
boolean needsHandling();

/**
* If the instance needs handling it will execute the Consumer received as parameter
* @param checkOnResponse the action to be executed in case of needing handling
*/
void ifNeedsHandling(Consumer<HttpResponse> checkOnResponse);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.clean_arch_enablers.http_client;

@FunctionalInterface
public interface HttpResponseHandler {

/**
* Method called when handling an HTTP Response
* @param httpResponse the HTTP response to be handled
*/
void handle(HttpResponse httpResponse);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.clean_arch_enablers.http_client;

import com.clean_arch_enablers.http_client.commons.HandlersFactory;

import java.util.Map;

public interface HttpResponseHandlersByStatusCodeFactory extends HandlersFactory<Map<Integer, HttpResponseHandler>> { }
Loading

0 comments on commit b202e06

Please sign in to comment.