Skip to content

Commit

Permalink
Refactor addHeaders() allowing customized headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mawp committed Oct 23, 2017
1 parent 66019e8 commit d04582e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
16 changes: 11 additions & 5 deletions core/src/main/java/org/web3j/protocol/http/HttpService.java
Expand Up @@ -39,6 +39,8 @@ public class HttpService extends Service {
private final String url;

private final boolean includeRawResponse;

private HashMap<String, String> headersMap = new HashMap<>();

public HttpService(String url, OkHttpClient httpClient, boolean includeRawResponses) {
super(includeRawResponses);
Expand Down Expand Up @@ -145,10 +147,14 @@ private InputStream buildInputStream(ResponseBody responseBody) throws IOExcepti
}

private Headers buildHeaders() {
Map<String, String> headers = new HashMap<>();
addHeaders(headers);
return Headers.of(headers);
return Headers.of(headersMap);
}

public void addHeader(String key, String value) {
headersMap.put(key, value);
}

public void addHeaders(Map<String, String> headers) {
headersMap.putAll(headers);
}

protected void addHeaders(Map<String, String> headers) { }
}
Expand Up @@ -18,6 +18,7 @@ public class InfuraHttpService extends HttpService {
public InfuraHttpService(String url, String clientVersion, boolean required) {
super(url);
clientVersionHeader = buildClientVersionHeader(clientVersion, required);
addHeaders(clientVersionHeader);
}

public InfuraHttpService(String url, String clientVersion) {
Expand All @@ -28,11 +29,6 @@ public InfuraHttpService(String url) {
this(url, "", false);
}

@Override
protected void addHeaders(Map<String, String> headers) {
headers.putAll(clientVersionHeader);
}

static Map<String, String> buildClientVersionHeader(String clientVersion, boolean required) {
if (clientVersion == null || clientVersion.equals("")) {
return Collections.emptyMap();
Expand Down

0 comments on commit d04582e

Please sign in to comment.