|
65 | 65 | public class Client {
|
66 | 66 | private final static String version = "3.3.0";
|
67 | 67 |
|
68 |
| - protected String userAgent = "Algolia for Android " + version; |
| 68 | + /** |
| 69 | + * The user agents as a raw string. This is what is passed in request headers. |
| 70 | + * WARNING: It is stored for efficiency purposes. It should not be modified directly. |
| 71 | + */ |
| 72 | + private String userAgentRaw; |
| 73 | + |
| 74 | + /*** |
| 75 | + * A version of a software library. |
| 76 | + * Used to construct the <code>User-Agent</code> header. |
| 77 | + */ |
| 78 | + public static class LibraryVersion { |
| 79 | + public final @NonNull String name; |
| 80 | + public final @NonNull String version; |
| 81 | + |
| 82 | + public LibraryVersion(@NonNull String name, @NonNull String version) { |
| 83 | + this.name = name; |
| 84 | + this.version = version; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean equals(Object object) { |
| 89 | + if (!(object instanceof LibraryVersion)) |
| 90 | + return false; |
| 91 | + LibraryVersion other = (LibraryVersion)object; |
| 92 | + return this.name.equals(other.name) && this.version.equals(other.version); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public int hashCode() { |
| 97 | + return name.hashCode() ^ version.hashCode(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** The user agents, as a structured list of library versions. */ |
| 102 | + private List<LibraryVersion> userAgents = new ArrayList<>(); |
69 | 103 |
|
70 | 104 | /** Connect timeout (ms). */
|
71 | 105 | private int connectTimeout = 2000;
|
@@ -120,6 +154,8 @@ public Client(@NonNull String applicationID, @NonNull String apiKey) {
|
120 | 154 | public Client(@NonNull String applicationID, @NonNull String apiKey, String[] hosts) {
|
121 | 155 | this.applicationID = applicationID;
|
122 | 156 | this.apiKey = apiKey;
|
| 157 | + this.addUserAgent(new LibraryVersion("Algolia for Android", version)); |
| 158 | + this.addUserAgent(new LibraryVersion("Android", Build.VERSION.RELEASE)); |
123 | 159 | if (hosts != null) {
|
124 | 160 | setReadHosts(hosts);
|
125 | 161 | setWriteHosts(hosts);
|
@@ -280,6 +316,59 @@ public Index initIndex(@NonNull String indexName) {
|
280 | 316 | return new Index(this, indexName);
|
281 | 317 | }
|
282 | 318 |
|
| 319 | + /** |
| 320 | + * Add a software library to the list of user agents. |
| 321 | + * |
| 322 | + * @param userAgent The library to add. |
| 323 | + */ |
| 324 | + public void addUserAgent(@NonNull LibraryVersion userAgent) { |
| 325 | + userAgents.add(userAgent); |
| 326 | + updateUserAgents(); |
| 327 | + } |
| 328 | + |
| 329 | + /** |
| 330 | + * Remove a software library from the list of user agents. |
| 331 | + * |
| 332 | + * @param userAgent The library to remove. |
| 333 | + */ |
| 334 | + public void removeUserAgent(@NonNull LibraryVersion userAgent) { |
| 335 | + userAgents.remove(userAgent); |
| 336 | + updateUserAgents(); |
| 337 | + } |
| 338 | + |
| 339 | + /** |
| 340 | + * Retrieve the list of declared user agents. |
| 341 | + * |
| 342 | + * @return The declared user agents. |
| 343 | + */ |
| 344 | + public @NonNull LibraryVersion[] getUserAgents() { |
| 345 | + return userAgents.toArray(new LibraryVersion[userAgents.size()]); |
| 346 | + } |
| 347 | + |
| 348 | + /** |
| 349 | + * Test whether a user agent is declared. |
| 350 | + * |
| 351 | + * @param userAgent The user agent to look for. |
| 352 | + * @return true if it is declared on this client, false otherwise. |
| 353 | + */ |
| 354 | + public boolean hasUserAgent(@NonNull LibraryVersion userAgent) { |
| 355 | + return userAgents.contains(userAgent); |
| 356 | + } |
| 357 | + |
| 358 | + private void updateUserAgents() { |
| 359 | + StringBuilder s = new StringBuilder(); |
| 360 | + for (LibraryVersion userAgent : userAgents) { |
| 361 | + if (s.length() != 0) { |
| 362 | + s.append("; "); |
| 363 | + } |
| 364 | + s.append(userAgent.name); |
| 365 | + s.append(" ("); |
| 366 | + s.append(userAgent.version); |
| 367 | + s.append(")"); |
| 368 | + } |
| 369 | + userAgentRaw = s.toString(); |
| 370 | + } |
| 371 | + |
283 | 372 | // ----------------------------------------------------------------------
|
284 | 373 | // Public operations
|
285 | 374 | // ----------------------------------------------------------------------
|
@@ -679,7 +768,7 @@ private byte[] _requestRaw(Method m, String url, String json, List<String> hosts
|
679 | 768 | }
|
680 | 769 |
|
681 | 770 | // set user agent
|
682 |
| - hostConnection.setRequestProperty("User-Agent", userAgent); |
| 771 | + hostConnection.setRequestProperty("User-Agent", userAgentRaw); |
683 | 772 |
|
684 | 773 | // write JSON entity
|
685 | 774 | if (json != null) {
|
|
0 commit comments