-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
PersonalizerAsyncClient.java
321 lines (297 loc) · 14.8 KB
/
PersonalizerAsyncClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.ai.personalizer;
import com.azure.ai.personalizer.implementation.PersonalizerClientV1Preview3Impl;
import com.azure.ai.personalizer.implementation.util.Transforms;
import com.azure.ai.personalizer.models.PersonalizerRankMultiSlotOptions;
import com.azure.ai.personalizer.models.PersonalizerRankMultiSlotResult;
import com.azure.ai.personalizer.models.PersonalizerRankOptions;
import com.azure.ai.personalizer.models.PersonalizerRankResult;
import com.azure.ai.personalizer.models.PersonalizerRewardMultiSlotOptions;
import com.azure.ai.personalizer.models.PersonalizerRewardOptions;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.FluxUtil;
import com.azure.core.util.logging.ClientLogger;
import reactor.core.publisher.Mono;
import static com.azure.core.util.FluxUtil.monoError;
import static com.azure.core.util.FluxUtil.withContext;
/**
* This class provides an asynchronous client that contains the operations that apply to Azure Personalizer.
* Operations allowed by the client are ranking a set of actions, activation and sending rewards for a single
* slot as well as multi-slot scenarios.
*
* <p><strong>Instantiating an asynchronous Personalizer Client</strong></p>
* <p>
* <!-- src_embed com.azure.ai.personalizer.PersonalizerAsyncClient.instantiation -->
* <pre>
* PersonalizerAsyncClient personalizerClient = new PersonalizerClientBuilder()
* .credential(new AzureKeyCredential("{key}"))
* .endpoint("{endpoint}")
* .buildAsyncClient();
* </pre>
* <!-- end com.azure.ai.personalizer.PersonalizerAsyncClient.instantiation -->
*
* @see PersonalizerClientBuilder
*/
@ServiceClient(builder = PersonalizerClientBuilder.class, isAsync = true)
public final class PersonalizerAsyncClient {
private final ClientLogger logger = new ClientLogger(PersonalizerAsyncClient.class);
private final PersonalizerClientV1Preview3Impl service;
PersonalizerAsyncClient(PersonalizerClientV1Preview3Impl service) {
this.service = service;
}
/**
* Request a list of actions to be ranked.
*
* <p>Submit a Personalizer rank request. Receives a context and a list of actions. Returns which of the provided
* actions should be used by your application, in rewardActionId.
*
* @param rankRequest A Personalizer Rank request.
* @return returns which action to use as rewardActionId, and additional information about each action as a result
* of a Rank request.
* @throws IllegalArgumentException if rankOptions is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<PersonalizerRankResult> rank(PersonalizerRankOptions rankRequest) {
return rankWithResponse(rankRequest).flatMap(FluxUtil::toMono);
}
/**
* Request a list of actions to be ranked.
*
* <p>Submit a Personalizer rank request. Receives a context and a list of actions. Returns which of the provided
* actions should be used by your application, in rewardActionId.
*
* @param rankOptions A Personalizer Rank request.
* @return returns which action to use as rewardActionId, and additional information about each action as a result
* of a Rank request along with {@link Response} on successful completion of {@link Mono}.
* @throws IllegalArgumentException if rankOptions is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<PersonalizerRankResult>> rankWithResponse(PersonalizerRankOptions rankOptions) {
try {
return withContext(context -> rankWithResponse(rankOptions, context));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
Mono<Response<PersonalizerRankResult>> rankWithResponse(PersonalizerRankOptions rankOptions, Context context) {
if (rankOptions == null) {
throw logger.logExceptionAsError(new IllegalArgumentException("'rankOptions' is required and cannot be null"));
}
return service.rankWithResponseAsync(rankOptions, context)
.onErrorMap(Transforms::mapToHttpResponseExceptionIfExists)
.map(response -> new SimpleResponse<>(response, response.getValue()));
}
/**
* Send a reward for an event.
*
* <p>Report reward between 0 and 1 that resulted from using the action specified in rewardActionId, for the
* specified event.
*
* @param eventId The event id this reward applies to.
* @param rewardValue The reward should be a floating point number, typically between 0 and 1.
* @return the completion of {@link Mono}.
* @throws IllegalArgumentException if eventId is null or empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Void> reward(String eventId, float rewardValue) {
return rewardWithResponse(eventId, rewardValue).flatMap(FluxUtil::toMono);
}
/**
* Send a reward for an event.
*
* <p>Report reward between 0 and 1 that resulted from using the action specified in rewardActionId, for the
* specified event.
*
* @param eventId The event id this reward applies to.
* @param rewardValue The reward should be a floating point number, typically between 0 and 1.
* @return the {@link Response} on successful completion of {@link Mono}.
* @throws IllegalArgumentException if eventId is null or empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Void>> rewardWithResponse(String eventId, float rewardValue) {
try {
return withContext(context -> rewardWithResponse(eventId, rewardValue, context));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
Mono<Response<Void>> rewardWithResponse(String eventId, float rewardValue, Context context) {
if (CoreUtils.isNullOrEmpty(eventId)) {
throw logger.logExceptionAsError(new IllegalArgumentException("'eventId' is required and cannot be null or empty"));
}
return service.getEvents().rewardWithResponseAsync(eventId, new PersonalizerRewardOptions().setValue(rewardValue), context)
.onErrorMap(Transforms::mapToHttpResponseExceptionIfExists)
.map(response -> new SimpleResponse<>(response, null));
}
/**
* Activate Event.
*
* <p>Report that the specified event was actually used (e.g. by being displayed to the user) and a reward should be
* expected for it.
*
* @param eventId The event ID to be activated.
* @return the completion of {@link Mono}.
* @throws IllegalArgumentException if eventId is null or empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Void> activate(String eventId) {
return activateWithResponse(eventId).flatMap(FluxUtil::toMono);
}
/**
* Activate Event.
*
* <p>Report that the specified event was actually used (e.g. by being displayed to the user) and a reward should be
* expected for it.
*
* @param eventId The event ID to be activated.
* @return the {@link Response} on successful completion of {@link Mono}.
* @throws IllegalArgumentException if eventId is null or empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Void>> activateWithResponse(String eventId) {
try {
return withContext(context -> activateWithResponse(eventId, context));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
Mono<Response<Void>> activateWithResponse(String eventId, Context context) {
if (CoreUtils.isNullOrEmpty(eventId)) {
throw logger.logExceptionAsError(new IllegalArgumentException("'eventId' is required and cannot be null or empty"));
}
return service.getEvents().activateWithResponseAsync(eventId, context)
.onErrorMap(Transforms::mapToHttpResponseExceptionIfExists)
.map(response -> new SimpleResponse<>(response, null));
}
/**
* Post multi-slot Rank.
*
* <p>Submit a Personalizer multi-slot rank request. Receives a context, a list of actions, and a list of slots.
* Returns which of the provided actions should be used in each slot, in each rewardActionId.
*
* @param rankMultiSlotOptions A Personalizer multi-slot Rank request.
* @return the response body on successful completion of {@link Mono}.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<PersonalizerRankMultiSlotResult> rankMultiSlot(PersonalizerRankMultiSlotOptions rankMultiSlotOptions) {
return rankMultiSlotWithResponse(rankMultiSlotOptions).flatMap(FluxUtil::toMono);
}
/**
* Post multi-slot Rank.
*
* <p>Submit a Personalizer multi-slot rank request. Receives a context, a list of actions, and a list of slots.
* Returns which of the provided actions should be used in each slot, in each rewardActionId.
*
* @param rankMultiSlotOptions A Personalizer multi-slot Rank request.
* @return the response body along with {@link Response} on successful completion of {@link Mono}.
* @throws IllegalArgumentException if rankMultiSlotOptions is null.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<PersonalizerRankMultiSlotResult>> rankMultiSlotWithResponse(PersonalizerRankMultiSlotOptions rankMultiSlotOptions) {
try {
return withContext(context -> rankMultiSlotWithResponse(rankMultiSlotOptions, context));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
Mono<Response<PersonalizerRankMultiSlotResult>> rankMultiSlotWithResponse(PersonalizerRankMultiSlotOptions rankMultiSlotOptions, Context context) {
if (rankMultiSlotOptions == null) {
throw logger.logExceptionAsError(new IllegalArgumentException("'rankMultiSlotOptions' is required and cannot be null"));
}
return service.getMultiSlots().rankWithResponseAsync(rankMultiSlotOptions, context)
.onErrorMap(Transforms::mapToHttpResponseExceptionIfExists)
.map(response -> new SimpleResponse<>(response, response.getValue()));
}
/**
* Post multi-slot Rewards.
*
* <p>Report reward that resulted from using the action specified in rewardActionId for the slot.
*
* @param eventId The event id this reward applies to.
* @param rewardMultiSlotOptions List of slot id and reward values. The reward should be a floating point number, typically between 0
* and 1.
* @return the completion of {@link Mono}.
* @throws IllegalArgumentException if rewardMultiSlotOptions is null or eventId is null or empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Void> rewardMultiSlot(String eventId, PersonalizerRewardMultiSlotOptions rewardMultiSlotOptions) {
return rewardMultiSlotWithResponse(eventId, rewardMultiSlotOptions).flatMap(FluxUtil::toMono);
}
/**
* Post multi-slot Rewards.
*
* <p>Report reward that resulted from using the action specified in rewardActionId for the slot.
*
* @param eventId The event id this reward applies to.
* @param rewardMultiSlotOptions List of slot id and reward values. The reward should be a floating point number, typically between 0
* and 1.
* @return the {@link Response} on successful completion of {@link Mono}.
* @throws IllegalArgumentException if rewardMultiSlotOptions is null or eventId is null or empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Void>> rewardMultiSlotWithResponse(String eventId, PersonalizerRewardMultiSlotOptions rewardMultiSlotOptions) {
try {
return withContext(context -> rewardMultiSlotWithResponse(eventId, rewardMultiSlotOptions, context));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
Mono<Response<Void>> rewardMultiSlotWithResponse(String eventId, PersonalizerRewardMultiSlotOptions rewardMultiSlotOptions, Context context) {
if (CoreUtils.isNullOrEmpty(eventId)) {
throw logger.logExceptionAsError(new IllegalArgumentException("'eventId' is required and cannot be null or empty"));
}
if (rewardMultiSlotOptions == null) {
throw logger.logExceptionAsError(new IllegalArgumentException("'rewardMultiSlotOptions' is required and cannot be null"));
}
return service.getMultiSlotEvents().rewardWithResponseAsync(eventId, rewardMultiSlotOptions, context)
.onErrorMap(Transforms::mapToHttpResponseExceptionIfExists)
.map(response -> new SimpleResponse<>(response, null));
}
/**
* Activate multi-slot Event.
*
* <p>Report that the specified event was actually used or displayed to the user and a rewards should be expected
* for it.
*
* @param eventId The event ID this activation applies to.
* @return the completion of {@link Mono}.
* @throws IllegalArgumentException if eventId is null or empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Void> activateMultiSlot(String eventId) {
return activateMultiSlotWithResponse(eventId).flatMap(FluxUtil::toMono);
}
/**
* Activate multi-slot Event.
*
* <p>Report that the specified event was actually used or displayed to the user and a rewards should be expected
* for it.
*
* @param eventId The event ID this activation applies to.
* @return the {@link Response} on successful completion of {@link Mono}.
* @throws IllegalArgumentException if eventId is null or empty.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Void>> activateMultiSlotWithResponse(String eventId) {
try {
return withContext(context -> activateMultiSlotWithResponse(eventId, context));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}
Mono<Response<Void>> activateMultiSlotWithResponse(String eventId, Context context) {
if (CoreUtils.isNullOrEmpty(eventId)) {
throw logger.logExceptionAsError(new IllegalArgumentException("'eventId' is required and cannot be null or empty"));
}
return service.getMultiSlotEvents().activateWithResponseAsync(eventId, context)
.onErrorMap(Transforms::mapToHttpResponseExceptionIfExists)
.map(response -> new SimpleResponse<>(response, null));
}
}