|
| 1 | +package com.sendgrid; |
| 2 | + |
| 3 | +import com.sendgrid.constant.ErrorMessages; |
| 4 | +import com.sendgrid.exception.AuthenticationException; |
| 5 | +import com.sendgrid.http.ApiKeyRestClient; |
| 6 | +import lombok.Getter; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +public class ApiKeySendGrid { |
| 13 | + private static String apiKey = System.getenv("SENDGRID_API_KEY"); |
| 14 | + private static String region = System.getenv("SENDGRID_REGION"); |
| 15 | + @Getter |
| 16 | + private static List<String> userAgentExtensions; |
| 17 | + private static volatile ApiKeyRestClient apiKeyRestClient; |
| 18 | + |
| 19 | + private ApiKeySendGrid() { |
| 20 | + } |
| 21 | + |
| 22 | + public static synchronized void init(final String apiKey) { |
| 23 | + if (apiKey == null || apiKey.isEmpty()) { |
| 24 | + throw new AuthenticationException(String.format(ErrorMessages.EMPTY_STRING, "API_KEY")); |
| 25 | + } |
| 26 | + ApiKeySendGrid.apiKey = apiKey; |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + // Explore Data Residency |
| 31 | + /** |
| 32 | + * Set the region. |
| 33 | + * |
| 34 | + * @param region region to make request |
| 35 | + * Global Region api.sendgrid.com |
| 36 | + * EU Region api.eu.sendgrid.com |
| 37 | + */ |
| 38 | + public static synchronized void setRegion(final String region) { |
| 39 | + if (region == null || region.isEmpty()) { |
| 40 | + throw new AuthenticationException(String.format(ErrorMessages.EMPTY_STRING, "REGION")); |
| 41 | + } |
| 42 | + if (!Objects.equals(region, ApiKeySendGrid.region)) { |
| 43 | + ApiKeySendGrid.invalidate(); |
| 44 | + } |
| 45 | + ApiKeySendGrid.region = region; |
| 46 | + } |
| 47 | + |
| 48 | + public static synchronized void setUserAgentExtensions(final List<String> userAgentExtensions) { |
| 49 | + if (userAgentExtensions != null && !userAgentExtensions.isEmpty()) { |
| 50 | + ApiKeySendGrid.userAgentExtensions = new ArrayList<>(userAgentExtensions); |
| 51 | + } else { |
| 52 | + // In case a developer wants to reset userAgentExtensions |
| 53 | + ApiKeySendGrid.userAgentExtensions = null; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + public static ApiKeyRestClient getRestClient() { |
| 59 | + if (ApiKeySendGrid.apiKeyRestClient == null) { |
| 60 | + synchronized (ApiKeySendGrid.class) { |
| 61 | + if (ApiKeySendGrid.apiKeyRestClient == null) { |
| 62 | + ApiKeySendGrid.apiKeyRestClient = buildRestClient(); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return ApiKeySendGrid.apiKeyRestClient; |
| 68 | + } |
| 69 | + |
| 70 | + private static ApiKeyRestClient buildRestClient() { |
| 71 | + if (ApiKeySendGrid.apiKey == null) { |
| 72 | + throw new AuthenticationException( |
| 73 | + "Api Key is not initialized, please call ApiKeySendGrid.init()" |
| 74 | + ); |
| 75 | + } |
| 76 | + ApiKeyRestClient.Builder builder = new ApiKeyRestClient.Builder(ApiKeySendGrid.apiKey); |
| 77 | + if (userAgentExtensions != null) { |
| 78 | + builder.userAgentExtensions(ApiKeySendGrid.userAgentExtensions); |
| 79 | + } |
| 80 | + // TODO: Check if it mandatory to fetch region from customer. |
| 81 | + builder.region(region); |
| 82 | + return builder.build(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Invalidates the volatile state held in the Sendgrid singleton. |
| 87 | + */ |
| 88 | + private static void invalidate() { |
| 89 | + ApiKeySendGrid.apiKeyRestClient = null; |
| 90 | + } |
| 91 | +} |
0 commit comments