Skip to content

Commit 456287e

Browse files
author
Inbal Tako
committed
Update readme
1 parent 70a8801 commit 456287e

1 file changed

Lines changed: 131 additions & 66 deletions

File tree

README.md

Lines changed: 131 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,52 @@ SecureNative can automatically load your config from *securenative.properties* f
5757

5858
```java
5959
// Options 1: Use default config file path
60-
SecureNative securenative = SecureNative.init();
60+
try {
61+
SecureNative securenative = SecureNative.init();
62+
} catch (SecureNativeSDKException | SecureNativeConfigException e) {
63+
e.printStackTrace();
64+
}
6165

6266
// Options 2: Use specific config file path
6367
Path path = Paths.get("/path/to/securenative.properties");
64-
SecureNative securenative = SecureNative.init(path);
68+
try {
69+
SecureNative.init(path);
70+
} catch (SecureNativeSDKException | SecureNativeConfigException e) {
71+
System.err.printf("Could not initialize SecureNative sdk; %s%n", e);
72+
}
6573
```
6674
### Option 2: Initialize via API Key
6775

6876
```java
69-
SecureNative securenative = SecureNative.init("YOUR_API_KEY");
77+
try {
78+
SecureNative securenative = SecureNative.init("YOUR_API_KEY");
79+
} catch (SecureNativeSDKException | SecureNativeConfigException e) {
80+
e.printStackTrace();
81+
}
7082
```
7183

7284
### Option 3: Initialize via ConfigurationBuilder
7385
```java
74-
SecureNative securenative = SecureNative.init(SecureNative.configBuilder()
75-
.withApiKey("API_KEY")
76-
.withMaxEvents(10)
77-
.withLogLevel("error")
78-
.build());
86+
try {
87+
securenative = SecureNative.init(SecureNative.configBuilder()
88+
.withApiKey("API_KEY")
89+
.withMaxEvents(10)
90+
.withLogLevel("error")
91+
.build());
92+
} catch (SecureNativeSDKException e) {
93+
e.printStackTrace();
94+
}
7995
```
8096

8197
## Getting SecureNative instance
8298
Once initialized, sdk will create a singleton instance which you can get:
8399
```java
84-
SecureNative securenative = SecureNative.getInstance();
100+
SecureNative securenative = null;
101+
try {
102+
securenative = SecureNative.getInstance();
103+
} catch (SecureNativeSDKIllegalStateException e) {
104+
System.err.printf("Could not get SecureNative instance; %s%n", e);
105+
}
85106
```
86107

87108
## Tracking events
@@ -90,53 +111,83 @@ Once the SDK has been initialized, tracking requests sent through the SDK
90111
instance. Make sure you build event with the EventBuilder:
91112

92113
```java
93-
SecureNative securenative = SecureNative.getInstance();
94-
95-
SecureNativeContext context = SecureNative.contextBuilder()
96-
.withIp("127.0.0.1")
97-
.withClientToken("SECURED_CLIENT_TOKEN")
98-
.withHeaders(Maps.defaultBuilder()
99-
.put("user-agent", "Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405")
114+
public void track() {
115+
SecureNative securenative = null;
116+
try {
117+
securenative = SecureNative.getInstance();
118+
} catch (SecureNativeSDKIllegalStateException e) {
119+
System.err.printf("Could not get SecureNative instance; %s%n", e);
120+
}
121+
122+
SecureNativeContext context = SecureNative.contextBuilder()
123+
.withIp("37.86.255.94")
124+
.withClientToken("SECURENATIVE_CLIENT_TOKEN")
125+
.withHeaders(Maps.defaultBuilder()
126+
.put("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36")
100127
.build())
101-
.build();
102-
103-
EventOptions eventOptions = EventOptionsBuilder.builder(EventTypes.LOG_IN)
104-
.userId("USER_ID")
105-
.userTraits("USER_NAME", "USER_EMAIL", "+01234566789")
106-
.context(context)
107-
.properties(Maps.builder()
108-
.put("prop1", "CUSTOM_PARAM_VALUE")
109-
.put("prop2", true)
110-
.put("prop3", 3)
111-
.build())
112-
.timestamp(new Date())
113-
.build();
114-
115-
securenative.track(eventOptions);
128+
.build();
129+
130+
EventOptions eventOptions = null;
131+
try {
132+
eventOptions = EventOptionsBuilder.builder(EventTypes.LOG_IN)
133+
.userId("11")
134+
.userTraits("track02", "t@somemail.com", "+01234566789")
135+
.context(context)
136+
.properties(Maps.builder()
137+
.put("prop1", "CUSTOM_PARAM_VALUE")
138+
.put("prop2", true)
139+
.put("prop3", 3)
140+
.build())
141+
.timestamp(new Date())
142+
.build();
143+
} catch (SecureNativeInvalidOptionsException e) {
144+
e.printStackTrace();
145+
}
146+
147+
try {
148+
securenative.track(eventOptions);
149+
} catch (SecureNativeInvalidOptionsException e) {
150+
e.printStackTrace();
151+
}
152+
}
116153
```
117154

118155
You can also create request context from HttpServletRequest:
119156

