Skip to content

Commit 72cd7df

Browse files
authored
Merge pull request #38 from securenative/dev
Update readme
2 parents 70a8801 + 43608be commit 72cd7df

File tree

1 file changed

+132
-66
lines changed

1 file changed

+132
-66
lines changed

README.md

Lines changed: 132 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,84 @@ 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+
@RequestMapping("/track")
115+
public void track() {
116+
SecureNative securenative = null;
117+
try {
118+
securenative = SecureNative.getInstance();
119+
} catch (SecureNativeSDKIllegalStateException e) {
120+
System.err.printf("Could not get SecureNative instance; %s%n", e);
121+
}
122+
123+
SecureNativeContext context = SecureNative.contextBuilder()
124+
.withIp("37.86.255.94")
125+
.withClientToken("SECURENATIVE_CLIENT_TOKEN")
126+
.withHeaders(Maps.defaultBuilder()
127+
.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")
100128
.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);
129+
.build();
130+
131+
EventOptions eventOptions = null;
132+
try {
133+
eventOptions = EventOptionsBuilder.builder(EventTypes.LOG_IN)
134+
.userId("1234")
135+
.userTraits("Your Name", "name@gmail.com", "+1234567890")
136+
.context(context)
137+
.properties(Maps.builder()
138+
.put("prop1", "CUSTOM_PARAM_VALUE")
139+
.put("prop2", true)
140+
.put("prop3", 3)
141+
.build())
142+
.timestamp(new Date())
143+
.build();
144+
} catch (SecureNativeInvalidOptionsException e) {
145+
e.printStackTrace();
146+
}
147+
148+
try {
149+
securenative.track(eventOptions);
150+
} catch (SecureNativeInvalidOptionsException e) {
151+
e.printStackTrace();
152+
}
153+
}
116154
```
117155

118156
You can also create request context from HttpServletRequest:
119157

120158
```java
121159
@RequestMapping("/track")
122160
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);
161+
SecureNative securenative = null;
162+
try {
163+
securenative = SecureNative.getInstance();
164+
} catch (SecureNativeSDKIllegalStateException e) {
165+
System.err.printf("Could not get SecureNative instance; %s%n", e);
166+
}
167+
168+
SecureNativeContext context = SecureNativeContextBuilder.fromHttpServletRequest(request).build();
169+
170+
EventOptions eventOptions = null;
171+
try {
172+
eventOptions = EventOptionsBuilder.builder(EventTypes.LOG_IN)
173+
.userId("1234")
174+
.userTraits("Your Name", "name@gmail.com", "+1234567890")
175+
.context(context)
176+
.properties(Maps.builder()
177+
.put("prop1", "CUSTOM_PARAM_VALUE")
178+
.put("prop2", true)
179+
.put("prop3", 3)
180+
.build())
181+
.timestamp(new Date())
182+
.build();
183+
} catch (SecureNativeInvalidOptionsException e) {
184+
e.printStackTrace();
185+
}
186+
187+
try {
188+
securenative.track(eventOptions);
189+
} catch (SecureNativeInvalidOptionsException e) {
190+
e.printStackTrace();
191+
}
140192
}
141193
```
142194

@@ -147,25 +199,34 @@ public void track(HttpServletRequest request, HttpServletResponse response) {
147199
```java
148200
@RequestMapping("/verify")
149201
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();
202+
SecureNativeContext context = SecureNativeContextBuilder.fromHttpServletRequest(request).build();
165203

166-
VerifyResult verifyResult = securenative.verify(eventOptions);
204+
EventOptions eventOptions = null;
205+
try {
206+
eventOptions = EventOptionsBuilder.builder(EventTypes.LOG_IN)
207+
.userId("1234")
208+
.userTraits("Your Name", "name@gmail.com", "+1234567890")
209+
.context(context)
210+
.properties(Maps.builder()
211+
.put("prop1", "CUSTOM_PARAM_VALUE")
212+
.put("prop2", true)
213+
.put("prop3", 3)
214+
.build())
215+
.timestamp(new Date())
216+
.build();
217+
} catch (SecureNativeInvalidOptionsException e) {
218+
e.printStackTrace();
219+
}
220+
221+
VerifyResult verifyResult = null;
222+
try {
223+
verifyResult = securenative.verify(eventOptions);
224+
} catch (SecureNativeInvalidOptionsException e) {
225+
e.printStackTrace();
226+
}
227+
167228
verifyResult.getRiskLevel(); // Low, Medium, High
168-
verifyResult.score(); // Risk score: 0 -1 (0 - Very Low, 1 - Very High)
229+
verifyResult.getScore(); // Risk score: 0 -1 (0 - Very Low, 1 - Very High)
169230
verifyResult.getTriggers(); // ["TOR", "New IP", "New City"]
170231
}
171232
```
@@ -177,7 +238,12 @@ Apply our filter to verify the request is from us, example in spring:
177238
```java
178239
@RequestMapping("/webhook")
179240
public void webhookEndpoint(HttpServletRequest request, HttpServletResponse response) {
180-
SecureNative securenative = SecureNative.getInstance();
241+
SecureNative securenative = null;
242+
try {
243+
securenative = SecureNative.getInstance();
244+
} catch (SecureNativeSDKIllegalStateException e) {
245+
System.err.printf("Could not get SecureNative instance; %s%n", e);
246+
}
181247

182248
// Checks if request is verified
183249
Boolean isVerified = securenative.verifyRequestPayload(request);

0 commit comments

Comments
 (0)