Skip to content

Commit 2f71df3

Browse files
committed
- adds getRequest Method for people to be able to extract the request to be executed
1 parent 379197d commit 2f71df3

File tree

6 files changed

+213
-117
lines changed

6 files changed

+213
-117
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
package com.microsoft.graph.http;
2424

2525
import okhttp3.HttpUrl;
26+
import okhttp3.Request;
2627
import okhttp3.HttpUrl.Builder;
2728

2829
import com.microsoft.graph.concurrency.ICallback;
30+
import com.microsoft.graph.concurrency.IProgressCallback;
2931
import com.microsoft.graph.core.IBaseClient;
3032
import com.microsoft.graph.httpcore.middlewareoption.IShouldRedirect;
3133
import com.microsoft.graph.httpcore.middlewareoption.IShouldRetry;
@@ -189,6 +191,37 @@ public URL getRequestUrl() {
189191
}
190192
return null;
191193
}
194+
/**
195+
* Returns the Request object to be executed
196+
* @return the Request object to be executed
197+
*/
198+
public Request GetHttpRequest() throws ClientException {
199+
return GetHttpRequest(null);
200+
}
201+
202+
/**
203+
* Returns the Request object to be executed
204+
* @param serializedObject the object to serialize at the body of the request
205+
* @param <T1> the type of the serialized object
206+
* @return the Request object to be executed
207+
*/
208+
public <T1> Request GetHttpRequest(final T1 serializedObject) throws ClientException {
209+
return GetHttpRequest(serializedObject, null);
210+
}
211+
212+
/**
213+
* Returns the Request object to be executed
214+
* @param serializedObject the object to serialize at the body of the request
215+
* @param progress the progress callback
216+
* @param <T1> the type of the serialized object
217+
* @param <T2> the type of the response object
218+
* @return the Request object to be executed
219+
*/
220+
@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);
224+
}
192225

