|
6 | 6 |
|
7 | 7 |  |
8 | 8 |
|
9 | | -# Install |
10 | | -- Using the source code |
11 | | -`mvn clean install` |
| 9 | +# About |
| 10 | +Client Java for working with Switcher-API. |
| 11 | +https://github.com/petruki/switcher-api |
| 12 | + |
| 13 | +- Able to work offline using a snapshot file downloaded from your remote Switcher-API Domain. |
| 14 | +- Silent mode automatically enables a contingent sub-process in case of connectivity issues. |
| 15 | +- Built-in mock implementation for automated testing. |
| 16 | +- Easy to setup. Switcher Context is responsible to manage all the complexity between your application and API. |
| 17 | + |
| 18 | +# Usage |
12 | 19 |
|
| 20 | +## Install |
| 21 | +- Using the source code `mvn clean install` |
13 | 22 | - Adding as a dependency - Maven |
14 | 23 | ```xml |
15 | 24 | <dependency> |
|
19 | 28 | </dependency> |
20 | 29 | ``` |
21 | 30 |
|
22 | | -# About |
23 | | -Client Java for working with Switcher-API. |
24 | | -https://github.com/petruki/switcher-api |
| 31 | +## Context properties |
| 32 | +The context map properties stores all information regarding connectivity and strategy settings. These constants can be accessed using *SwitcherContextParam*. |
25 | 33 |
|
26 | | -Switcher Client is a friendly lib to interact with the Switcher API by: |
27 | | -- Simplifying validations throughout your remote Switcher configuration. |
28 | | -- Able to work offline using a snapshot claimed from your remote Switcher-API. |
29 | | -- Able to run in silent mode that will prevent your application to not be 100% dependent on the online API. |
30 | | -- Being flexible in order to remove the complexity of multi-staging (add as many environments as you want). |
31 | | -- Being friendly by making possible to manipulate switchers without changing your online switchers. (useful for automated tests - see below some examples about bypassing switchers). |
32 | | -- Being secure by using OAuth 2 flow. Requests are made using tokens that will validate your domain, component, environment and API key. |
33 | | -Tokens have an expiration time and are not stored. The Switcher Client is responsible to renew it using your settings. |
34 | | - |
35 | | -# Example |
36 | | -## Client configuration |
37 | 34 | ```java |
38 | | -properties.put(SwitcherContextParam.URL, "http://localhost:3000/criteria"); |
| 35 | +properties.put(SwitcherContextParam.URL, "https://switcher-load-balance.herokuapp.com"); |
39 | 36 | properties.put(SwitcherContextParam.APIKEY, "API_KEY"); |
40 | 37 | properties.put(SwitcherContextParam.DOMAIN, "MyDomain"); |
41 | 38 | properties.put(SwitcherContextParam.COMPONENT, "MyApp"); |
42 | 39 | properties.put(SwitcherContextParam.ENVIRONMENT, "default"); |
43 | 40 | properties.put(SwitcherContextParam.SILENT_MODE, true); |
44 | 41 | properties.put(SwitcherContextParam.RETRY_AFTER, "5s"); |
45 | | -properties.put(SwitcherContextParam.SNAPSHOT_LOCATION, SNAPSHOTS_LOCAL + "default.json"); |
| 42 | +properties.put(SwitcherContextParam.SNAPSHOT_LOCATION, "/src/resources"); |
46 | 43 |
|
47 | 44 | SwitcherFactory.buildContext(properties, false); |
48 | | -Switcher switcher = SwitcherFactory.getSwitcher("FF2FOR2020"); |
| 45 | +Switcher switcher = SwitcherFactory.getSwitcher("FEATURE01"); |
| 46 | +switcher.isItOn(); |
| 47 | +``` |
| 48 | + |
| 49 | +- URL: Endpoint of your Swither-API. |
| 50 | +- APIKEY: Switcher-API key generated after creating a domain. |
| 51 | +- DOMAIN: Domain name. |
| 52 | +- COMPONENT: Application name. |
| 53 | +- ENVIRONMENT: Environment name. Production environment is named as 'default'. |
| 54 | +- SILENT_MODE: (boolean) Activate contingency in case of some problem with connectivity with the API. |
| 55 | +- RETRY_AFTER: Time given to the module to re-establish connectivity with the API - e.g. 5s (s: seconds - m: minutes - h: hours) |
| 56 | +- SNAPSHOT_LOCATION: Set the folder location where snapshot files will be saved. |
| 57 | +- SNAPSHOT_AUTO_LOAD: (boolean) Set the module to automatically download the snapshot configuration. |
| 58 | + |
| 59 | +## Executing |
| 60 | +There are a few different ways to call the API using the java library. |
| 61 | +Here are some examples: |
| 62 | + |
| 63 | +1. **No parameters** |
| 64 | +Invoking the API can be done by obtaining the switcher object and calling *isItOn*. It can also be forced to call another key any time you want. |
| 65 | + |
| 66 | +```java |
| 67 | +Switcher switcher = SwitcherFactory.getSwitcher("FEATURE01"); |
49 | 68 | switcher.isItOn(); |
| 69 | +//or |
| 70 | +switcher.isItOn("FEATURE01"); |
| 71 | +``` |
| 72 | + |
| 73 | +2. **Strategy validation - preparing input** |
| 74 | +Loading information into the switcher can be made by using *prepareEntry*, in case you want to include input from a different place of your code. Otherwise, it is also possible to include everything in the same call. |
| 75 | + |
| 76 | +```java |
| 77 | +List<Entry> entries = new ArrayList<>(); |
| 78 | +entries.add(new Entry(Entry.DATE, "2019-12-10")); |
| 79 | +entries.add(new Entry(Entry.DATE, "2020-12-10")); |
| 80 | + |
| 81 | +switcher.prepareEntry(entries); |
| 82 | +switcher.isItOn(); |
| 83 | +//or |
| 84 | +switcher.isItOn(entries); |
| 85 | +``` |
| 86 | + |
| 87 | +3. **Strategy validation - all-in-one execution** |
| 88 | +All-in-one method is fast and include everything you need to execute a complex call to the API. Stack inputs changing the last parameter to *true* in case you need to add more values to the strategy validator. |
| 89 | + |
| 90 | +```java |
| 91 | +switcher.isItOn("FEATURE01", new Entry(Entry.NETWORK, "10.0.0.3"), false); |
50 | 92 | ``` |
51 | | -- **APIKEY**: Obtained after creating your domain using the Switcher-API project. |
52 | | -- **ENVIRONMENT**: You can run multiple environments. Production environment is 'default' which is created automatically after creating the domain. |
53 | | -- **DOMAIN**: This is your business name identification. |
54 | | -- **COMPONENT**: This is the name of the application that will be using this API. |
55 | | -- **URL**: Endpoint of your Swither-API. |
56 | | -- **SILENT_MODE**: Enable the client work in silent mode if some problem network issues happen. It's necessary to save the snapshot localy. |
57 | | -- **RETRY_AFTER**: Represents the time a retry to reach the API should take. |
58 | 93 |
|
59 | 94 | ## Offline settings |
| 95 | +You can also force the Switcher library to work offline. In this case, the snapshot location must be set up and the context re-built using the offline flag. |
| 96 | + |
60 | 97 | ```java |
61 | | -properties.put(SwitcherContextParam.SNAPSHOT_LOCATION, SNAPSHOTS_LOCAL + "default.json"); |
| 98 | +properties.put(SwitcherContextParam.SNAPSHOT_LOCATION, "/src/resources"); |
62 | 99 | SwitcherFactory.buildContext(properties, true); |
63 | 100 |
|
64 | | -Switcher switcher = SwitcherFactory.getSwitcher("FF2FOR2020"); |
| 101 | +Switcher switcher = SwitcherFactory.getSwitcher("FEATURE01"); |
65 | 102 | switcher.isItOn(); |
66 | 103 | ``` |
67 | 104 |
|
68 | | -## Bypass example |
| 105 | +## Built-in mock feature |
| 106 | +Write automated tests using this built-in mock mechanism to guide your test scenario according to what you want to test. |
| 107 | +</br>*SwitcherExecutor* implementation has 2 methods that can make mock tests easier. Use assume to force a value to a switcher and forget to reset its original state. |
| 108 | + |
69 | 109 | ```java |
70 | | -Switcher switcher = SwitcherFactory.getSwitcher("FF2FOR2020"); |
71 | | -switcher.isItOn(); // Pretending your API or Snapshot return 'true' |
| 110 | +Switcher switcher = SwitcherFactory.getSwitcher("FEATURE01"); |
72 | 111 |
|
73 | | -switcher.assume("FF2FOR2020", false); |
74 | | -switcher.isItOn(); // Now, it's going to return 'false' |
| 112 | +SwitcherExecutor.assume("FEATURE01", false); |
| 113 | +switcher.isItOn(); // 'false' |
75 | 114 |
|
76 | | -switcher.forget("FF2FOR2020"); |
77 | | -switcher.isItOn(); // Now, it's going to return 'true' |
| 115 | +SwitcherExecutor.forget("FEATURE01"); |
| 116 | +switcher.isItOn(); // Now, it's going to return the result retrieved from the API or the Snaopshot file |
78 | 117 | ``` |
79 | 118 |
|
80 | 119 | # Version Log |
| 120 | +- 1.0.2: |
| 121 | + - Improved performance when loading snapshot file. |
| 122 | + - Snapshot file auto load when updated. |
| 123 | + - Re-worked built-in mock implementation |
81 | 124 | - 1.0.1: Security patch - Log4J has been updated |
82 | 125 | - 1.0.0: Working release |
0 commit comments