Skip to content

Commit a54a555

Browse files
committed
- adds methods to get the native request from an SDK request
1 parent d89345c commit a54a555

File tree

10 files changed

+298
-62
lines changed

10 files changed

+298
-62
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"java.configuration.updateBuildConfiguration": "interactive"
2+
"java.configuration.updateBuildConfiguration": "automatic"
33
}

src/main/java/com/microsoft/graph/http/BaseCollectionRequest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.net.URL;
2626
import java.util.List;
2727

28+
import com.microsoft.graph.concurrency.IProgressCallback;
2829
import com.microsoft.graph.core.ClientException;
2930
import com.microsoft.graph.core.IBaseClient;
3031
import com.microsoft.graph.httpcore.middlewareoption.IShouldRedirect;
@@ -34,6 +35,8 @@
3435
import com.microsoft.graph.options.Option;
3536
import com.microsoft.graph.options.QueryOption;
3637

38+
import okhttp3.Request;
39+
3740
/**
3841
* A request against a collection
3942
*
@@ -297,4 +300,47 @@ public void setDelay(long delay) {
297300
public long getDelay() {
298301
return baseRequest.getDelay();
299302
}
303+
304+
/**
305+
* Sets the HTTP method and returns the current request
306+
*
307+
* @param httpMethod the HTTP method
308+
* @return the current request
309+
*/
310+
public IHttpRequest withHttpMethod(final HttpMethod httpMethod) {
311+
baseRequest.setHttpMethod(httpMethod);
312+
return this;
313+
}
314+
/**
315+
* Returns the Request object to be executed
316+
* @return the Request object to be executed
317+
*/
318+
@Override
319+
public Request getHttpRequest() throws ClientException {
320+
return baseRequest.getHttpRequest();
321+
}
322+
323+
/**
324+
* Returns the Request object to be executed
325+
* @param serializedObject the object to serialize at the body of the request
326+
* @param <requestBodyType> the type of the serialized object
327+
* @return the Request object to be executed
328+
*/
329+
@Override
330+
public <requestBodyType> Request getHttpRequest(final requestBodyType serializedObject) throws ClientException {
331+
return baseRequest.getHttpRequest(serializedObject);
332+
}
333+
334+
/**
335+
* Returns the Request object to be executed
336+
* @param serializedObject the object to serialize at the body of the request
337+
* @param progress the progress callback
338+
* @param <requestBodyType> the type of the serialized object
339+
* @param <responseType> the type of the response object
340+
* @return the Request object to be executed
341+
*/
342+
@Override
343+
public <requestBodyType, responseType> Request getHttpRequest(final requestBodyType serializedObject, final IProgressCallback<responseType> progress) throws ClientException {
344+
return baseRequest.getHttpRequest(serializedObject, progress);
345+
}
300346
}

src/main/java/com/microsoft/graph/http/BaseRequest.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import com.microsoft.graph.concurrency.ICallback;
3030
import com.microsoft.graph.concurrency.IProgressCallback;
31+
import com.microsoft.graph.content.MSBatchRequestContent;
32+
import com.microsoft.graph.content.MSBatchRequestStep;
3133
import com.microsoft.graph.core.IBaseClient;
3234
import com.microsoft.graph.httpcore.middlewareoption.IShouldRedirect;
3335
import com.microsoft.graph.httpcore.middlewareoption.IShouldRetry;
@@ -43,9 +45,11 @@
4345
import java.net.MalformedURLException;
4446
import java.net.URL;
4547
import java.util.ArrayList;
48+
import java.util.Arrays;
4649
import java.util.Collections;
4750
import java.util.LinkedList;
4851
import java.util.List;
52+
import java.util.concurrent.ThreadLocalRandom;
4953

