-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from AssemblyAI/niels/user-agent
Add AAI UserAgent
- Loading branch information
Showing
9 changed files
with
256 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.assemblyai.api.core; | ||
|
||
public class Constants { | ||
public static final String SDK_VERSION = "1.1.3"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.assemblyai.api.core; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.assemblyai.api.core.Constants.SDK_VERSION; | ||
|
||
public class UserAgent { | ||
private static final UserAgent defaultUserAgent; | ||
|
||
static { | ||
defaultUserAgent = createDefaultUserAgent(); | ||
} | ||
|
||
private final Map<String, UserAgentItem> userAgent; | ||
|
||
public UserAgent(Map<String, UserAgentItem> userAgent) { | ||
this.userAgent = userAgent; | ||
} | ||
|
||
public UserAgent(UserAgent a, UserAgent b) { | ||
this.userAgent = UserAgent.merge(a.userAgent, b.userAgent); | ||
} | ||
|
||
public String toAssemblyAIUserAgentString() { | ||
StringBuilder sb = new StringBuilder(); | ||
if (this.userAgent == null) { | ||
return sb.toString(); | ||
} | ||
|
||
sb.append(" AssemblyAI/1.0 ("); | ||
sb.append( | ||
this.userAgent | ||
.entrySet() | ||
.stream() | ||
.map(entry -> String.format( | ||
"%s=%s/%s", | ||
entry.getKey(), | ||
entry.getValue().getName(), | ||
entry.getValue().getVersion() | ||
)) | ||
.collect(Collectors.joining(" ")) | ||
); | ||
sb.append(")"); | ||
return sb.toString(); | ||
} | ||
|
||
public static UserAgent getDefault(){ | ||
return defaultUserAgent; | ||
} | ||
|
||
private static UserAgent createDefaultUserAgent() { | ||
HashMap<String, UserAgentItem> defaultUserAgent = new HashMap<>(); | ||
defaultUserAgent.put( | ||
"sdk", | ||
new UserAgentItem( | ||
"Java", | ||
SDK_VERSION | ||
) | ||
); | ||
defaultUserAgent.put( | ||
"runtime_env", | ||
new UserAgentItem( | ||
System.getProperty("java.runtime.name"), | ||
System.getProperty("java.runtime.version") | ||
) | ||
); | ||
|
||
return new UserAgent(defaultUserAgent); | ||
} | ||
|
||
private UserAgent merge(UserAgent other) { | ||
return new UserAgent(UserAgent.merge(this.userAgent, other.userAgent)); | ||
} | ||
|
||
private static Map<String, UserAgentItem> merge(Map<String, UserAgentItem> a, Map<String, UserAgentItem> b) { | ||
Map<String, UserAgentItem> newUserAgent = new HashMap<>(); | ||
|
||
// user this user agent | ||
newUserAgent.putAll(a); | ||
// override with incoming user agent | ||
newUserAgent.putAll(b); | ||
|
||
// remove all null values | ||
for (Map.Entry<String, UserAgentItem> entry : b.entrySet()) { | ||
if (entry.getValue() == null) { | ||
newUserAgent.remove(entry.getKey()); | ||
} | ||
} | ||
return newUserAgent; | ||
} | ||
} | ||
|
47 changes: 47 additions & 0 deletions
47
src/main/java/com/assemblyai/api/core/UserAgentInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.assemblyai.api.core; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.IOException; | ||
|
||
public class UserAgentInterceptor implements Interceptor { | ||
private final UserAgent userAgent; | ||
|
||
public UserAgentInterceptor(UserAgent userAgent) { | ||
this.userAgent = userAgent; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Response intercept(@NotNull Chain chain) throws IOException { | ||
if (userAgent == null) { | ||
return chain.proceed(chain.request()); | ||
} | ||
|
||
Request originalRequest = chain.request(); | ||
String userAgentString = originalRequest.headers().get("User-Agent"); | ||
if (userAgentString != null) { | ||
// if already contains AssemblyAI UA, skip | ||
if (userAgentString.contains("AssemblyAI/")) { | ||
return chain.proceed(chain.request()); | ||
} | ||
} else { | ||
userAgentString = ""; | ||
} | ||
|
||
String assemblyAIUserAgentString = userAgent.toAssemblyAIUserAgentString(); | ||
// if AAI UA null or empty, skip | ||
if (assemblyAIUserAgentString == null || assemblyAIUserAgentString.isEmpty()) { | ||
return chain.proceed(chain.request()); | ||
} | ||
|
||
userAgentString += " " + assemblyAIUserAgentString; | ||
Request requestWithUserAgent = originalRequest.newBuilder() | ||
.header("User-Agent", userAgentString) | ||
.build(); | ||
return chain.proceed(requestWithUserAgent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.assemblyai.api.core; | ||
|
||
public class UserAgentItem{ | ||
private final String name; | ||
private final String version; | ||
|
||
public UserAgentItem(String name, String version) { | ||
this.name = name; | ||
this.version = version; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.assemblyai.api; | ||
|
||
import com.assemblyai.api.core.UserAgent; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.assemblyai.api.core.Constants.SDK_VERSION; | ||
|
||
public final class UserAgentTest { | ||
@Test | ||
public void ShouldCreateDefaultUserAgent() { | ||
UserAgent agent = UserAgent.getDefault(); | ||
assert agent != null; | ||
String userAgentString = agent.toAssemblyAIUserAgentString(); | ||
assert userAgentString != null; | ||
assert userAgentString.contains("AssemblyAI/1.0 ("); | ||
assert userAgentString.endsWith(")"); | ||
assert userAgentString.contains("sdk=Java/" + SDK_VERSION); | ||
assert userAgentString.contains("runtime_env=OpenJDK Runtime Environment/"); | ||
} | ||
} |