120157
```java
121158
@RequestMapping("/track")
122159
public void track(HttpServletRequest request, HttpServletResponse response) {
123-
SecureNativeContext context = SecureNative.contextBuilder()
124-
.fromHttpServletRequest(request)
125-
.build();
126-
127-
EventOptions eventOptions = EventOptionsBuilder.builder(EventTypes.LOG_IN)
128-
.userId("USER_ID")
129-
.userTraits("USER_NAME", "USER_EMAIL", "+01234566789")
130-
.context(context)
131-
.properties(Maps.builder()
132-
.put("prop1", "CUSTOM_PARAM_VALUE")
133-
.put("prop2", true)
134-
.put("prop3", 3)
135-
.build())
136-
.timestamp(new Date())
137-
.build();
138-
139-
securenative.track(eventOptions);
160+
SecureNative securenative = null;
161+
try {
162+
securenative = SecureNative.getInstance();
163+
} catch (SecureNativeSDKIllegalStateException e) {
164+
System.err.printf("Could not get SecureNative instance; %s%n", e);
165+
}
166+
167+
SecureNativeContext context = SecureNativeContextBuilder.fromHttpServletRequest(request).build();
168+
169+
EventOptions eventOptions = null;
170+
try {
171+
eventOptions = EventOptionsBuilder.builder(EventTypes.LOG_IN)
172+
.userId("USER_ID")
173+
.userTraits("USER_NAME", "USER_EMAIL", "+01234566789")
174+
.context(context)
175+
.properties(Maps.builder()
176+
.put("prop1", "CUSTOM_PARAM_VALUE")
177+
.put("prop2", true)
178+
.put("prop3", 3)
179+
.build())
180+
.timestamp(new Date())
181+
.build();
182+
} catch (SecureNativeInvalidOptionsException e) {
183+
e.printStackTrace();
184+
}
185+
186+
try {
187+
securenative.track(eventOptions);
188+
} catch (SecureNativeInvalidOptionsException e) {
189+
e.printStackTrace();
190+
}
140191
}
141192
```
142193

@@ -147,25 +198,34 @@ public void track(HttpServletRequest request, HttpServletResponse response) {
147198
```java
148199
@RequestMapping("/verify")
149200
public void verify(HttpServletRequest request, HttpServletResponse response) {
150-
SecureNativeContext context = SecureNative.contextBuilder()
151-
.fromHttpServletRequest(request)
152-
.build();
153-
154-
EventOptions eventOptions = EventOptionsBuilder.builder(EventTypes.LOG_IN)
155-
.userId("USER_ID")
156-
.userTraits("USER_NAME", "USER_EMAIL", "+01234566789")
157-
.context(context)
158-
.properties(Maps.builder()
159-
.put("prop1", "CUSTOM_PARAM_VALUE")
160-
.put("prop2", true)
161-
.put("prop3", 3)
162-
.build())
163-
.timestamp(new Date())
164-
.build();
201+
SecureNativeContext context = SecureNativeContextBuilder.fromHttpServletRequest(request).build();
165202

166-
VerifyResult verifyResult = securenative.verify(eventOptions);
203+
EventOptions eventOptions = null;
204+
try {
205+
eventOptions = EventOptionsBuilder.builder(EventTypes.LOG_IN)
206+
.userId("USER_ID")
207+
.userTraits("USER_NAME", "USER_EMAIL", "+01234566789")
208+
.context(context)
209+
.properties(Maps.builder()
210+
.put("prop1", "CUSTOM_PARAM_VALUE")
211+
.put("prop2", true)
212+
.put("prop3", 3)
213+
.build())
214+
.timestamp(new Date())
215+
.build();
216+
} catch (SecureNativeInvalidOptionsException e) {
217+
e.printStackTrace();
218+
}
219+
220+
VerifyResult verifyResult = null;
221+
try {
222+
verifyResult = securenative.verify(eventOptions);
223+
} catch (SecureNativeInvalidOptionsException e) {
224+
e.printStackTrace();
225+
}
226+
167227
verifyResult.getRiskLevel(); // Low, Medium, High
168-
verifyResult.score(); // Risk score: 0 -1 (0 - Very Low, 1 - Very High)
228+
verifyResult.getScore(); // Risk score: 0 -1 (0 - Very Low, 1 - Very High)
169229
verifyResult.getTriggers(); // ["TOR", "New IP", "New City"]
170230
}
171231
```
@@ -177,7 +237,12 @@ Apply our filter to verify the request is from us, example in spring:
177237
```java
178238
@RequestMapping("/webhook")
179239
public void webhookEndpoint(HttpServletRequest request, HttpServletResponse response) {
180-
SecureNative securenative = SecureNative.getInstance();
240+
SecureNative securenative = null;
241+
try {
242+
securenative = SecureNative.getInstance();
243+
} catch (SecureNativeSDKIllegalStateException e) {
244+
System.err.printf("Could not get SecureNative instance; %s%n", e);
245+
}
181246

182247
// Checks if request is verified
183248
Boolean isVerified = securenative.verifyRequestPayload(request);

0 commit comments

Comments
 (0)