Http Client for JVM - munichbughunter/SevenFacette GitHub Wiki
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.
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.
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);
}
// ...
}
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 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 |
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 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)
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);
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.
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());
All requests need an HttpHeader-object. This object is a mutable map that holds string keys and lists of string values.
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
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"}");
}
}