5054
/**
5155
* An HTTP request
@@ -191,36 +195,39 @@ public URL getRequestUrl() {
191195
}
192196
return null;
193197
}
198+
194199
/**
195200
* Returns the Request object to be executed
196201
* @return the Request object to be executed
197202
*/
198-
public Request GetHttpRequest() throws ClientException {
199-
return GetHttpRequest(null);
203+
@Override
204+
public Request getHttpRequest() throws ClientException {
205+
return getHttpRequest(null);
200206
}
201207

202208
/**
203209
* Returns the Request object to be executed
204210
* @param serializedObject the object to serialize at the body of the request
205-
* @param <T1> the type of the serialized object
211+
* @param <requestBodyType> the type of the serialized object
206212
* @return the Request object to be executed
207213
*/
208-
public <T1> Request GetHttpRequest(final T1 serializedObject) throws ClientException {
209-
return GetHttpRequest(serializedObject, null);
214+
@Override
215+
public <requestBodyType> Request getHttpRequest(final requestBodyType serializedObject) throws ClientException {
216+
return getHttpRequest(serializedObject, null);
210217
}
211218

212219
/**
213220
* Returns the Request object to be executed
214221
* @param serializedObject the object to serialize at the body of the request
215222
* @param progress the progress callback
216-
* @param <T1> the type of the serialized object
217-
* @param <T2> the type of the response object
223+
* @param <requestBodyType> the type of the serialized object
224+
* @param <responseType> the type of the response object
218225
* @return the Request object to be executed
219226
*/
220227
@SuppressWarnings("unchecked")
221-
public <T1, T2> Request GetHttpRequest(final T1 serializedObject, final IProgressCallback<T2> progress) throws ClientException {
222-
final IHttpProvider provider = this.getClient().getHttpProvider();
223-
return provider.getHttpRequest(this, (Class<T2>) responseClass, serializedObject, progress);
228+
@Override
229+
public <requestBodyType, responseType> Request getHttpRequest(final requestBodyType serializedObject, final IProgressCallback<responseType> progress) throws ClientException {
230+
return client.getHttpProvider().getHttpRequest(this, (Class<responseType>) responseClass, serializedObject, progress);
224231
}
225232

226233
private String addFunctionParameters() {
@@ -396,6 +403,17 @@ public void setHttpMethod(final HttpMethod httpMethod) {
396403
method = httpMethod;
397404
}
398405

406+
/**
407+
* Sets the HTTP method and returns the current request
408+
*
409+
* @param httpMethod the HTTP method
410+
* @return the current request
411+
*/
412+
public IHttpRequest withHttpMethod(final HttpMethod httpMethod) {
413+
method = httpMethod;
414+
return this;
415+
}
416+
399417
/**
400418
* Gets the client
401419
*

src/main/java/com/microsoft/graph/http/BaseStreamRequest.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package com.microsoft.graph.http;
2424

2525
import com.microsoft.graph.concurrency.ICallback;
26+
import com.microsoft.graph.concurrency.IProgressCallback;
2627
import com.microsoft.graph.core.ClientException;
2728
import com.microsoft.graph.core.IBaseClient;
2829
import com.microsoft.graph.httpcore.middlewareoption.IShouldRedirect;
@@ -32,6 +33,8 @@
3233
import com.microsoft.graph.options.HeaderOption;
3334
import com.microsoft.graph.options.Option;
3435

36+
import okhttp3.Request;
37+
3538
import java.io.InputStream;
3639
import java.net.URL;
3740
import java.util.List;
@@ -269,5 +272,48 @@ public void setDelay(long delay) {
269272
public long getDelay() {
270273
return baseRequest.getDelay();
271274
}
272-
275+
/**
276+
* Sets the HTTP method and returns the current request
277+
*
278+
* @param httpMethod the HTTP method
279+
* @return the current request
280+
*/
281+
@Override
282+
public IHttpRequest withHttpMethod(final HttpMethod httpMethod) {
283+
baseRequest.setHttpMethod(httpMethod);
284+
return this;
285+
}
286+
287+
/**
288+
* Returns the Request object to be executed
289+
* @return the Request object to be executed
290+
*/
291+
@Override
292+
public Request getHttpRequest() throws ClientException {
293+
return baseRequest.getHttpRequest();
294+
}
295+
296+
/**
297+
* Returns the Request object to be executed
298+
* @param serializedObject the object to serialize at the body of the request
299+
* @param <requestBodyType> the type of the serialized object
300+
* @return the Request object to be executed
301+
*/
302+
@Override
303+
public <requestBodyType> Request getHttpRequest(final requestBodyType serializedObject) throws ClientException {
304+
return baseRequest.getHttpRequest(serializedObject);
305+
}
306+
307+
/**
308+
* Returns the Request object to be executed
309+
* @param serializedObject the object to serialize at the body of the request
310+
* @param progress the progress callback
311+
* @param <requestBodyType> the type of the serialized object
312+
* @param <responseType> the type of the response object
313+
* @return the Request object to be executed
314+
*/
315+
@Override
316+
public <requestBodyType, responseType> Request getHttpRequest(final requestBodyType serializedObject, final IProgressCallback<responseType> progress) throws ClientException {
317+
return baseRequest.getHttpRequest(serializedObject, progress);
318+
}
273319
}

src/main/java/com/microsoft/graph/http/CoreHttpProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,6 @@ public <Result, Body> Request getHttpRequest(final IHttpRequest request,
220220
final Body serializable,
221221
final IProgressCallback<Result> progress) throws ClientException {
222222
final int defaultBufferSize = 4096;
223-
if (authenticationProvider != null) {
224-
authenticationProvider.authenticateRequest(request);
225-
}
226223

227224
final URL requestUrl = request.getRequestUrl();
228225
logger.logDebug("Starting to send request, URL " + requestUrl.toString());
@@ -370,6 +367,9 @@ public Request authenticateRequest(Request request) {
370367
okBuilder.retryOnConnectionFailure(false);
371368
this.corehttpClient = okBuilder.build();
372369
}
370+
if (authenticationProvider != null) {
371+
authenticationProvider.authenticateRequest(request);
372+
}
373373
Request coreHttpRequest = getHttpRequest(request, resultClass, serializable, progress);
374374
Response response = corehttpClient.newCall(coreHttpRequest).execute();
375375
InputStream in = null;

src/main/java/com/microsoft/graph/http/IHttpRequest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222

2323
package com.microsoft.graph.http;
2424

25+
import com.microsoft.graph.concurrency.IProgressCallback;
26+
import com.microsoft.graph.core.ClientException;
2527
import com.microsoft.graph.httpcore.middlewareoption.IShouldRedirect;
2628
import com.microsoft.graph.httpcore.middlewareoption.IShouldRetry;
2729
import com.microsoft.graph.options.HeaderOption;
2830
import com.microsoft.graph.options.Option;
2931

32+
import okhttp3.Request;
33+
3034
import java.net.URL;
3135
import java.util.List;
3236

@@ -154,5 +158,37 @@ public interface IHttpRequest {
154158
* @return Delay between retries in seconds
155159
*/
156160
long getDelay();
161+
162+
/**
163+
* Sets the HTTP method and returns the current request
164+
*
165+
* @param httpMethod the HTTP method
166+
* @return the current request
167+
*/
168+
IHttpRequest withHttpMethod(final HttpMethod httpMethod);
169+
170+
/**
171+
* Returns the Request object to be executed
172+
* @return the Request object to be executed
173+
*/
174+
Request getHttpRequest() throws ClientException;
175+
176+
/**
177+
* Returns the Request object to be executed
178+
* @param serializedObject the object to serialize at the body of the request
179+
* @param <requestBodyType> the type of the serialized object
180+
* @return the Request object to be executed
181+
*/
182+
<requestBodyType> Request getHttpRequest(final requestBodyType serializedObject) throws ClientException;
183+
184+
/**
185+
* Returns the Request object to be executed
186+
* @param serializedObject the object to serialize at the body of the request
187+
* @param progress the progress callback
188+
* @param <requestBodyType> the type of the serialized object
189+
* @param <responseType> the type of the response object
190+
* @return the Request object to be executed
191+
*/
192+
<requestBodyType, responseType> Request getHttpRequest(final requestBodyType serializedObject, final IProgressCallback<responseType> progress) throws ClientException;
157193
}
158194

src/test/java/com/microsoft/graph/functional/TestBase.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,7 @@ private void GetAuthenticatedClient()
4545
if (graphClient == null) {
4646
try {
4747
accessToken = GetAccessToken().replace("\"", "");
48-
IAuthenticationProvider mAuthenticationProvider = new IAuthenticationProvider() {
49-
@Override
50-
public void authenticateRequest(final IHttpRequest request) {
51-
request.addHeader("Authorization",
52-
"Bearer " + accessToken);
53-
}
54-
};
55-
IClientConfig mClientConfig = DefaultClientConfig.createWithAuthenticationProvider(mAuthenticationProvider);
48+
IClientConfig mClientConfig = DefaultClientConfig.createWithAuthenticationProvider(new TestBaseAuthenticationProvider(accessToken));
5649

5750
graphClient = GraphServiceClient.fromConfig(mClientConfig);
5851
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.microsoft.graph.functional;
2+
3+
import com.microsoft.graph.authentication.IAuthenticationProvider;
4+
import com.microsoft.graph.http.IHttpRequest;
5+
import com.microsoft.graph.httpcore.ICoreAuthenticationProvider;
6+
7+
import okhttp3.Request;
8+
9+
public class TestBaseAuthenticationProvider implements IAuthenticationProvider, ICoreAuthenticationProvider {
10+
private String _accessToken;
11+
public TestBaseAuthenticationProvider(String accessToken) {
12+
_accessToken = accessToken;
13+
}
14+
@Override
15+
public Request authenticateRequest(Request request) {
16+
return request.newBuilder().addHeader("Authorization", "Bearer " + _accessToken).build();
17+
}
18+
19+
@Override
20+
public void authenticateRequest(IHttpRequest request) {
21+
request.addHeader("Authorization",
22+
"Bearer " + _accessToken);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)