-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStatesRequest.java
More file actions
159 lines (126 loc) · 4.13 KB
/
Copy pathStatesRequest.java
File metadata and controls
159 lines (126 loc) · 4.13 KB
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
/*
* Copyright 2016-2025 Berry Cloud Ltd. All rights reserved.
*/
package dev.learning.xapi.client;
import static dev.learning.xapi.client.XapiClientConstants.ACTIVITIES_STATE_PATH;
import static dev.learning.xapi.client.XapiClientConstants.ACTIVITY_ID_PARAM;
import static dev.learning.xapi.client.XapiClientConstants.ACTIVITY_ID_TEMPLATE;
import static dev.learning.xapi.client.XapiClientConstants.AGENT_PARAM;
import static dev.learning.xapi.client.XapiClientConstants.AGENT_TEMPLATE;
import static dev.learning.xapi.client.XapiClientConstants.REGISTRATION_PARAM;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.learning.xapi.model.Agent;
import java.net.URI;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;
import lombok.Getter;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.experimental.SuperBuilder;
import org.springframework.web.util.UriBuilder;
/**
* Abstract superclass of xAPI state resource request.
*
* @see <a href=
* "https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Communication.md#23-state-resource">State
* Resource</a>
* @author István Rátkai (Selindek)
*/
@SuperBuilder
@Getter
abstract class StatesRequest implements Request {
private static final ObjectMapper objectMapper = new ObjectMapper();
/** The <strong>activityId</strong> query parameter. */
@NonNull private final URI activityId;
/** The <strong>agent</strong> query parameter. */
@NonNull private final Agent agent;
/** The optional <strong>registration</strong> query parameter. */
private final UUID registration;
@Override
public UriBuilder url(UriBuilder uriBuilder, Map<String, Object> queryParams) {
queryParams.put(ACTIVITY_ID_PARAM, activityId);
queryParams.put(AGENT_PARAM, agentToJsonString());
return uriBuilder
.path(ACTIVITIES_STATE_PATH)
.queryParam(ACTIVITY_ID_PARAM, ACTIVITY_ID_TEMPLATE)
.queryParam(AGENT_PARAM, AGENT_TEMPLATE)
.queryParamIfPresent(REGISTRATION_PARAM, Optional.ofNullable(registration));
}
// Exception in write value as string should be impossible.
@SneakyThrows
private String agentToJsonString() {
return objectMapper.writeValueAsString(agent);
}
/** Builder for StatesRequest. */
public abstract static class Builder<C extends StatesRequest, B extends Builder<C, B>> {
/**
* Consumer Builder for agent.
*
* @param agent The Consumer Builder for agent.
* @return This builder
* @see StatesRequest#agent
*/
public B agent(Consumer<Agent.Builder<?, ?>> agent) {
final Agent.Builder<?, ?> builder = Agent.builder();
agent.accept(builder);
return agent(builder.build());
}
/**
* Sets the agent.
*
* @param agent The Agent of the StatesRequest.
* @return This builder
* @see StatesRequest#agent
*/
public B agent(Agent agent) {
this.agent = agent;
return self();
}
/**
* Sets the activityId.
*
* @param activityId The activityId of the StatesRequest.
* @return This builder
* @see StatesRequest#activityId
*/
public B activityId(String activityId) {
this.activityId = URI.create(activityId);
return self();
}
/**
* Sets the activityId.
*
* @param activityId The activityId of the StatesRequest.
* @return This builder
* @see StatesRequest#activityId
*/
public B activityId(URI activityId) {
this.activityId = activityId;
return self();
}
/**
* Sets the registration.
*
* @param registration The registration of the StatesRequest.
* @return This builder
* @see StatesRequest#registration
*/
public B registration(String registration) {
this.registration = UUID.fromString(registration);
return self();
}
/**
* Sets the registration.
*
* @param registration The registration of the StatesRequest.
* @return This builder
* @see StatesRequest#registration
*/
public B registration(UUID registration) {
this.registration = registration;
return self();
}
}
}