Http Client for JVM - munichbughunter/SevenFacette GitHub Wiki

General setup

Create a class for your client. This class represents the technical logic of the system. So if multiple (micro-)services exist create one client per service. Or if the API has logical subdivisions one class can be created per subdivision. Create a property for the GenericHttpClient.

Configuration

Configuration files

The easiest way is to create a configuration file (see. Configuration). For every rest-client create one named section in the configuration file. Then access the configuration in the classes constructor:

public class MyClient {

  private GenericHttpClient client;

  public MyClient() {
    client = HttpClientFactory.createClient("myClient");
  }
  //..
}

All configurations will be loaded in the background and be used as soon as the client is instantiated.

Manual configuration

It is possible to create a configuration on your own. Create a HttpClientConfig and set the needed properties. Instead of using the clients' hand this configuration over to the client.

public class MyClient {

  private GenericHttpClient client;

  public MyClient() {
    HttpClientConfig config = new HttpClientConfig();
    Url url = new Url().protocol("https").baseUrl("mySuperNewService.com");
    config.setUrl(url);
    HttpProxy proxy = new HttpProxy();
    proxy.setHost("localhost");
    proxy.setPort(8080);
    config.setProxy(proxy);
    client = HttpClientFactory.createClient(config);
  }
  // ...
}

Combine configuration file with manual authentication

It might be useful to create a configuration file but not provide the authentication in this file. In this case, create the configuration file as described above but leave out the authentication. Then create the authentication and provide authentication and client name to the ClientFactory.

public class MyClient {

  private GenericHttpClient client;
  
  public MyClient() {
    HashMap<String, String> authentication = new HashMap();
    authentication.put("type", "basic");
    authentication.put("username", System.getProperty("myClient_username"));
    authentication.put("password", System.getProperty("myClient_password"));
    client = HttpClientFactory.createClient("myClient", authentication);
  }

  //..
}

Timeouts

Timeouts can be set via the HttpConfiguration. The following timeouts are available:

Timeout Default value
Socket timeout 10_000 ms
Connection timeout 10_000 ms
Connection request timeout 10_000 ms

Url

The Url-object consists of four parts: protocol://baseUrl:port/path

  • protocol: like Http or https, javascript, ... Http is the default which will be used if no other protocol is defined
  • baseUrl: like 'localhost'
  • port: a numeric value. The port needs to be between 0 and 65535. -1 means no port will be added to the Url and is the default.
  • path: will be added to the end

Authentication

Authentication can be added to the client via the configuration file or by creating a Hashmap for the configuration. All authentications need a type-parameter so that they can be mapped to the corresponding authentication. The further fields are depending on the chosen authentication. Currently, the following authentication types are supported:

  • basic
    • username
    • password
    • realm (optional - default is "")
    • sendWithoutRequest (optional - default is false)
  • oauth1
    • consumer_key
    • access_token
    • secret
    • sendWithoutRequest (optional - default is false)
  • oauth2
    • bearer
    • sendWithoutRequest (optional - default is false)

Proxy

A proxy can be set in the configuration file or as part of the manual configuration - see above.

HttpConfig config = new HttpConfig();
HttpProxy proxy = new HttpProxy();
proxy.setPort(8080);
proxy.setHost("localhost");
config.setProxy(proxy);

Requests

Currently GET, HEAD, POST, PUT, PATCH and DELETE requests are supported. You can send

  • Strings (including graphQL, JSON, XML)
  • Byte Array
  • multipart body For strings add the used content-type, e.g. CONTENTTYPES.APPLICATION_JSON.

Multipart content

To send multipart bodies you need to create a MultipartBody-object which is a mutable list of MultiPartData. Each MultiPartData consists of a string name and a string or byte array value.

MultipartBody body = new MultipartBody()
  .addStringPart("firstPart", "My content")
  .addByteArrayPart("second Part", "My second content".getBytes());

Headers

All requests need an HttpHeader-object. This object is a mutable map that holds string keys and lists of string values.

Response

If the requests can be performed a HttpResponse-object will be returned - otherwise, an exception is thrown. This object consists of

  • body
  • status
  • headers The headers are a map of string keys and lists of string values

Example

First create a client

public class MyClient {
  private GenericHttpClient client;

  public MyClient() {
    client = HttpClientFactory.createClient("myClient");
  }
  
  // Simple get requests
  public HttpResponse getHealthStatus() {
    return client.get("health", new HttpHeader());
  }

  // Send string content
  public HttpResponse createNewData(String content) {
    return client.post("new", CONTENTTYPES.TEXT_PLAIN, new HttpHeader()); 
  }

  // Send graphQl content
  public HttpResponse getDataByGraphQL(String request) {
    return client.postGraphQl("graphql", request, CONTENTTYPES.APPLICATION_GRAPHQL, new HttpHeader());
  }
}

The created client can then easily be used for test creation:

public MyClientTest {
  @Test
  public checkHealthStatus() {
    MyClient client = new MyClient();
    HttpResponse response = client.getHealthStatus();

    assertThat(HttpReponse.getStatus()).equals(200);
    assertThat(HttpReponse.getBody()).equals("OK");
  }

  @Test
  public checkNewDataCreation() {
    MyClient client = new MyClient();
    HttpResponse response = client.createNewData("My new data");
    
    assertThat(HttpReponse.getStatus()).equals(200);
    assertThat(HttpReponse.getBody()).equals("MyNewID");
  }

  @Test
  public checkGraphQLResponse() {
    MyClient client = new MyClient();
    String request = "{query:{id}}";
    HttpResponse response = client.getDataByGraphQL(request);

    assertThat(HttpReponse.getStatus()).equals(200);
    assertThat(HttpReponse.getBody()).equals("{"id": "TestID"}");
  }
  
}
⚠️ **GitHub.com Fallback** ⚠️