update API to use full item addressing

This commit is contained in:
skilion 2017-12-27 15:12:38 +01:00
parent d65d00c46d
commit d9c9915bc3

View file

@ -8,12 +8,12 @@ static import log;
private immutable { private immutable {
string clientId = "22c49a0d-d21c-4792-aed1-8f163c982546"; string clientId = "22c49a0d-d21c-4792-aed1-8f163c982546";
string authUrl = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"; string authUrl = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
string redirectUrl = "https://login.microsoftonline.com/common/oauth2/nativeclient"; string redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
string tokenUrl = "https://login.microsoftonline.com/common/oauth2/v2.0/token"; string tokenUrl = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
string driveUrl = "https://graph.microsoft.com/v1.0/me/drive"; string driveUrl = "https://graph.microsoft.com/v1.0/me/drive";
string itemByIdUrl = "https://graph.microsoft.com/v1.0/me/drive/items/"; string itemByIdUrl = "https://graph.microsoft.com/v1.0/me/drive/items/";
string itemByPathUrl = "https://graph.microsoft.com/v1.0/me/drive/root:/"; string itemByPathUrl = "https://graph.microsoft.com/v1.0/me/drive/root:/";
string driveByIdUrl = "https://graph.microsoft.com/v1.0/me/drives/"; string driveByIdUrl = "https://graph.microsoft.com/v1.0/drives/";
} }
class OneDriveException: Exception class OneDriveException: Exception
@ -90,48 +90,54 @@ final class OneDriveApi
return true; return true;
} }
// https://dev.onedrive.com/drives/default.htm // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/drive_get
JSONValue getDefaultDrive() JSONValue getDefaultDrive()
{ {
checkAccessTokenExpired(); checkAccessTokenExpired();
return get(driveUrl); return get(driveUrl);
} }
// https://dev.onedrive.com/items/view_delta.htm // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get
JSONValue getDefaultRoot()
{
checkAccessTokenExpired();
return get(driveUrl ~ "/root");
}
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_delta
JSONValue viewChangesById(const(char)[] driveId, const(char)[] id, const(char)[] deltaLink) JSONValue viewChangesById(const(char)[] driveId, const(char)[] id, const(char)[] deltaLink)
{ {
checkAccessTokenExpired(); checkAccessTokenExpired();
if (deltaLink) return get(deltaLink); const(char)[] url = deltaLink;
const(char)[] url = driveByIdUrl ~ driveId ~ "/items/" ~ id ~ "/delta"; if (url == null) {
url ~= "?select=id,name,eTag,cTag,deleted,file,folder,root,fileSystemInfo,remoteItem,parentReference"; url = driveByIdUrl ~ driveId ~ "/items/" ~ id ~ "/delta";
url ~= "?select=id,name,eTag,cTag,deleted,file,folder,root,fileSystemInfo,remoteItem,parentReference";
}
return get(url); return get(url);
} }
// https://dev.onedrive.com/items/view_delta.htm // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_delta
JSONValue viewChangesByPath(const(char)[] path, const(char)[] deltaLink) JSONValue viewChangesByPath(const(char)[] path, const(char)[] deltaLink)
{ {
checkAccessTokenExpired(); checkAccessTokenExpired();
if (deltaLink) return get(deltaLink); const(char)[] url = deltaLink;
string url = itemByPathUrl ~ encodeComponent(path) ~ ":/delta"; if (url == null) {
// HACK if (path == ".") url = driveUrl ~ "/root/delta";
if (path == ".") url = driveUrl ~ "/root/delta"; else url = itemByPathUrl ~ encodeComponent(path) ~ ":/delta";
url ~= "?select=id,name,eTag,cTag,deleted,file,folder,root,fileSystemInfo,remoteItem,parentReference"; url ~= "?select=id,name,eTag,cTag,deleted,file,folder,root,fileSystemInfo,remoteItem,parentReference";
}
return get(url); return get(url);
} }
// https://dev.onedrive.com/items/download.htm // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get_content
void downloadById(const(char)[] id, string saveToPath) void downloadById(const(char)[] driveId, const(char)[] id, string saveToPath)
{ {
checkAccessTokenExpired(); checkAccessTokenExpired();
import std.file;
scope(failure) { scope(failure) {
if (exists(saveToPath)) remove(saveToPath); if (exists(saveToPath)) remove(saveToPath);
} }
// mkdir if need, or File(saveToPath, "wb") may fail mkdirRecurse(dirName(saveToPath));
if ( !exists(dirName(saveToPath)) ) { const(char)[] url = driveByIdUrl ~ driveId ~ "/items/" ~ id ~ "/content?AVOverride=1";
mkdirRecurse(dirName(saveToPath));
}
const(char)[] url = itemByIdUrl ~ id ~ "/content?AVOverride=1";
download(url, saveToPath); download(url, saveToPath);
} }
@ -145,21 +151,21 @@ final class OneDriveApi
return upload(localPath, url); return upload(localPath, url);
} }
// https://dev.onedrive.com/items/update.htm // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_update
JSONValue updateById(const(char)[] id, JSONValue data, const(char)[] eTag = null) JSONValue updateById(const(char)[] driveId, const(char)[] id, JSONValue data, const(char)[] eTag = null)
{ {
checkAccessTokenExpired(); checkAccessTokenExpired();
char[] url = itemByIdUrl ~ id; const(char)[] url = driveByIdUrl ~ driveId ~ "/items/" ~ id;
if (eTag) http.addRequestHeader("If-Match", eTag); if (eTag) http.addRequestHeader("If-Match", eTag);
http.addRequestHeader("Content-Type", "application/json"); http.addRequestHeader("Content-Type", "application/json");
return patch(url, data.toString()); return patch(url, data.toString());
} }
// https://dev.onedrive.com/items/delete.htm // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_delete
void deleteById(const(char)[] id, const(char)[] eTag = null) void deleteById(const(char)[] driveId, const(char)[] id, const(char)[] eTag = null)
{ {
checkAccessTokenExpired(); checkAccessTokenExpired();
char[] url = itemByIdUrl ~ id; const(char)[] url = driveByIdUrl ~ driveId ~ "/items/" ~ id;
//TODO: investigate why this always fail with 412 (Precondition Failed) //TODO: investigate why this always fail with 412 (Precondition Failed)
//if (eTag) http.addRequestHeader("If-Match", eTag); //if (eTag) http.addRequestHeader("If-Match", eTag);
del(url); del(url);