Skip to content

Commit 1839d6b

Browse files
committed
refactor ParameterizableRequest, now reveals the ParameterBuilder for direct access usage
1 parent b4938ad commit 1839d6b

9 files changed

Lines changed: 209 additions & 203 deletions

File tree

lib/src/main/java/com/auth0/authentication/AuthenticationAPIClient.java

Lines changed: 105 additions & 110 deletions
Large diffs are not rendered by default.

lib/src/main/java/com/auth0/authentication/AuthenticationRequest.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*/
4040
public class AuthenticationRequest implements Request<Authentication> {
4141

42+
private static final String ID_TOKEN_KEY = "id_token";
43+
4244
private final ParameterizableRequest<Token> credentialsRequest;
4345
private final ParameterizableRequest<UserProfile> tokenInfoRequest;
4446

@@ -49,49 +51,51 @@ public class AuthenticationRequest implements Request<Authentication> {
4951

5052
/**
5153
* Adds additional parameters for the login request
54+
*
5255
* @param parameters as a non-null dictionary
5356
* @return itself
5457
*/
5558
public AuthenticationRequest addParameters(Map<String, Object> parameters) {
56-
credentialsRequest.addParameters(parameters);
59+
credentialsRequest.getParameterBuilder().addAll(parameters);
5760
return this;
5861
}
5962

6063
/**
6164
* Set the scope used to authenticate the user
65+
*
6266
* @param scope value
6367
* @return itself
6468
*/
6569
public AuthenticationRequest setScope(String scope) {
66-
credentialsRequest.addParameters(new ParameterBuilder().clearAll().setScope(scope).asDictionary());
70+
credentialsRequest.getParameterBuilder().setScope(scope);
6771
return this;
6872
}
6973

7074
/**
7175
* Set the connection used to authenticate
76+
*
7277
* @param connection name
7378
* @return itself
7479
*/
7580
public AuthenticationRequest setConnection(String connection) {
76-
credentialsRequest.addParameters(new ParameterBuilder().clearAll().setConnection(connection).asDictionary());
81+
credentialsRequest.getParameterBuilder().setConnection(connection);
7782
return this;
7883
}
7984

8085
/**
8186
* Starts the log in request and then fetches the user's profile
87+
*
8288
* @param callback called on either success or failure
8389
*/
8490
@Override
8591
public void start(final BaseCallback<Authentication> callback) {
8692
credentialsRequest.start(new BaseCallback<Token>() {
8793
@Override
8894
public void onSuccess(final Token token) {
89-
Map<String, Object> parameters = new ParameterBuilder()
90-
.clearAll()
91-
.set("id_token", token.getIdToken())
92-
.asDictionary();
9395
tokenInfoRequest
94-
.addParameters(parameters)
96+
.getParameterBuilder()
97+
.set(ID_TOKEN_KEY, token.getIdToken());
98+
tokenInfoRequest
9599
.start(new BaseCallback<UserProfile>() {
96100
@Override
97101
public void onSuccess(UserProfile profile) {
@@ -114,18 +118,15 @@ public void onFailure(Auth0Exception error) {
114118

115119
/**
116120
* Logs in the user with Auth0 and fetches it's profile.
121+
*
117122
* @return authentication object containing the user's tokens and profile
118123
* @throws Auth0Exception when either authentication or profile fetch fails
119124
*/
120125
@Override
121126
public Authentication execute() throws Auth0Exception {
122127
Token token = credentialsRequest.execute();
123-
Map<String, Object> parameters = new ParameterBuilder()
124-
.clearAll()
125-
.set("id_token", token.getIdToken())
126-
.asDictionary();
128+
tokenInfoRequest.getParameterBuilder().set(ID_TOKEN_KEY, token.getIdToken());
127129
UserProfile profile = tokenInfoRequest
128-
.addParameters(parameters)
129130
.execute();
130131
return new Authentication(profile, token);
131132
}

lib/src/main/java/com/auth0/authentication/ChangePasswordRequest.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,34 @@ public class ChangePasswordRequest implements ParameterizableRequest<Void> {
4242

4343
ChangePasswordRequest(ParameterizableRequest<Void> request) {
4444
this.request = request;
45-
}
46-
47-
private ChangePasswordRequest addParameter(String key, Object value) {
48-
Map<String, Object> parameters = new HashMap<>();
49-
parameters.put(key, value);
50-
return addParameters(parameters);
45+
request.getParameterBuilder();
5146
}
5247

5348
/**
5449
* Set the 'password' parameter to be sent in the request
50+
*
5551
* @param newPassword the new password
5652
* @return itself
5753
*/
5854
public ChangePasswordRequest setNewPassword(String newPassword) {
59-
return addParameter(PASSWORD_KEY, newPassword);
55+
request.getParameterBuilder().set(PASSWORD_KEY, newPassword);
56+
return this;
6057
}
6158

6259
/**
63-
* Adds additional parameters to be sent in the request
64-
* @param parameters as a non-null dictionary
65-
* @return itself
60+
* Getter for this request ParameterBuilder.
61+
*
62+
* @return the associated ParameterBuilder instance
6663
*/
6764
@Override
68-
public ChangePasswordRequest addParameters(Map<String, Object> parameters) {
69-
request.addParameters(parameters);
70-
return this;
65+
public ParameterBuilder getParameterBuilder() {
66+
return request.getParameterBuilder();
7167
}
7268

7369
/**
7470
* Adds an additional header for the request
75-
* @param name of the header
71+
*
72+
* @param name of the header
7673
* @param value of the header
7774
* @return itself
7875
*/
@@ -84,6 +81,7 @@ public ChangePasswordRequest addHeader(String name, String value) {
8481

8582
/**
8683
* Starts the change password request
84+
*
8785
* @param callback called either on success or failure
8886
*/
8987
@Override
@@ -93,6 +91,7 @@ public void start(final BaseCallback<Void> callback) {
9391

9492
/**
9593
* Executes the change password request
94+
*
9695
* @return nothing on success
9796
* @throws Auth0Exception when the change password request fails
9897
*/

lib/src/main/java/com/auth0/authentication/DelegationRequest.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@
2525
package com.auth0.authentication;
2626

2727
import com.auth0.Auth0Exception;
28+
import com.auth0.authentication.result.Delegation;
29+
import com.auth0.callback.BaseCallback;
2830
import com.auth0.request.ParameterizableRequest;
2931
import com.auth0.request.Request;
30-
import com.auth0.callback.BaseCallback;
31-
import com.auth0.authentication.result.Delegation;
3232

33-
import java.util.HashMap;
3433
import java.util.Map;
3534

3635
/**
3736
* Represents a delegation request for Auth0 tokens that will yield a new delegation token.
3837
* The delegation response depends on the 'api_type' parameter.
38+
*
3939
* @param <T> type of object that will hold the delegation response. When requesting Auth0’s 'id_token' you can
40-
* use {@link Delegation}, otherwise you’ll need to provide an object that can be created from the JSON
41-
* payload or just use {@code Map<String, Object>}
40+
* use {@link Delegation}, otherwise you’ll need to provide an object that can be created from the JSON
41+
* payload or just use {@code Map<String, Object>}
4242
*/
4343
public class DelegationRequest<T> implements Request<T> {
4444

@@ -54,23 +54,24 @@ public class DelegationRequest<T> implements Request<T> {
5454
}
5555

5656
private DelegationRequest<T> addParameter(String key, Object value) {
57-
Map<String, Object> parameters = new HashMap<>();
58-
parameters.put(key, value);
59-
return addParameters(parameters);
57+
request.getParameterBuilder().set(key, value);
58+
return this;
6059
}
6160

6261
/**
6362
* Add additional parameters to be sent in the request
63+
*
6464
* @param parameters as a non-null dictionary
6565
* @return itself
6666
*/
6767
public DelegationRequest<T> addParameters(Map<String, Object> parameters) {
68-
request.addParameters(parameters);
68+
request.getParameterBuilder().addAll(parameters);
6969
return this;
7070
}
7171

7272
/**
7373
* Set the 'api_type' parameter to be sent in the request
74+
*
7475
* @param apiType the delegation api type
7576
* @return itself
7677
*/
@@ -80,15 +81,18 @@ public DelegationRequest<T> setApiType(String apiType) {
8081

8182
/**
8283
* Set the 'scope' used to make the delegation
84+
*
8385
* @param scope value
8486
* @return itself
8587
*/
8688
public DelegationRequest<T> setScope(String scope) {
87-
return addParameters(new ParameterBuilder().clearAll().setScope(scope).asDictionary());
89+
request.getParameterBuilder().setScope(scope);
90+
return this;
8891
}
8992

9093
/**
9194
* Set the 'target' parameter to be sent in the request
95+
*
9296
* @param target the delegation target
9397
* @return itself
9498
*/
@@ -98,6 +102,7 @@ public DelegationRequest<T> setTarget(String target) {
98102

99103
/**
100104
* Starts the delegation request against Auth0 API
105+
*
101106
* @param callback called either on success or failure
102107
*/
103108
@Override
@@ -107,6 +112,7 @@ public void start(final BaseCallback<T> callback) {
107112

108113
/**
109114
* Executes the delegation request against Auth0 API
115+
*
110116
* @return the delegation response on success
111117
* @throws Auth0Exception when the delegation request fails
112118
*/

lib/src/main/java/com/auth0/authentication/ParameterBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public ParameterBuilder addAll(Map<String, Object> parameters) {
177177
*
178178
* @return itself
179179
*/
180-
public ParameterBuilder clearAll() {
180+
public ParameterBuilder clearAllA() {
181181
parameters.clear();
182182
return this;
183183
}

lib/src/main/java/com/auth0/authentication/SignUpRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class SignUpRequest implements Request<Authentication> {
5252
* @return itself
5353
*/
5454
public SignUpRequest addSignUpParameters(Map<String, Object> parameters) {
55-
signUpRequest.addParameters(parameters);
55+
signUpRequest.getParameterBuilder().addAll(parameters);
5656
return this;
5757
}
5858

lib/src/main/java/com/auth0/internal/BaseRequest.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.auth0.APIException;
2828
import com.auth0.Auth0Exception;
2929
import com.auth0.RequestBodyBuildException;
30+
import com.auth0.authentication.ParameterBuilder;
3031
import com.auth0.callback.BaseCallback;
3132
import com.auth0.request.AuthorizableRequest;
3233
import com.auth0.request.ParameterizableRequest;
@@ -47,12 +48,12 @@
4748
abstract class BaseRequest<T> implements ParameterizableRequest<T>, AuthorizableRequest<T>, Callback {
4849

4950
private final Map<String, String> headers;
50-
private final Map<String, Object> parameters;
5151
protected final HttpUrl url;
5252
protected final OkHttpClient client;
5353
private final ObjectReader reader;
5454
private final ObjectReader errorReader;
5555
private final ObjectWriter writer;
56+
private final ParameterBuilder builder;
5657

5758
private BaseCallback<T> callback;
5859

@@ -68,7 +69,7 @@ public BaseRequest(HttpUrl url, OkHttpClient client, ObjectReader reader, Object
6869
this.writer = writer;
6970
this.callback = callback;
7071
this.headers = new HashMap<>();
71-
this.parameters = new HashMap<>();
72+
this.builder = ParameterBuilder.newEmptyBuilder();
7273
}
7374

7475
protected void setCallback(BaseCallback<T> callback) {
@@ -92,16 +93,12 @@ protected Request.Builder newBuilder() {
9293
return builder;
9394
}
9495

95-
protected Map<String, Object> getParameters() {
96-
return parameters;
97-
}
98-
9996
protected ObjectReader getReader() {
10097
return reader;
10198
}
10299

103100
protected RequestBody buildBody() throws RequestBodyBuildException {
104-
return JsonRequestBodyBuilder.createBody(parameters, writer);
101+
return JsonRequestBodyBuilder.createBody(builder.asDictionary(), writer);
105102
}
106103

107104
protected APIException parseUnsuccessfulResponse(Response response) {
@@ -140,11 +137,8 @@ public AuthorizableRequest<T> setBearer(String jwt) {
140137
}
141138

142139
@Override
143-
public ParameterizableRequest<T> addParameters(Map<String, Object> parameters) {
144-
if (parameters != null) {
145-
this.parameters.putAll(parameters);
146-
}
147-
return this;
140+
public ParameterBuilder getParameterBuilder() {
141+
return builder;
148142
}
149143

150144
@Override

lib/src/main/java/com/auth0/request/ParameterizableRequest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,26 @@
2424

2525
package com.auth0.request;
2626

27-
import java.util.Map;
27+
import com.auth0.authentication.ParameterBuilder;
2828

2929
/**
3030
* Defines a request that can be configured (payload and headers)
31+
*
3132
* @param <T>
3233
*/
3334
public interface ParameterizableRequest<T> extends Request<T> {
3435

3536
/**
36-
* Adds additional parameters to send in the request
37-
* @param parameters as a non-null dictionary
38-
* @return itself
37+
* Getter for this request ParameterBuilder.
38+
*
39+
* @return the associated ParameterBuilder instance
3940
*/
40-
ParameterizableRequest<T> addParameters(Map<String, Object> parameters);
41+
ParameterBuilder getParameterBuilder();
4142

4243
/**
4344
* Adds an additional header for the request
44-
* @param name of the header
45+
*
46+
* @param name of the header
4547
* @param value of the header
4648
* @return itself
4749
*/

0 commit comments

Comments
 (0)