Giter VIP home page Giter VIP logo

http4j's Introduction

HTT4J

Fork

(Almost) all the code is from the original project here.

All credits go to the original contributors.

Description

This is a simple, lightweight and tiny wrapper for Java's HttpURLConnection. It has no external dependencies and is written for Java 8.

It comes with a entity mapping system (serialization and deserialization for request and response bodies) with optional mappings for third party libraries (currently supporting: GSON).

Rationale

Most HTTP client for Java are either built for Java 11+, or have a large amount of dependencies, which means that in order to use them, one needs to built a fatjar that often end up being huge. This aims to offer a nicer way to interact with the Java 8 HTTP client, without having to double the size of the output artifacts.

Usage

Repository

repositories {
    mavenCentral()
    maven("https://repo.deltapvp.net/")
}

dependencies {
    compileOnly("net.deltapvp:http4j:1.5.2-SNAPSHOT")
}
<dependency>
    <groupId>net.deltapvp</groupId>
    <artifactId>http4j</artifactId>
    <version>1.5.2-SNAPSHOT</version>
</dependency>

Code

JavaDocs: https://reposilite.deltapvp.net/javadoc/snapshots/net/deltapvp/http4j/1.5.2-SNAPSHOT

All requests are done using an instance of com.intellectualsites.http.HttpClient:

HttpClient client = HttpClient.newBuilder()
    .withBaseURL("https://your.api.com")
    .withEntityMapper(EntityMapper.newInstance())
    .build();

The client also take in a com.intellectualsites.http.EntityMapper instance. This is used to map request & response bodies to Java objects. By default, it includes a mapper for Java strings.

EntityMapper entityMapper = EntityMapper.newInstance()
    .registerDeserializer(JsonObject.class, GsonMapper.deserializer(JsonObject.class, GSON));

The above snippet would create an entity mapper that maps to and from Java strings, and from HTTP response's to GSON json objects.

This can then be included in the HTTP client by using <builder>.withEntityMapper(mapper) to be used in all requests, or added to individual requests.

HTTP4J also supports request decorators, that can be used to modify each request. These are added by using:

builder.withDecorator(request -> {
    request.doSomething();
});

The built client can then be used to make HTTP requests, like such:

client.post("/some/api").withInput(() -> "Hello World")
    .onStatus(200, response -> {
        System.out.println("Everything is fine");
        System.out.println("Response: " + response.getResponseEntity(String.class));
    })
    .onStatus(404, response -> System.err.println("Could not find the resource =("))
    .onRemaining(response -> System.err.printf("Got status code: %d\n", response.getStatusCode()))
    .onException(Throwable::printStackTrace)
    .execute();

Exception Handling

HTTP4J will forward all RuntimeExceptions by default, and wrap all other exceptions (that do not extend RuntimeException) in a RuntimeException.

By using onException(exception -> {}) you are able to modify the behaviour.

Examples

More examples can be found in HttpClientTest.java

Projects using HTTP4J:

IntellectualSites/Arkitektonika-Client: Client for the Arkitektonika API broccolai/tickets: Discord bot

http4j's People

Contributors

citymonstret avatar notmyfault avatar powercasgamer avatar renovate[bot] avatar

http4j's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: Retry-After: unexpected status code 502
  • WARN: Package lookup failures

Warning

Renovate failed to look up the following dependencies: Failed to look up maven package net.deltapvp.titan:net.deltapvp.titan.gradle.plugin.

Files affected: gradle/libs.versions.toml


Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/build.yml
  • actions/checkout v3
  • gradle/wrapper-validation-action v1
  • actions/setup-java v3
  • actions/cache v3
  • gradle/gradle-build-action v2
  • actions/upload-artifact v3
gradle
gradle.properties
settings.gradle.kts
build.gradle
  • org.junit.jupiter:junit-jupiter-api 5.9.1
  • org.junit.jupiter:junit-jupiter-engine 5.9.1
  • org.mock-server:mockserver-netty 5.14.0
  • org.mock-server:mockserver-client-java 5.14.0
  • ch.qos.logback:logback-classic 1.4.5
gradle/libs.versions.toml
  • org.jetbrains:annotations 23.0.0
  • com.google.code.gson:gson 2.10
  • net.kyori.indra 3.1.0-SNAPSHOT
  • net.kyori.indra.publishing 3.1.0-SNAPSHOT
  • net.deltapvp.titan 1.0.91-SNAPSHOT
gradle-wrapper
gradle/wrapper/gradle-wrapper.properties
  • gradle 7.6

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.