Skip to content

Commit bbd656d

Browse files
authored
Merge pull request #26 from OpenCageData/15-add-user-agent-header
feat: 🎸 adds the header User-Agent
2 parents cad331f + afa271c commit bbd656d

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

build.gradle

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ test {
4141
systemProperties = System.properties
4242
}
4343

44+
jar {
45+
manifest {
46+
attributes(
47+
'Specification-Title': 'Java Client for the OpenCage REST API',
48+
'Specification-Vendor': 'OpenCage GmbH',
49+
'Specification-Version': archiveVersion,
50+
'Implementation-Title': 'Java Client for the OpenCage REST API',
51+
'Implementation-Vendor': 'OpenCage GmbH',
52+
'Implementation-Version': archiveVersion,
53+
)
54+
}
55+
}
56+
57+
4458
publishing {
4559
publications {
4660
mavenJava(MavenPublication) {

src/main/java/com/opencagedata/jopencage/JOpenCageGeocoder.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,26 @@ public class JOpenCageGeocoder {
4040
private String path = OPENCAGE_PATH;
4141
private String format = "json";
4242
private String apiKey;
43+
private String userAgent;
4344

4445
/**
4546
* JOpenCageGeocoder
46-
*
47+
*
4748
* @param apiKey your Opencage Data API Key
4849
*/
4950
public JOpenCageGeocoder(String apiKey) {
5051
this.apiKey = apiKey;
52+
String packageVersion = this.getClass().getPackage().getImplementationVersion();
53+
StringBuilder userAgentBuilder = new StringBuilder("jOpenCage/");
54+
if (packageVersion != null) {
55+
userAgentBuilder.append(packageVersion);
56+
}
57+
this.userAgent = userAgentBuilder.toString();
5158
}
5259

5360
/**
5461
* forward geocoding
55-
*
62+
*
5663
* @param request the request
5764
* @return JOpenCageResponse
5865
*/
@@ -62,7 +69,7 @@ public JOpenCageResponse forward(JOpenCageForwardRequest request) {
6269

6370
/**
6471
* reverse geocoding
65-
*
72+
*
6673
* @param request the request
6774
* @return JOpenCageResponse
6875
*/
@@ -108,6 +115,7 @@ private JOpenCageResponse sendRequest(JOpenCageRequest jOpenCageRequest) {
108115
if (url != null) {
109116
try (CloseableHttpClient httpclient = HttpClientBuilder.create().build()) {
110117
HttpGet getRequest = new HttpGet(url);
118+
getRequest.setHeader("User-Agent", this.userAgent);
111119

112120
HttpClientResponseHandler<JOpenCageResponse> rh = new AbstractHttpClientResponseHandler<JOpenCageResponse>() {
113121
@Override
@@ -151,7 +159,7 @@ public JOpenCageResponse handleEntity(HttpEntity entity) throws IOException {
151159

152160
/**
153161
* Is HTTPS enabled?
154-
*
162+
*
155163
* @return boolean
156164
*/
157165
public boolean isHttpsEnabled() {
@@ -160,7 +168,7 @@ public boolean isHttpsEnabled() {
160168

161169
/**
162170
* Toggle HTTPS usage
163-
*
171+
*
164172
* @param httpsEnabled enable or disable the HTTPS usage
165173
*/
166174
public void setHttpsEnabled(boolean httpsEnabled) {
@@ -169,7 +177,7 @@ public void setHttpsEnabled(boolean httpsEnabled) {
169177

170178
/**
171179
* API Host
172-
*
180+
*
173181
* @return host
174182
*/
175183
public String getHost() {
@@ -178,7 +186,7 @@ public String getHost() {
178186

179187
/**
180188
* Set API Host
181-
*
189+
*
182190
* @param host the host
183191
*/
184192
public void setHost(String host) {
@@ -187,7 +195,7 @@ public void setHost(String host) {
187195

188196
/**
189197
* API path
190-
*
198+
*
191199
* @return String the path
192200
*/
193201
public String getPath() {
@@ -196,7 +204,7 @@ public String getPath() {
196204

197205
/**
198206
* Set API path
199-
*
207+
*
200208
* @param path the path
201209
*/
202210
public void setPath(String path) {
@@ -205,7 +213,7 @@ public void setPath(String path) {
205213

206214
/**
207215
* API Key
208-
*
216+
*
209217
* @return String the API Key
210218
*/
211219
public String getApiKey() {
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
Manifest-Version: 1.0
2-
Main-Class: com.byteowls.jopencage
2+
Specification-Title: Java Client for the OpenCage REST API
3+
Specification-Vendor: OpenCage GmbH
4+
Implementation-Title: Java Client for the OpenCage REST API
5+
Implementation-Vendor: OpenCage GmbH
36

src/test/java/com/opencagedata/jopencage/JOpenCageTestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public abstract class JOpenCageTestUtils {
66

77
public static void freeTrailSleep() {
88
try {
9-
Thread.sleep(1000); // free trial accounts are limited to 1 request/second
9+
Thread.sleep(100); // free trial accounts are limited to 1 request/second, put back 1000 if running tests returns 429 errors
1010
} catch (InterruptedException ignore) {}
1111
}
1212

0 commit comments

Comments
 (0)