Skip to content

Commit cdae209

Browse files
committed
fix: microsoft need not be strict in response-body expectation
1 parent 9e967cb commit cdae209

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

extensions/data-transfer/portability-data-transfer-microsoft/src/main/java/org/datatransferproject/transfer/microsoft/MicrosoftApiResponse.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,19 @@ public abstract class MicrosoftApiResponse {
6464
/** HTTP body of the response if any was present. */
6565
public abstract Optional<String> body();
6666

67+
/** HTTP body of the response if any was present. */
68+
public abstract Optional<IOException> bodyException();
69+
6770
/**
6871
* Builds from key fields within an HTTP response, closing said response
6972
*
73+
* <p>If the body is a requirement and you need to fail-fast for bad response-bodies, then make
74+
* sure you're calling an introspective method that tries to actually utiilze the body, eg: {@link
75+
* getJsonValue} (or add a new one). By default, because bodies are not always provided or needed,
76+
* this construction method tries to save the response-body but stores an error in {@link
77+
* bodyException} (which will be included in the usual exception output via {@link
78+
* throwDtpException} and co).
79+
*
7080
* <p>Warning: this loads the entire response body to memory, as we assume all Microsoft API
7181
* responses we're dealing with are (at the most complex end) just JSON responses intended to be
7282
* parsed (as opposed to say, streams of an arbitrarily-large file's bytes).
@@ -81,15 +91,13 @@ public static MicrosoftApiResponse ofResponse(Response response) throws IOExcept
8191
final String body;
8292
try {
8393
body = response.body().string();
94+
if (!Strings.isNullOrEmpty(body)) {
95+
builder.setBody(body);
96+
}
8497
} catch (IOException e) {
85-
throw builder
86-
.build()
87-
.toIoException("bug? loading body from response shouldn't hit IOException, but did", e);
98+
builder.setBodyException(e);
8899
}
89100

90-
if (!Strings.isNullOrEmpty(body)) {
91-
builder.setBody(body);
92-
}
93101
response.close(); // only close if we _had_ a body field to read
94102
}
95103

@@ -104,6 +112,8 @@ public abstract static class Builder {
104112

105113
public abstract Builder setBody(String body);
106114

115+
public abstract Builder setBodyException(IOException bodyException);
116+
107117
public abstract MicrosoftApiResponse build();
108118
}
109119

extensions/data-transfer/portability-data-transfer-microsoft/src/main/java/org/datatransferproject/transfer/microsoft/media/MicrosoftMediaImporter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ private Request.Builder buildCreateUploadSessionPath(
321321
* <ul>
322322
* <li>https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0#upload-bytes-to-the-upload-session
323323
* </ul>
324+
*
325+
* Returns a response, which is only expected to have a response-body in the
326+
* event that this is the final chunk.
324327
*/
325328
private MicrosoftApiResponse uploadChunk(
326329
DataChunk chunk, String photoUploadUrl, long totalFileSize, String mediaType)
@@ -390,7 +393,10 @@ private Pair<Request, MicrosoftApiResponse> tryWithCreds(Request.Builder request
390393
* standard token-refresh retry options.
391394
*
392395
* <p>Prefer {@link tryWithCredsOrFail(Request.Builder, String, String)} if you ultimately only
393-
* care about a particular JSON key in the response body.
396+
* care about a particular JSON key in the response body. If you don't
397+
* need/expect a response body (or don't know yet) then this is the right
398+
* method to call instead (and then call {@link
399+
* MicrosoftApiResponse#getJsonValue} yourself later).
394400
*
395401
* <p>Example usage:
396402
*

0 commit comments

Comments
 (0)