Skip to content

Commit 4cec761

Browse files
committed
Update readme code samples based on previous versions
1 parent ca409ae commit 4cec761

1 file changed

Lines changed: 108 additions & 29 deletions

File tree

README.md

Lines changed: 108 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,42 +83,121 @@ Then manually install the following JARs:
8383

8484
Please follow the [installation](#installation) instruction and execute the following Java code:
8585

86+
### Integration API
87+
88+
**Note:** The Integration API's V1 `Update customer session` and `Update customer profile` endpoints are now deprecated. Use their V2 instead. See [Migrating to V2](https://docs.talon.one/docs/dev/tutorials/migrating-to-v2) for more information.
89+
8690
```java
91+
package com.example.consumer;
92+
93+
import com.google.gson.Gson;
8794

88-
// Import classes:
8995
import one.talon.ApiClient;
90-
import one.talon.ApiException;
91-
import one.talon.Configuration;
92-
import one.talon.auth.*;
93-
import one.talon.models.*;
9496
import one.talon.api.IntegrationApi;
95-
96-
public class Example {
97-
public static void main(String[] args) {
98-
ApiClient defaultClient = Configuration.getDefaultApiClient();
99-
defaultClient.setBasePath("https://yourbaseurl.talon.one");
100-
101-
// Configure API key authorization: api_key_v1
102-
ApiKeyAuth api_key_v1 = (ApiKeyAuth) defaultClient.getAuthentication("api_key_v1");
103-
api_key_v1.setApiKey("YOUR API KEY");
104-
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
105-
//api_key_v1.setApiKeyPrefix("Token");
106-
107-
IntegrationApi apiInstance = new IntegrationApi(defaultClient);
108-
NewAudience body = new NewAudience(); // NewAudience | body
109-
try {
110-
Audience result = apiInstance.createAudienceV2(body);
111-
System.out.println(result);
112-
} catch (ApiException e) {
113-
System.err.println("Exception when calling IntegrationApi#createAudienceV2");
114-
System.err.println("Status code: " + e.getCode());
115-
System.err.println("Reason: " + e.getResponseBody());
116-
System.err.println("Response headers: " + e.getResponseHeaders());
117-
e.printStackTrace();
97+
import one.talon.api.ManagementApi;
98+
import one.talon.model.*;
99+
100+
public class TalonApiTest {
101+
public static void main(String[] args) {
102+
Gson gson = new Gson();
103+
IntegrationApi iApi = new IntegrationApi(new ApiClient("api_key_v1"));
104+
105+
// Setup: basePath
106+
iApi.getApiClient().setBasePath("https://yourbaseurl.talon.one");
107+
// Setup: when using 'api_key_v1', set apiKey & apiKeyPrefix must be provided
108+
iApi.getApiClient().setApiKeyPrefix("ApiKey-v1");
109+
iApi.getApiClient().setApiKey("dbc644d33aa74d582bd9479c59e16f970fe13bf34a208c39d6c7fa7586968468");
110+
111+
try {
112+
// Creating a cart item object
113+
CartItem cartItem = new CartItem();
114+
cartItem.setName("Hawaiian Pizza");
115+
cartItem.setSku("pizza-x");
116+
cartItem.setQuantity(1);
117+
cartItem.setPrice(new java.math.BigDecimal("5.5"));
118+
119+
// Creating a customer session of V2
120+
NewCustomerSessionV2 customerSession = new NewCustomerSessionV2();
121+
customerSession.setProfileId("Cool_Dude");
122+
customerSession.addCouponCodesItem("Cool-Summer!");
123+
customerSession.addCartItemsItem(cartItem);
124+
125+
// Initiating integration request wrapping the customer session update
126+
IntegrationRequest request = new IntegrationRequest()
127+
.customerSession(customerSession)
128+
// Optional parameter of requested information to be present on the response related to the customer session update
129+
.responseContent(Arrays.asList(
130+
IntegrationRequest.ResponseContentEnum.CUSTOMERSESSION,
131+
IntegrationRequest.ResponseContentEnum.CUSTOMERPROFILE
132+
));
133+
134+
// Flag to communicate whether the request is a "dry run"
135+
Boolean dryRun = false;
136+
137+
// Create/update a customer session using `updateCustomerSessionV2` function
138+
IntegrationStateV2 is = iApi.updateCustomerSessionV2("deetdoot", request, dryRun);
139+
System.out.println(is.toString());
140+
141+
// Parsing the returned effects list, please consult https://developers.talon.one/Integration-API/handling-effects-v2 for the full list of effects and their corresponding properties
142+
for (Effect eff : is.getEffects()) {
143+
if (eff.getEffectType().equals("addLoyaltyPoints")) {
144+
// Typecasting according to the specific effect type
145+
AddLoyaltyPointsEffectProps props = gson.fromJson(
146+
gson.toJson(eff.getProps()),
147+
AddLoyaltyPointsEffectProps.class
148+
);
149+
// Access the specific effect's properties
150+
System.out.println(props.getName());
151+
System.out.println(props.getProgramId());
152+
System.out.println(props.getValue());
153+
}
154+
if (eff.getEffectType().equals("acceptCoupon")) {
155+
// Typecasting according to the specific effect type
156+
AcceptCouponEffectProps props = gson.fromJson(
157+
gson.toJson(eff.getProps()),
158+
AcceptCouponEffectProps.class
159+
);
160+
// work with AcceptCouponEffectProps' properties
161+
// ...
162+
}
163+
}
164+
} catch (Exception e) {
165+
System.out.println(e);
166+
}
118167
}
119-
}
120168
}
169+
```
170+
171+
### Management API
121172

173+
```java
174+
package com.example.consumer;
175+
176+
import one.talon.ApiClient;
177+
import one.talon.api.IntegrationApi;
178+
import one.talon.api.ManagementApi;
179+
import one.talon.model.*;
180+
181+
public class TalonApiTest {
182+
public static void main(String[] args) {
183+
// Management API example to load application with id 7
184+
ManagementApi mApi = new ManagementApi(new ApiClient("management_key"));
185+
186+
// Setup: basePath
187+
mApi.getApiClient().setBasePath("https://yourbaseurl.talon.one");
188+
// Setup: when using 'management_key', set apiKey & apiKeyPrefix must be provided
189+
mApi.getApiClient().setApiKeyPrefix("ManagementKey-v1");
190+
mApi.getApiClient().setApiKey("2f0dce055da01ae595005d7d79154bae7448d319d5fc7c5b2951fadd6ba1ea07");
191+
192+
try {
193+
// Calling `getApplication` function with the desired id (7)
194+
Application application = mApi.getApplication(7);
195+
System.out.println(application.toString());
196+
} catch (Exception e) {
197+
System.out.println(e);
198+
}
199+
}
200+
}
122201
```
123202

124203
## Documentation for API Endpoints

0 commit comments

Comments
 (0)