33import java .io .IOException ;
44import java .io .InputStream ;
55import java .nio .charset .StandardCharsets ;
6+ import java .util .Map ;
67
78/**
89 * Provides metadata about the SDK that can be attached to outgoing requests, such as the current
910 * version and the default {@code User-Agent} header value.
1011 */
1112public final class SdkMetadata {
13+ private static final String API_VERSION_RESOURCE = "/com/sumup/sdk/api-version.txt" ;
1214 private static final String VERSION_RESOURCE = "/com/sumup/sdk/sdk-version.txt" ;
1315 private static final String USER_AGENT_PREFIX = "sumup-java" ;
16+ private static final String LANGUAGE = "java" ;
1417
15- private static final String VERSION = loadVersion ();
18+ private static final String API_VERSION = loadResource (API_VERSION_RESOURCE );
19+ private static final String VERSION = loadResource (VERSION_RESOURCE );
1620 private static final String USER_AGENT = USER_AGENT_PREFIX + "/v" + VERSION ;
21+ private static final Map <String , String > RUNTIME_HEADERS =
22+ Map .of (
23+ "X-Sumup-Api-Version" , API_VERSION ,
24+ "X-Sumup-Lang" , LANGUAGE ,
25+ "X-Sumup-Package-Version" , VERSION ,
26+ "X-Sumup-OS" , System .getProperty ("os.name" , "unknown" ),
27+ "X-Sumup-Arch" , runtimeArch (),
28+ "X-Sumup-Runtime" , runtimeIdentifier (),
29+ "X-Sumup-Runtime-Version" , Runtime .version ().toString ());
1730
1831 private SdkMetadata () {}
1932
33+ /** Returns the API version declared by the OpenAPI specification. */
34+ public static String apiVersion () {
35+ return API_VERSION ;
36+ }
37+
2038 /** Returns the SDK version read from the generated version resource. */
2139 public static String version () {
2240 return VERSION ;
@@ -27,8 +45,28 @@ public static String userAgent() {
2745 return USER_AGENT ;
2846 }
2947
30- private static String loadVersion () {
31- try (InputStream stream = SdkMetadata .class .getResourceAsStream (VERSION_RESOURCE )) {
48+ /** Returns the runtime headers that should be sent with each request. */
49+ public static Map <String , String > runtimeHeaders () {
50+ return RUNTIME_HEADERS ;
51+ }
52+
53+ static String runtimeArch () {
54+ String arch = System .getProperty ("os.arch" , "unknown" ).toLowerCase ();
55+ return switch (arch ) {
56+ case "amd64" , "x86_64" -> "x86_64" ;
57+ case "x86" , "i386" , "i486" , "i586" , "i686" -> "x86" ;
58+ case "aarch64" , "arm64" -> "arm64" ;
59+ case "arm" , "armv7" , "armv7l" -> "arm" ;
60+ default -> arch ;
61+ };
62+ }
63+
64+ private static String runtimeIdentifier () {
65+ return LANGUAGE + Runtime .version ();
66+ }
67+
68+ private static String loadResource (String path ) {
69+ try (InputStream stream = SdkMetadata .class .getResourceAsStream (path )) {
3270 if (stream == null ) {
3371 return "unknown" ;
3472 }
0 commit comments