/
AbstractClient.java
67 lines (57 loc) · 1.94 KB
/
AbstractClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.chavaillaz.client.common;
import com.chavaillaz.client.common.security.AnonymousAuthentication;
import com.chavaillaz.client.common.security.Authentication;
import com.chavaillaz.client.common.security.PasswordAuthentication;
import com.chavaillaz.client.common.security.TokenAuthentication;
import com.chavaillaz.client.common.utility.ProxyConfiguration;
/**
* Abstract class implementing common parts of general client.
*
* @param <C> The HTTP client type
* @param <I> The interface type (to be returned by methods in order to chain calls)
*/
public abstract class AbstractClient<C, I> implements Client<I> {
protected final String baseUrl;
protected Authentication authentication;
protected ProxyConfiguration proxy;
/**
* Creates a new abstract client.
*
* @param baseUrl The base URL of the endpoints
*/
protected AbstractClient(String baseUrl) {
this.baseUrl = baseUrl;
withAnonymousAuthentication();
}
/**
* Creates a new HTTP client that will be used to communicate with the desired endpoints.
*
* @return The HTTP client
*/
protected abstract C newHttpClient();
@Override
public I withProxy(String host, Integer port) {
this.proxy = ProxyConfiguration.from(host, port);
return (I) this;
}
@Override
public I withProxy(String url) {
this.proxy = ProxyConfiguration.from(url);
return (I) this;
}
@Override
public I withAnonymousAuthentication() {
this.authentication = new AnonymousAuthentication();
return (I) this;
}
@Override
public I withTokenAuthentication(String token) {
this.authentication = new TokenAuthentication(token);
return (I) this;
}
@Override
public I withUserAuthentication(String username, String password) {
this.authentication = new PasswordAuthentication(username, password);
return (I) this;
}
}