Skip to content

Commit 1735a2f

Browse files
committed
Adding user agent string/
1 parent 5e6f76b commit 1735a2f

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

src/main/java/com/blockscore/common/Constants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public final class Constants {
99
public static final String AUTHORIZATION_HEADER = "Authorization";
1010
public static final String ACCEPT_HEADER = "Accept";
1111
public static final String BLOCKSCORE_DOMAIN = "https://api.blockscore.com";
12-
12+
public static final String USER_AGENT = "blockscore-java/4.0.0 (https://github.com/BlockScore/blockscore-java)";
13+
1314
private static final String VERSION_CODE = "4";
1415
private static final String ACCEPT_CONTENTS = "application/vnd.blockscore+json;version=%s";
1516

src/main/java/com/blockscore/net/BlockscoreApiClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
import retrofit.converter.JacksonConverter;
1717

1818
import java.io.UnsupportedEncodingException;
19-
import java.util.Base64;
19+
20+
import javax.xml.bind.DatatypeConverter;
2021

2122
/**
2223
* The Blockscore Java API client.
@@ -152,7 +153,7 @@ public PaginatedResult<Candidate> listCandidates() {
152153
@NotNull
153154
private String getEncodedAuthorization() {
154155
try {
155-
return "Basic " + Base64.getEncoder().encodeToString(apiKey.getBytes("utf-8"));
156+
return "Basic " + DatatypeConverter.printBase64Binary(apiKey.getBytes("UTF-8"));
156157
} catch (UnsupportedEncodingException e) {
157158
throw new RuntimeException(e);
158159
}

src/main/java/com/blockscore/net/BlockscoreHttpClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.blockscore.net;
22

3+
import com.blockscore.common.Constants;
4+
import com.blockscore.net.UserAgentInterceptor;
5+
36
import com.squareup.okhttp.OkHttpClient;
47
import com.squareup.okhttp.OkUrlFactory;
58
import retrofit.client.Request;
@@ -28,6 +31,7 @@ private OkHttpClient generateDefaultHttpClient() {
2831
OkHttpClient client = new OkHttpClient();
2932
client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
3033
client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
34+
client.networkInterceptors().add(new UserAgentInterceptor(Constants.USER_AGENT));
3135
return client;
3236
}
3337

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.blockscore.net;
2+
3+
import com.squareup.okhttp.Interceptor;
4+
import com.squareup.okhttp.Interceptor.Chain;
5+
import com.squareup.okhttp.Response;
6+
import com.squareup.okhttp.Request;
7+
8+
import java.io.IOException;
9+
10+
class UserAgentInterceptor implements Interceptor {
11+
12+
private final String userAgent;
13+
14+
public UserAgentInterceptor(String userAgent) {
15+
this.userAgent = userAgent;
16+
}
17+
18+
@Override
19+
public Response intercept(Chain chain) throws IOException {
20+
Request originalRequest = chain.request();
21+
Request requestWithUserAgent =
22+
originalRequest.newBuilder()
23+
.removeHeader("User-Agent")
24+
.addHeader("User-Agent", userAgent)
25+
.build();
26+
return chain.proceed(requestWithUserAgent);
27+
}
28+
}

0 commit comments

Comments
 (0)