193226
private String addFunctionParameters() {
194227
final StringBuilder requestUrl = new StringBuilder(this.requestUrl);

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

Lines changed: 135 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,137 @@ public <Result, Body, DeserializeType> Result send(final IHttpRequest request,
203203
final IStatefulResponseHandler<Result, DeserializeType> handler) throws ClientException {
204204
return sendRequestInternal(request, resultClass, serializable, null, handler);
205205
}
206+
/**
207+
* Sends the HTTP request
208+
*
209+
* @param request the request description
210+
* @param resultClass the class of the response from the service
211+
* @param serializable the object to send to the service in the body of the request
212+
* @param progress the progress callback for the request
213+
* @param <Result> the type of the response object
214+
* @param <Body> the type of the object to send to the service in the body of the request
215+
* @return the result from the request
216+
* @throws ClientException an exception occurs if the request was unable to complete for any reason
217+
*/
218+
public <Result, Body> Request getHttpRequest(final IHttpRequest request,
219+
final Class<Result> resultClass,
220+
final Body serializable,
221+
final IProgressCallback<Result> progress) throws ClientException {
222+
final int defaultBufferSize = 4096;
223+
if (authenticationProvider != null) {
224+
authenticationProvider.authenticateRequest(request);
225+
}
226+
227+
final URL requestUrl = request.getRequestUrl();
228+
logger.logDebug("Starting to send request, URL " + requestUrl.toString());
229+
230+
if(this.connectionConfig == null) {
231+
this.connectionConfig = new DefaultConnectionConfig();
232+
}
233+
234+
// Request level middleware options
235+
RedirectOptions redirectOptions = new RedirectOptions(request.getMaxRedirects() > 0? request.getMaxRedirects() : this.connectionConfig.getMaxRedirects(),
236+
request.getShouldRedirect() != null? request.getShouldRedirect() : this.connectionConfig.getShouldRedirect());
237+
RetryOptions retryOptions = new RetryOptions(request.getShouldRetry() != null? request.getShouldRetry() : this.connectionConfig.getShouldRetry(),
238+
request.getMaxRetries() > 0? request.getMaxRetries() : this.connectionConfig.getMaxRetries(),
239+
request.getDelay() > 0? request.getDelay() : this.connectionConfig.getDelay());
240+
241+
Request coreHttpRequest = convertIHttpRequestToOkHttpRequest(request);
242+
Request.Builder corehttpRequestBuilder = coreHttpRequest
243+
.newBuilder()
244+
.tag(RedirectOptions.class, redirectOptions)
245+
.tag(RetryOptions.class, retryOptions);
246+
247+
String contenttype = null;
248+
249+
logger.logDebug("Request Method " + request.getHttpMethod().toString());
250+
List<HeaderOption> requestHeaders = request.getHeaders();
251+
252+
for(HeaderOption headerOption : requestHeaders) {
253+
if(headerOption.getName().equalsIgnoreCase(Constants.CONTENT_TYPE_HEADER_NAME)) {
254+
contenttype = headerOption.getValue().toString();
255+
break;
256+
}
257+
}
258+
259+
final byte[] bytesToWrite;
260+
corehttpRequestBuilder.addHeader("Accept", "*/*");
261+
if (serializable == null) {
262+
// Send an empty body through with a POST request
263+
// This ensures that the Content-Length header is properly set
264+
if (request.getHttpMethod() == HttpMethod.POST) {
265+
bytesToWrite = new byte[0];
266+
contenttype = Constants.BINARY_CONTENT_TYPE;
267+
}
268+
else {
269+
bytesToWrite = null;
270+
}
271+
} else if (serializable instanceof byte[]) {
272+
logger.logDebug("Sending byte[] as request body");
273+
bytesToWrite = (byte[]) serializable;
274+
275+
// If the user hasn't specified a Content-Type for the request
276+
if (!hasHeader(requestHeaders, Constants.CONTENT_TYPE_HEADER_NAME)) {
277+
corehttpRequestBuilder.addHeader(Constants.CONTENT_TYPE_HEADER_NAME, Constants.BINARY_CONTENT_TYPE);
278+
contenttype = Constants.BINARY_CONTENT_TYPE;
279+
}
280+
} else {
281+
logger.logDebug("Sending " + serializable.getClass().getName() + " as request body");
282+
final String serializeObject = serializer.serializeObject(serializable);
283+
try {
284+
bytesToWrite = serializeObject.getBytes(Constants.JSON_ENCODING);
285+
} catch (final UnsupportedEncodingException ex) {
286+
final ClientException clientException = new ClientException("Unsupported encoding problem: ",
287+
ex);
288+
logger.logError("Unsupported encoding problem: " + ex.getMessage(), ex);
289+
throw clientException;
290+
}
291+
292+
// If the user hasn't specified a Content-Type for the request
293+
if (!hasHeader(requestHeaders, Constants.CONTENT_TYPE_HEADER_NAME)) {
294+
corehttpRequestBuilder.addHeader(Constants.CONTENT_TYPE_HEADER_NAME, Constants.JSON_CONTENT_TYPE);
295+
contenttype = Constants.JSON_CONTENT_TYPE;
296+
}
297+
}
298+
299+
RequestBody requestBody = null;
300+
// Handle cases where we've got a body to process.
301+
if (bytesToWrite != null) {
302+
final String mediaContentType = contenttype;
303+
requestBody = new RequestBody() {
304+
@Override
305+
public long contentLength() throws IOException {
306+
return bytesToWrite.length;
307+
}
308+
@Override
309+
public void writeTo(BufferedSink sink) throws IOException {
310+
OutputStream out = sink.outputStream();
311+
int writtenSoFar = 0;
312+
BufferedOutputStream bos = new BufferedOutputStream(out);
313+
int toWrite;
314+
do {
315+
toWrite = Math.min(defaultBufferSize, bytesToWrite.length - writtenSoFar);
316+
bos.write(bytesToWrite, writtenSoFar, toWrite);
317+
writtenSoFar = writtenSoFar + toWrite;
318+
if (progress != null) {
319+
executors.performOnForeground(writtenSoFar, bytesToWrite.length,
320+
progress);
321+
}
322+
} while (toWrite > 0);
323+
bos.close();
324+
out.close();
325+
}
326+
327+
@Override
328+
public MediaType contentType() {
329+
return MediaType.parse(mediaContentType);
330+
}
331+
};
332+
}
206333

334+
corehttpRequestBuilder.method(request.getHttpMethod().toString(), requestBody);
335+
return corehttpRequestBuilder.build();
336+
}
207337
/**
208338
* Sends the HTTP request
209339
*
@@ -225,22 +355,8 @@ private <Result, Body, DeserializeType> Result sendRequestInternal(final IHttpRe
225355
final IProgressCallback<Result> progress,
226356
final IStatefulResponseHandler<Result, DeserializeType> handler)
227357
throws ClientException {
228-
final int defaultBufferSize = 4096;
229358

230359
try {
231-
if (authenticationProvider != null) {
232-
authenticationProvider.authenticateRequest(request);
233-
}
234-
235-
InputStream in = null;
236-
boolean isBinaryStreamInput = false;
237-
final URL requestUrl = request.getRequestUrl();
238-
logger.logDebug("Starting to send request, URL " + requestUrl.toString());
239-
240-
if(this.connectionConfig == null) {
241-
this.connectionConfig = new DefaultConnectionConfig();
242-
}
243-
244360
if(this.corehttpClient == null) {
245361
OkHttpClient.Builder okBuilder = HttpClients.createDefault(new ICoreAuthenticationProvider() {
246362
@Override
@@ -254,107 +370,14 @@ public Request authenticateRequest(Request request) {
254370
okBuilder.retryOnConnectionFailure(false);
255371
this.corehttpClient = okBuilder.build();
256372
}
257-
258-
// Request level middleware options
259-
RedirectOptions redirectOptions = new RedirectOptions(request.getMaxRedirects() > 0? request.getMaxRedirects() : this.connectionConfig.getMaxRedirects(),
260-
request.getShouldRedirect() != null? request.getShouldRedirect() : this.connectionConfig.getShouldRedirect());
261-
RetryOptions retryOptions = new RetryOptions(request.getShouldRetry() != null? request.getShouldRetry() : this.connectionConfig.getShouldRetry(),
262-
request.getMaxRetries() > 0? request.getMaxRetries() : this.connectionConfig.getMaxRetries(),
263-
request.getDelay() > 0? request.getDelay() : this.connectionConfig.getDelay());
264-
265-
Request coreHttpRequest = convertIHttpRequestToOkHttpRequest(request);
266-
Request.Builder corehttpRequestBuilder = coreHttpRequest
267-
.newBuilder()
268-
.tag(RedirectOptions.class, redirectOptions)
269-
.tag(RetryOptions.class, retryOptions);
270-
271-
String contenttype = null;
272-
Response response = null;
273-
373+
Request coreHttpRequest = getHttpRequest(request, resultClass, serializable, progress);
374+
Response response = corehttpClient.newCall(coreHttpRequest).execute();
375+
InputStream in = null;
376+
boolean isBinaryStreamInput = false;
274377
try {
275-
logger.logDebug("Request Method " + request.getHttpMethod().toString());
276-
List<HeaderOption> requestHeaders = request.getHeaders();
277-
278-
for(HeaderOption headerOption : requestHeaders) {
279-
if(headerOption.getName().equalsIgnoreCase(Constants.CONTENT_TYPE_HEADER_NAME)) {
280-
contenttype = headerOption.getValue().toString();
281-
break;
282-
}
283-
}
284-
285-
final byte[] bytesToWrite;
286-
corehttpRequestBuilder.addHeader("Accept", "*/*");
287-
if (serializable == null) {
288-
// Send an empty body through with a POST request
289-
// This ensures that the Content-Length header is properly set
290-
if (request.getHttpMethod() == HttpMethod.POST) {
291-
bytesToWrite = new byte[0];
292-
contenttype = Constants.BINARY_CONTENT_TYPE;
293-
}
294-
else {
295-
bytesToWrite = null;
296-
}
297-
} else if (serializable instanceof byte[]) {
298-
logger.logDebug("Sending byte[] as request body");
299-
bytesToWrite = (byte[]) serializable;
300-
301-
// If the user hasn't specified a Content-Type for the request
302-
if (!hasHeader(requestHeaders, Constants.CONTENT_TYPE_HEADER_NAME)) {
303-
corehttpRequestBuilder.addHeader(Constants.CONTENT_TYPE_HEADER_NAME, Constants.BINARY_CONTENT_TYPE);
304-
contenttype = Constants.BINARY_CONTENT_TYPE;
305-
}
306-
} else {
307-
logger.logDebug("Sending " + serializable.getClass().getName() + " as request body");
308-
final String serializeObject = serializer.serializeObject(serializable);
309-
bytesToWrite = serializeObject.getBytes(Constants.JSON_ENCODING);
310-
311-
// If the user hasn't specified a Content-Type for the request
312-
if (!hasHeader(requestHeaders, Constants.CONTENT_TYPE_HEADER_NAME)) {
313-
corehttpRequestBuilder.addHeader(Constants.CONTENT_TYPE_HEADER_NAME, Constants.JSON_CONTENT_TYPE);
314-
contenttype = Constants.JSON_CONTENT_TYPE;
315-
}
316-
}
317-
318-
RequestBody requestBody = null;
319-
// Handle cases where we've got a body to process.
320-
if (bytesToWrite != null) {
321-
final String mediaContentType = contenttype;
322-
requestBody = new RequestBody() {
323-
@Override
324-
public long contentLength() throws IOException {
325-
return bytesToWrite.length;
326-
}
327-
@Override
328-
public void writeTo(BufferedSink sink) throws IOException {
329-
OutputStream out = sink.outputStream();
330-
int writtenSoFar = 0;
331-
BufferedOutputStream bos = new BufferedOutputStream(out);
332-
int toWrite;
333-
do {
334-
toWrite = Math.min(defaultBufferSize, bytesToWrite.length - writtenSoFar);
335-
bos.write(bytesToWrite, writtenSoFar, toWrite);
336-
writtenSoFar = writtenSoFar + toWrite;
337-
if (progress != null) {
338-
executors.performOnForeground(writtenSoFar, bytesToWrite.length,
339-
progress);
340-
}
341-
} while (toWrite > 0);
342-
bos.close();
343-
out.close();
344-
}
345-
346-
@Override
347-
public MediaType contentType() {
348-
return MediaType.parse(mediaContentType);
349-
}
350-
};
351-
}
352-
353-
corehttpRequestBuilder.method(request.getHttpMethod().toString(), requestBody);
354-
coreHttpRequest = corehttpRequestBuilder.build();
355378

356379
// Call being executed
357-
response = corehttpClient.newCall(coreHttpRequest).execute();
380+
358381

359382
if (handler != null) {
360383
handler.configConnection(response);
@@ -420,11 +443,6 @@ public MediaType contentType() {
420443
final boolean shouldLogVerbosely = logger.getLoggingLevel() == LoggerLevel.DEBUG;
421444
logger.logError("Graph service exception " + ex.getMessage(shouldLogVerbosely), ex);
422445
throw ex;
423-
} catch (final UnsupportedEncodingException ex) {
424-
final ClientException clientException = new ClientException("Unsupported encoding problem: ",
425-
ex);
426-
logger.logError("Unsupported encoding problem: " + ex.getMessage(), ex);
427-
throw clientException;
428446
} catch (final Exception ex) {
429447
final ClientException clientException = new ClientException("Error during http request",
430448
ex);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import com.microsoft.graph.options.HeaderOption;
3636
import com.microsoft.graph.serializer.ISerializer;
3737

38+
import okhttp3.Request;
39+
3840
import java.io.BufferedInputStream;
3941
import java.io.BufferedOutputStream;
4042
import java.io.ByteArrayInputStream;
@@ -530,4 +532,10 @@ public IConnectionConfig getConnectionConfig() {
530532
public void setConnectionConfig(IConnectionConfig connectionConfig) {
531533
this.connectionConfig = connectionConfig;
532534
}
535+
536+
@Override
537+
public <Result, BodyType> Request getHttpRequest(IHttpRequest request, Class<Result> resultClass,
538+
BodyType serializable, IProgressCallback<Result> progress) throws ClientException {
539+
return null;
540+
}
533541
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
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.IConnectionConfig;
2829
import com.microsoft.graph.serializer.ISerializer;
2930

31+
import okhttp3.Request;
32+
3033
/**
3134
* Sends HTTP requests
3235
*/
@@ -105,4 +108,21 @@ <Result, BodyType, DeserializeType> Result send(final IHttpRequest request,
105108
final BodyType serializable,
106109
final IStatefulResponseHandler<Result, DeserializeType> handler)
107110
throws ClientException;
111+
/**
112+
* Sends the HTTP request
113+
*
114+
* @param request the request description
115+
* @param resultClass the class of the response from the service
116+
* @param serializable the object to send to the service in the body of the request
117+
* @param progress the progress callback for the request
118+
* @param <Result> the type of the response object
119+
* @param <BodyType> the type of the object to send to the service in the body of the request
120+
* @return the result from the request
121+
* @throws ClientException an exception occurs if the request was unable to complete for any reason
122+
*/
123+
<Result, BodyType> Request getHttpRequest(final IHttpRequest request,
124+
final Class<Result> resultClass,
125+
final BodyType serializable,
126+
final IProgressCallback<Result> progress)
127+
throws ClientException;
108128
}

0 commit comments

Comments
 (0)