@@ -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 () {
0 commit comments