From 489b7ccc54bf9a3a5588c74615662ab86d0320b4 Mon Sep 17 00:00:00 2001 From: Lossy Date: Tue, 11 Jan 2022 22:20:19 -0600 Subject: [PATCH] Add JavaSteam version as default user-agent. --- build.gradle | 22 +++++++++++++--- .../javasteam/steam/webapi/WebAPI.java | 26 ++++++++++--------- .../javasteam/steam/webapi/WebAPITest.java | 12 +++++++++ 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/build.gradle b/build.gradle index 1cc63147..e0b89e3f 100644 --- a/build.gradle +++ b/build.gradle @@ -67,13 +67,11 @@ sourceSets { java { srcDirs += new File(buildDir, 'generated/source/steamd/main/java') srcDirs += new File(buildDir, 'generated/source/proto/main/java') + srcDirs += new File(buildDir, 'generated/source/in/dragonbra/javasteam') } } } -compileJava.dependsOn generateSteamLanguage -check.dependsOn jacocoTestReport - task sourcesJar(type: Jar, dependsOn: classes) { classifier = 'sources' from sourceSets.main.allSource @@ -84,6 +82,24 @@ task javadocJar(type: Jar, dependsOn: javadoc) { from javadoc.destinationDir } +task generatVersionClass { + def outputDir = file("$buildDir/generated/source/in/dragonbra/javasteam/util") + outputs.dir outputDir + doFirst { + outputDir.exists() || outputDir.mkdirs() + new File(outputDir, "Versions.java") + .write("""package in.dragonbra.javasteam.util; + +public class Versions { + public static final String VERSION = \"$project.version\"; +}""") + } +} + +compileJava.dependsOn generatVersionClass +compileJava.dependsOn generateSteamLanguage +check.dependsOn jacocoTestReport + javadoc { exclude "**/in/dragonbra/javasteam/protobufs/**" } diff --git a/src/main/java/in/dragonbra/javasteam/steam/webapi/WebAPI.java b/src/main/java/in/dragonbra/javasteam/steam/webapi/WebAPI.java index 113a11a4..dccfc73b 100644 --- a/src/main/java/in/dragonbra/javasteam/steam/webapi/WebAPI.java +++ b/src/main/java/in/dragonbra/javasteam/steam/webapi/WebAPI.java @@ -1,6 +1,7 @@ package in.dragonbra.javasteam.steam.webapi; import in.dragonbra.javasteam.types.KeyValue; +import in.dragonbra.javasteam.util.Versions; import in.dragonbra.javasteam.util.WebHelpers; import in.dragonbra.javasteam.util.compat.Consumer; import okhttp3.*; @@ -46,9 +47,8 @@ public WebAPI(OkHttpClient httpClient, String baseAddress, String _interface, St * @param version The version of the function to call. * @param parameters A map of string key value pairs representing arguments to be passed to the API. * @return A {@link KeyValue} object representing the results of the Web API call. - * @throws IOException if the request could not be executed + * @throws IOException if the request could not be executed * @throws WebAPIRequestException the request was successful but returned a non success response code - * */ public KeyValue call(String httpMethod, String function, int version, Map parameters) throws IOException, WebAPIRequestException { @@ -116,8 +116,8 @@ public KeyValue call(String httpMethod, String function, int version) throws IOE /** * Manually calls the specified Web API function with the provided details. This method is synchronous. * - * @param function The function name to call. - * @param version The version of the function to call. + * @param function The function name to call. + * @param version The version of the function to call. * @return A {@link KeyValue} object representing the results of the Web API call. * @throws IOException if the request could not be executed */ @@ -140,7 +140,7 @@ public KeyValue call(String httpMethod, String function) throws IOException { /** * Manually calls the specified Web API function with the provided details. This method is synchronous. * - * @param function The function name to call. + * @param function The function name to call. * @return A {@link KeyValue} object representing the results of the Web API call. * @throws IOException if the request could not be executed */ @@ -226,10 +226,10 @@ public void call(String httpMethod, String function, final Consumer ca /** * Manually calls the specified Web API function with the provided details. This method is asynchronous. * - * @param function The function name to call. - * @param version The version of the function to call. - * @param callback the callback that will be called with the resulting {@link KeyValue} object. - * @param error the callback for handling response errors. + * @param function The function name to call. + * @param version The version of the function to call. + * @param callback the callback that will be called with the resulting {@link KeyValue} object. + * @param error the callback for handling response errors. * @throws IOException if the request could not be executed */ public void call(String function, int version, final Consumer callback, @@ -254,9 +254,9 @@ public void call(String function, Map parameters, final Consumer /** * Manually calls the specified Web API function with the provided details. This method is asynchronous. * - * @param function The function name to call. - * @param callback the callback that will be called with the resulting {@link KeyValue} object. - * @param error the callback for handling response errors. + * @param function The function name to call. + * @param callback the callback that will be called with the resulting {@link KeyValue} object. + * @param error the callback for handling response errors. * @throws IOException if the request could not be executed */ public void call(String function, final Consumer callback, final Consumer error) @@ -297,6 +297,7 @@ private Request buildRequest(String httpMethod, String function, int version, Ma } Request.Builder builder = new Request.Builder(); + builder.header("User-Agent", "JavaSteam-" + Versions.VERSION); HttpUrl.Builder urlBuilder = baseAddress.newBuilder() .addPathSegment(_interface) @@ -345,6 +346,7 @@ public class WebAPIRequestException extends IOException { /** * Initializes a new instance of the {@link WebAPIRequestException} class. + * * @param response the response object from the call */ WebAPIRequestException(Response response) { diff --git a/src/test/java/in/dragonbra/javasteam/steam/webapi/WebAPITest.java b/src/test/java/in/dragonbra/javasteam/steam/webapi/WebAPITest.java index 285f870d..292d5f35 100644 --- a/src/test/java/in/dragonbra/javasteam/steam/webapi/WebAPITest.java +++ b/src/test/java/in/dragonbra/javasteam/steam/webapi/WebAPITest.java @@ -4,6 +4,7 @@ import in.dragonbra.javasteam.steam.steamclient.configuration.ISteamConfigurationBuilder; import in.dragonbra.javasteam.steam.steamclient.configuration.SteamConfiguration; import in.dragonbra.javasteam.types.KeyValue; +import in.dragonbra.javasteam.util.Versions; import in.dragonbra.javasteam.util.compat.Consumer; import okhttp3.HttpUrl; import okhttp3.mockwebserver.MockResponse; @@ -64,6 +65,17 @@ public void tearDown() throws IOException { server.shutdown(); } + @Test + public void requestHeaders() throws InterruptedException, IOException { + WebAPI api = config.getWebAPI("TestInterface"); + + api.call("TestFunction"); + + RecordedRequest request = server.takeRequest(); + + assertEquals("JavaSteam-" + Versions.VERSION, request.getHeaders().get("User-Agent")); + } + @Test public void steamConfigWebApiInterface() { SteamConfiguration config = SteamConfiguration.create(new Consumer() {