|
| 1 | +/* |
| 2 | + * This code was auto generated by AfterShip SDK Generator. |
| 3 | + * Do not edit the class manually. |
| 4 | + */ |
| 5 | +package com.aftership; |
| 6 | + |
| 7 | +import com.aftership.auth.AuthType; |
| 8 | +import com.aftership.constant.ErrorEnum; |
| 9 | +import com.aftership.exception.ApiException; |
| 10 | +import com.aftership.http.AfterShipClient; |
| 11 | + |
| 12 | +public class AfterShip { |
| 13 | + private static final String DEFAULT_DOMAIN = "https://api.aftership.com"; |
| 14 | + private static final int DEFAULT_TIMEOUT = 10000; |
| 15 | + private static final int DEFAULT_MAX_RETRY = 2; |
| 16 | + private static AfterShipClient client; |
| 17 | + |
| 18 | + private static String apiKey = System.getenv("AFTERSHIP_TRACKING_SDK_API_KEY"); |
| 19 | + private static String apiSecret = System.getenv("AFTERSHIP_TRACKING_SDK_API_SECRET"); |
| 20 | + private static AuthType authType; |
| 21 | + |
| 22 | + private static String domain = System.getenv("AFTERSHIP_TRACKING_SDK_DOMAIN"); |
| 23 | + private static Integer maxRetry; |
| 24 | + private static Integer timeout; |
| 25 | + private static String userAgent = System.getenv("AFTERSHIP_TRACKING_SDK_USER_AGENT"); |
| 26 | + private static String proxy = System.getenv("AFTERSHIP_TRACKING_SDK_PROXY"); |
| 27 | + |
| 28 | + private AfterShip() {} |
| 29 | + |
| 30 | + public static synchronized void init(final String apiKey) { |
| 31 | + AfterShip.apiKey = apiKey; |
| 32 | + } |
| 33 | + |
| 34 | + public static synchronized void init( |
| 35 | + final String apiKey, final String apiSecret, AuthType authType) { |
| 36 | + AfterShip.apiKey = apiKey; |
| 37 | + AfterShip.apiSecret = apiSecret; |
| 38 | + AfterShip.authType = authType; |
| 39 | + } |
| 40 | + |
| 41 | + private static void getAuthType() { |
| 42 | + if (authType != null) { |
| 43 | + return; |
| 44 | + } |
| 45 | + String auth = System.getenv("AFTERSHIP_TRACKING_SDK_AUTHENTICATION_TYPE"); |
| 46 | + if (auth != null && !auth.isEmpty()) { |
| 47 | + auth = auth.toUpperCase(); |
| 48 | + if (auth.equals(AuthType.AES.name())) { |
| 49 | + authType = AuthType.AES; |
| 50 | + return; |
| 51 | + } |
| 52 | + if (auth.equals(AuthType.RSA.name())) { |
| 53 | + authType = AuthType.RSA; |
| 54 | + return; |
| 55 | + } |
| 56 | + } |
| 57 | + authType = AuthType.APIKEY; |
| 58 | + } |
| 59 | + |
| 60 | + private static Integer getIntegerFromEnv(String envName, Integer defaultValue) { |
| 61 | + String envValue = System.getenv(envName); |
| 62 | + if (envValue == null) { |
| 63 | + return defaultValue; |
| 64 | + } |
| 65 | + if (envValue.isEmpty()) { |
| 66 | + return defaultValue; |
| 67 | + } |
| 68 | + try { |
| 69 | + return Integer.parseInt(envValue); |
| 70 | + } catch (NumberFormatException e) { |
| 71 | + return defaultValue; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public static AfterShipClient getRestClient() throws Exception { |
| 76 | + if (client == null) { |
| 77 | + client = buildRestClient(); |
| 78 | + } |
| 79 | + return client; |
| 80 | + } |
| 81 | + |
| 82 | + private static AfterShipClient buildRestClient() throws Exception { |
| 83 | + getAuthType(); |
| 84 | + if (apiKey == null || apiKey.isEmpty()) { |
| 85 | + throw new ApiException(ErrorEnum.INVALID_API_KEY.getCode(), "Invalid option `apiKey`"); |
| 86 | + } |
| 87 | + if (AuthType.AES.equals(authType) || AuthType.RSA.equals(authType)) { |
| 88 | + if (apiSecret == null || apiSecret.isEmpty()) { |
| 89 | + throw new ApiException(ErrorEnum.INVALID_API_KEY.getCode(), "Invalid option `apiSecret`"); |
| 90 | + } |
| 91 | + } |
| 92 | + if (timeout == null) { |
| 93 | + Integer envTimeout = getIntegerFromEnv("AFTERSHIP_TRACKING_SDK_TIMEOUT", DEFAULT_TIMEOUT); |
| 94 | + setTimeout(envTimeout); |
| 95 | + } |
| 96 | + if (maxRetry == null) { |
| 97 | + Integer envMaxRetry = |
| 98 | + getIntegerFromEnv("AFTERSHIP_TRACKING_SDK_MAX_RETRY", DEFAULT_MAX_RETRY); |
| 99 | + setMaxRetry(envMaxRetry); |
| 100 | + } |
| 101 | + AfterShipClient.Builder builder = new AfterShipClient.Builder(apiKey, apiSecret, authType); |
| 102 | + if (domain != null && !domain.isEmpty()) { |
| 103 | + builder.setDomain(domain); |
| 104 | + } else { |
| 105 | + builder.setDomain(DEFAULT_DOMAIN); |
| 106 | + } |
| 107 | + if (userAgent != null && !userAgent.isEmpty()) { |
| 108 | + builder.setUserAgent(userAgent); |
| 109 | + } |
| 110 | + if (proxy != null && !proxy.isEmpty()) { |
| 111 | + builder.setProxy(proxy); |
| 112 | + } |
| 113 | + return builder.setTimeout(timeout).setMaxRetry(maxRetry).build(); |
| 114 | + } |
| 115 | + |
| 116 | + public static void setDomain(final String domain) throws Exception { |
| 117 | + if (domain == null || domain.isEmpty()) { |
| 118 | + throw new ApiException(ErrorEnum.INVALID_OPTION.getCode(), "Invalid option `domain`"); |
| 119 | + } |
| 120 | + AfterShip.domain = domain; |
| 121 | + } |
| 122 | + |
| 123 | + public static void setMaxRetry(final int maxRetry) throws Exception { |
| 124 | + if (maxRetry < 0 || maxRetry > 10) { |
| 125 | + throw new ApiException(ErrorEnum.INVALID_OPTION.getCode(), "Invalid option `maxRetry`"); |
| 126 | + } |
| 127 | + AfterShip.maxRetry = maxRetry; |
| 128 | + } |
| 129 | + |
| 130 | + public static void setTimeout(final int timeout) throws Exception { |
| 131 | + if (timeout < 0) { |
| 132 | + throw new ApiException(ErrorEnum.INVALID_OPTION.getCode(), "Invalid option `timeout`"); |
| 133 | + } |
| 134 | + AfterShip.timeout = timeout; |
| 135 | + } |
| 136 | + |
| 137 | + public static void setUserAgent(final String userAgent) { |
| 138 | + AfterShip.userAgent = userAgent; |
| 139 | + } |
| 140 | + |
| 141 | + public static void setProxy(final String proxy) { |
| 142 | + AfterShip.proxy = proxy; |
| 143 | + } |
| 144 | +} |
0 commit comments