Update onedrive.d

* Update patch() and consumers of patch() to flag if the JSON response needs to be validated or not
* Update checking of response when a 204 generated
This commit is contained in:
abraunegg 2026-01-27 18:19:28 +11:00
commit e2ad2db383

View file

@ -1012,7 +1012,7 @@ class OneDriveApi {
string[string] requestHeaders;
const(char)[] url = driveByIdUrl ~ driveId ~ "/items/" ~ id;
if (eTag) requestHeaders["If-Match"] = to!string(eTag);
return patch(url, data.toString(), requestHeaders);
return patch(url, data.toString(), false, requestHeaders);
}
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_delete
@ -1152,7 +1152,7 @@ class OneDriveApi {
const JSONValue request = [
"expirationDateTime": expirationDateTime.toISOExtString()
];
return patch(url, request.toString());
return patch(url, request.toString(), true);
}
// Delete Webhook subscription
@ -1755,8 +1755,8 @@ class OneDriveApi {
}, validateJSONResponse, callingFunction, lineno);
}
private JSONValue patch(const(char)[] url, const(char)[] patchData, string[string] requestHeaders=null, const(char)[] contentType = "application/json", string callingFunction=__FUNCTION__, int lineno=__LINE__) {
bool validateJSONResponse = true;
private JSONValue patch(const(char)[] url, const(char)[] patchData, bool validateJSONResponseInput, string[string] requestHeaders=null, const(char)[] contentType = "application/json", string callingFunction=__FUNCTION__, int lineno=__LINE__) {
bool validateJSONResponse = validateJSONResponseInput;
return oneDriveErrorHandlerWrapper((CurlResponse response) {
connect(HTTP.Method.patch, url, false, response, requestHeaders);
curlEngine.setContent(contentType, patchData);
@ -1851,11 +1851,19 @@ class OneDriveApi {
// Do we need to validate the JSON response?
if (validateJSONResponse) {
// 204 = No Content (valid success for PATCH/DELETE events)
// Also allow an empty response body (some Graph operations legitimately return no payload)
if (response.statusLine.code != 204 && response.content.length != 0) {
const code = response.statusLine.code;
// 204 = No Content is a valid success response for some Graph operations (e.g. PATCH/DELETE).
// In that case, there is no JSON payload to validate.
if (code != 204) {
// If caller expects JSON, an empty body is not acceptable
if (response.content.length == 0) {
throw new OneDriveException( 0, "Caller requested a JSON object response, but the response body was empty", response);
}
// Body is present: it must be a JSON object
if (result.type() != JSONType.object) {
throw new OneDriveException(0, "Caller request a non null JSON response, get null instead", response);
throw new OneDriveException(0, "Caller requested a JSON object response, but the response was not a JSON object", response);
}
}
}