|
1 | 1 | (ns unleash-client-clojure.builder
|
| 2 | + "Create and configure builders that build instances of UnleashConfig." |
2 | 3 | (:import [no.finn.unleash CustomHttpHeadersProvider UnleashContextProvider]
|
3 | 4 | [no.finn.unleash.util UnleashConfig UnleashConfig$Builder UnleashScheduledExecutor]
|
4 | 5 | [no.finn.unleash.event UnleashSubscriber]))
|
5 | 6 |
|
6 |
| -(defn build ^UnleashConfig [& fs] |
7 |
| - (let [bldr (UnleashConfig$Builder.)] |
| 7 | +(defn build |
| 8 | + "Expects to be applied with a variadic number of arguments, each of which is a function that expects an |
| 9 | + UnleashConfig$Builder instance. |
| 10 | + Returns an instance of UnleashConfig that had all of its parameters set by said functions. |
| 11 | +
|
| 12 | + Using this building pattern allows users to manipulate the builder instance in ways that aren't |
| 13 | + implemented in this library by passing a function that expects an UnleashConfig$Builder. |
| 14 | +
|
| 15 | + Example: |
| 16 | + (build (app-name \"test-app\") |
| 17 | + (unleash-api \"http://example.unleash.com/api\"))" |
| 18 | + ^UnleashConfig [& builder-param-setters] |
| 19 | + (let [builder (UnleashConfig$Builder.)] |
8 | 20 | (.build ^UnleashConfig$Builder
|
9 | 21 | (reduce
|
10 | 22 | (fn
|
11 |
| - ([] bldr) |
12 |
| - ([bldr f] |
13 |
| - (f bldr))) |
14 |
| - bldr |
15 |
| - fs)))) |
16 |
| - |
17 |
| -(defn app-name [^String name] |
18 |
| - (fn [^UnleashConfig$Builder bldr] |
19 |
| - (.appName bldr name))) |
20 |
| - |
21 |
| -(defn instance-id [^String id] |
22 |
| - (fn [^UnleashConfig$Builder bldr] |
23 |
| - (.instanceId bldr id))) |
24 |
| - |
25 |
| -(defn unleash-api [^String api] |
26 |
| - (fn [^UnleashConfig$Builder bldr] |
27 |
| - (.unleashAPI bldr api))) |
28 |
| - |
29 |
| -(defn custom-http-header [header-name header-value] |
30 |
| - (fn [^UnleashConfig$Builder bldr] |
31 |
| - (.customHttpHeader bldr header-name header-value))) |
32 |
| - |
33 |
| -(defn send-metrics-interval [^Long interval-seconds] |
34 |
| - (fn [^UnleashConfig$Builder bldr] |
35 |
| - (.sendMetricsInterval bldr interval-seconds))) |
36 |
| - |
37 |
| -(defn fetch-toggles-interval [^Long interval-seconds] |
38 |
| - (fn [^UnleashConfig$Builder bldr] |
39 |
| - (.fetchTogglesInterval bldr interval-seconds))) |
40 |
| - |
41 |
| -(defn synchronous-fetch-on-initialisation [enable?] |
42 |
| - (fn [^UnleashConfig$Builder bldr] |
43 |
| - (.synchronousFetchOnInitialisation bldr enable?))) |
44 |
| - |
45 |
| -(defn enable-proxy-authentication-by-jvm-properties [] |
46 |
| - (fn [^UnleashConfig$Builder bldr] |
47 |
| - (.enableProxyAuthenticationByJvmProperties bldr))) |
48 |
| - |
49 |
| -(defn backup-file [^String backup-file] |
50 |
| - (fn [^UnleashConfig$Builder bldr] |
51 |
| - (.backupFile bldr backup-file))) |
52 |
| - |
53 |
| -(defn environment [^String environment] |
54 |
| - (fn [^UnleashConfig$Builder bldr] |
55 |
| - (.environment bldr environment))) |
56 |
| - |
57 |
| -(defn custom-http-header-provider [^CustomHttpHeadersProvider provider] |
58 |
| - (fn [^UnleashConfig$Builder bldr] |
59 |
| - (.customHttpHeadersProvider bldr provider))) |
60 |
| - |
61 |
| -(defn unleash-context-provider [^UnleashContextProvider provider] |
62 |
| - (fn [^UnleashConfig$Builder bldr] |
63 |
| - (.unleashContextProvider bldr provider))) |
64 |
| - |
65 |
| -(defn scheduled-executor [^UnleashScheduledExecutor executor] |
66 |
| - (fn [^UnleashConfig$Builder bldr] |
67 |
| - (.scheduledExecutor bldr executor))) |
68 |
| - |
69 |
| -(defn subscriber [^UnleashSubscriber subscriber] |
70 |
| - (fn [^UnleashConfig$Builder bldr] |
71 |
| - (.subscriber bldr subscriber))) |
| 23 | + ([] builder) |
| 24 | + ([builder set-builder-param] |
| 25 | + (set-builder-param builder))) |
| 26 | + builder |
| 27 | + builder-param-setters)))) |
| 28 | + |
| 29 | +(defn app-name |
| 30 | + "Expects an application name. |
| 31 | + Returns a function that expects an UnleashConfig$Builder, and sets the appName property of the builder." |
| 32 | + [^String name] |
| 33 | + (fn [^UnleashConfig$Builder builder] |
| 34 | + (.appName builder name))) |
| 35 | + |
| 36 | +(defn instance-id |
| 37 | + "Expects an instance ID. |
| 38 | + Returns a function that expects an UnleashConfig$Builder, and sets the instanceId property of the builder." |
| 39 | + [^String id] |
| 40 | + (fn [^UnleashConfig$Builder builder] |
| 41 | + (.instanceId builder id))) |
| 42 | + |
| 43 | +(defn unleash-api |
| 44 | + "Expects a URL for an Unleash server. |
| 45 | + Returns a function that expects an UnleashConfig$Builder, and sets the unleashAPI property of the builder." |
| 46 | + [^String api] |
| 47 | + (fn [^UnleashConfig$Builder builder] |
| 48 | + (.unleashAPI builder api))) |
| 49 | + |
| 50 | +(defn custom-http-header |
| 51 | + "Expects an HTTP header name and value. |
| 52 | + Returns a function that expects an UnleashConfig$Builder, and adds the relevant HTTP header name and value to the headers used by the builder. |
| 53 | + These headers would be added to outgoing HTTP requests sent by the Unleash client to the Unleash server. |
| 54 | +
|
| 55 | + Can be used multiple times to set multiple headers. |
| 56 | +
|
| 57 | + Example: |
| 58 | + (build [(unleash-api \"http://example.unleash.com/api\") |
| 59 | + (custom-http-header \"X-AF-HEADER-1\" \"a\") |
| 60 | + (custom-http-header \"X-AF-HEADER-2\" \"b\") |
| 61 | + (custom-http-header \"X-AF-HEADER-3\" \"c\")])" |
| 62 | + [header-name header-value] |
| 63 | + (fn [^UnleashConfig$Builder builder] |
| 64 | + (.customHttpHeader builder header-name header-value))) |
| 65 | + |
| 66 | +(defn send-metrics-interval |
| 67 | + "Expects a long that marks the interval (in seconds) of sending metrics. |
| 68 | + Returns a function that expects an UnleashConfig$Builder, and sets the sendMetricsInterval property of the builder." |
| 69 | + [^Long interval-seconds] |
| 70 | + (fn [^UnleashConfig$Builder builder] |
| 71 | + (.sendMetricsInterval builder interval-seconds))) |
| 72 | + |
| 73 | +(defn fetch-toggles-interval |
| 74 | + "Expects a long that marks the interval (in seconds) of fetching toggles' states from the Unleash server. |
| 75 | + Returns a function that expects an UnleashConfig$Builder, and sets the fetchTogglesInterval property of the builder." |
| 76 | + [^Long interval-seconds] |
| 77 | + (fn [^UnleashConfig$Builder builder] |
| 78 | + (.fetchTogglesInterval builder interval-seconds))) |
| 79 | + |
| 80 | +(defn synchronous-fetch-on-initialisation |
| 81 | + "Expects a boolean that controls if toggles are fetched synchronously when the Unleash client is initialised. |
| 82 | + Returns a function that expects an UnleashConfig$Builder, and sets the synchronousFetchOnInitialisation property of |
| 83 | + the builder." |
| 84 | + [enable?] |
| 85 | + (fn [^UnleashConfig$Builder builder] |
| 86 | + (.synchronousFetchOnInitialisation builder enable?))) |
| 87 | + |
| 88 | +(defn enable-proxy-authentication-by-jvm-properties |
| 89 | + "Expects a boolean that controls if authentication against an HTTP proxy would use the JVM settings http.proxyUser and |
| 90 | + http.proxyPassword. |
| 91 | + Returns a function that expects an UnleashConfig$Builder, and sets the isProxyAuthenticationByJvmProperties property of the builder." |
| 92 | + [] |
| 93 | + (fn [^UnleashConfig$Builder builder] |
| 94 | + (.enableProxyAuthenticationByJvmProperties builder))) |
| 95 | + |
| 96 | +(defn backup-file |
| 97 | + "Expects a backup file path, into which the Unleash client would backup its state to be used when the Unleash server |
| 98 | + is unavailable. By defeault this would be unleash-repo.json in the directory set by the JVM property java.io.tmpdir. |
| 99 | + Returns a function that expects an UnleashConfig$Builder, and sets the backupFile property of the builder." |
| 100 | + [^String backup-file] |
| 101 | + (fn [^UnleashConfig$Builder builder] |
| 102 | + (.backupFile builder backup-file))) |
| 103 | + |
| 104 | +(defn environment |
| 105 | + "Expects an application environment. |
| 106 | + Returns a function that expects an UnleashConfig$Builder, and sets the environment property of the builder." |
| 107 | + [^String environment] |
| 108 | + (fn [^UnleashConfig$Builder builder] |
| 109 | + (.environment builder environment))) |
| 110 | + |
| 111 | +(defn custom-http-header-provider |
| 112 | + "Expects an instance of CustomHttpHeadersProvider, which adds headers to outgoing HTTP requests that are sent to the |
| 113 | + Unleash server, based on the contents of the request itself. |
| 114 | + Returns a function that expects an UnleashConfig$Builder, and sets the custom-http-headers-provider property of the |
| 115 | + builder." |
| 116 | + [^CustomHttpHeadersProvider provider] |
| 117 | + (fn [^UnleashConfig$Builder builder] |
| 118 | + (.customHttpHeadersProvider builder provider))) |
| 119 | + |
| 120 | + |
| 121 | +(defn unleash-context-provider |
| 122 | + "Expects an instance of UnleashContextProvider, which would provide the context for calls of enabled? instead of |
| 123 | + having it provided as an arguments in the call site. |
| 124 | + Returns a function that expects an UnleashConfig$Builder, and sets the contextProvider property of the builder." |
| 125 | + [^UnleashContextProvider provider] |
| 126 | + (fn [^UnleashConfig$Builder builder] |
| 127 | + (.unleashContextProvider builder provider))) |
| 128 | + |
| 129 | +(defn scheduled-executor |
| 130 | + "Expects an instance of UnleashScheduledExecutor, which would be used to periodically send usage metrics. The interval |
| 131 | + of sending those metrics can be set with 'send-metrics-interval'. |
| 132 | + Returns a function that expects an UnleashConfig$Builder, and sets the scheduledExecutor property of the builder." |
| 133 | + [^UnleashScheduledExecutor executor] |
| 134 | + (fn [^UnleashConfig$Builder builder] |
| 135 | + (.scheduledExecutor builder executor))) |
| 136 | + |
| 137 | +(defn subscriber |
| 138 | + "Expects an instance of UnleashSubscriber, which would be notified when the Unleash client's internal state changes. |
| 139 | + Returns a function that expects an UnleashConfig$Builder, and sets the subscriber property of the builder." |
| 140 | + [^UnleashSubscriber subscriber] |
| 141 | + (fn [^UnleashConfig$Builder builder] |
| 142 | + (.subscriber builder subscriber))) |
0 commit comments