|
| 1 | +package io.ipdata.client; |
| 2 | + |
| 3 | +import feign.Client; |
| 4 | +import io.ipdata.client.service.CacheConfig; |
| 5 | +import io.ipdata.client.service.IpdataService; |
| 6 | +import io.ipdata.client.service.IpdataServiceBuilder; |
| 7 | +import java.net.URL; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | +import lombok.AccessLevel; |
| 10 | +import lombok.NoArgsConstructor; |
| 11 | +import lombok.Setter; |
| 12 | +import lombok.experimental.Accessors; |
| 13 | +import lombok.experimental.UtilityClass; |
| 14 | +import org.slf4j.Logger; |
| 15 | + |
| 16 | +@UtilityClass |
| 17 | +public final class Ipdata { |
| 18 | + |
| 19 | + public static Ipdata.Builder builder() { |
| 20 | + return new Builder(); |
| 21 | + } |
| 22 | + |
| 23 | + @Setter |
| 24 | + @Accessors(fluent = true, chain = true) |
| 25 | + @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| 26 | + public static class Builder { |
| 27 | + |
| 28 | + /** |
| 29 | + * The Ipdata API key |
| 30 | + */ |
| 31 | + @Setter |
| 32 | + private String key; |
| 33 | + /** |
| 34 | + * The target URL for the client |
| 35 | + */ |
| 36 | + @Setter |
| 37 | + private URL url; |
| 38 | + /** |
| 39 | + * (Optional) To Cache api calls, a cache configuration can be provided |
| 40 | + */ |
| 41 | + @Setter(AccessLevel.PACKAGE) |
| 42 | + private CacheConfig cacheConfig; |
| 43 | + /** |
| 44 | + * (Optional) Configures a logger to use by the client |
| 45 | + */ |
| 46 | + @Setter |
| 47 | + private Logger logger; |
| 48 | + /** |
| 49 | + * (Optional) Configures a custom Feign client (If you wanna use this with Ribbon or Hystrix for example). |
| 50 | + */ |
| 51 | + @Setter |
| 52 | + private Client feignClient; |
| 53 | + |
| 54 | + /** |
| 55 | + * Configures a cache with default configuration parameters. |
| 56 | + * Note: Overrides any previously configured cache. |
| 57 | + * |
| 58 | + * @return this builder |
| 59 | + */ |
| 60 | + public Builder withDefaultCache() { |
| 61 | + return new CacheConfigBuilder(this) |
| 62 | + .maxSize(Long.MAX_VALUE) |
| 63 | + .timeout(4, TimeUnit.HOURS) |
| 64 | + .registerCacheConfig(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Allows for customizing the configuration of the cache. |
| 69 | + * Note: Overrides any previously configured cache. |
| 70 | + * |
| 71 | + * @return a cache configuration builder |
| 72 | + * @see CacheConfigBuilder |
| 73 | + */ |
| 74 | + public CacheConfigBuilder withCache() { |
| 75 | + return new CacheConfigBuilder(this); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return An Ipdata api facade |
| 80 | + * @see io.ipdata.client.service.IpdataService |
| 81 | + */ |
| 82 | + public IpdataService get() { |
| 83 | + if (key == null) { |
| 84 | + throw new IllegalArgumentException("Key can't be null"); |
| 85 | + } |
| 86 | + if (url == null) { |
| 87 | + throw new IllegalArgumentException("Target URL can't be null"); |
| 88 | + } |
| 89 | + return IpdataServiceBuilder.of(url, key, cacheConfig, logger, feignClient).build(); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments