Skip to content

Latest commit

 

History

History
109 lines (74 loc) · 3.2 KB

README.md

File metadata and controls

109 lines (74 loc) · 3.2 KB

HTTP Common

GraalVM Ready Java CI Quality Gate Status Coverage Maintainability Rating

Small library with HTTP common components like HttpStatus, MediaType, URIBuilder, etc.

Have no dependencies.

Dependency 🚀

Java 17+ support.

Gradle

implementation "io.goodforgod:http-common:1.0.0"

Maven

<dependency>
    <groupId>io.goodforgod</groupId>
    <artifactId>http-common</artifactId>
    <version>1.0.0</version>
</dependency>

HttpStatus

Library provides HttpStatus with code and reasons, all mostly used HttpStatus are present there for easier use.

HttpStatus status = HttpStatus.ACCEPTED;

int code = status.code();
String reason = status.reason();

HttpStatus statusAccepted = HttpStatus.valueOf(201);

HttpMethod

Describes HttpMethod common methods of HTTP protocol.

final HttpMethod method = HttpMethod.of("get");
assertEquarls(HttpMethod.GET, method);

MediaType

Library provides MediaType class for correctly parsing and interpreting MediaType / ContentTypes.

There are plenty of widely used and preconfigured MediaTypes.

MediaType jsonMediaTypeUtf8 = MediaType.APPLICATION_JSON_UTF_8_TYPE;

MediaType mediaType = MediaType.of("application/json");

Optional<MediaType> jsonMediaType = MediaType.ofExtension("json");

URIBuilder

Library provide URIBuilder for easier and safer way of building URI.

URIBuilder.of("https://api.etherscan.io").path("/api")
                .param("module", "block")
                .param("action", "getblockreward")
                .build()

HttpHeaders

Describes HTTP headers and can be instantiated via different builders available.

final Map<String, List<String>> headerMap = new HashMap<>();
headerMap.put(HttpHeaders.CONNECTION, List.of("keep-alive"));
headerMap.put(HttpHeaders.CONTENT_LENGTH, List.of("72"));
headerMap.put(HttpHeaders.CONTENT_TYPE, List.of("application/json"));

final HttpHeaders headers = HttpHeaders.ofMultiMap(headerMap);

final Optional<Long> contentLength = headers.contentLength();
final Optional<MediaType> mediaType = headers.contentType();

FormattedException

Exception that allow to format messages like SLF4J logger and other similar.

throw new FormattedException("Failed for {} with code {}", "Bob", 200);

Resulted exception message:

Failed for Bob with code 200

License

This project licensed under the Apache License 2.0 - see the LICENSE file for details