Moved ListUrl, PutUrl and DeleteUrl to WebApi.

This commit is contained in:
verhoek
2018-06-01 16:21:28 +02:00
parent 83679c0f12
commit c063fde018
2 changed files with 73 additions and 43 deletions
@@ -157,14 +157,8 @@ namespace Duplicati.Library.Backend.GoogleDrive
}
var isUpdate = !string.IsNullOrWhiteSpace(fileId);
var values = new NameValueCollection {
{ WebApi.Google.QueryParam.UploadType,
WebApi.GoogleDrive.QueryValue.Resumable } };
var url = isUpdate ?
WebApi.GoogleDrive.FileUploadUrl(Library.Utility.Uri.UrlPathEncode(fileId), values) :
WebApi.GoogleDrive.FileUploadUrl(values);
var url = WebApi.GoogleDrive.PutUrl(fileId);
var item = new GoogleDriveFolderItem
{
@@ -276,7 +270,7 @@ namespace Duplicati.Library.Backend.GoogleDrive
{
foreach (var fileid in from n in GetFileEntries(remotename) select n.id)
{
var url = WebApi.GoogleDrive.FileQueryUrl(Library.Utility.Uri.UrlPathEncode(fileid), SupportsTeamDriveParam());
var url = WebApi.GoogleDrive.DeleteUrl(Library.Utility.Uri.UrlPathEncode(fileid), m_useTeamDrive);
m_oauth.GetJSONData<object>(url, x =>
{
x.Method = "DELETE";
@@ -360,7 +354,7 @@ namespace Duplicati.Library.Backend.GoogleDrive
public string[] DNSName
{
get { return new string[] { new System.Uri(WebApi.GoogleDrive.Url.DRIVE).Host, new System.Uri(WebApi.GoogleDrive.Url.UPLOAD).Host }; }
get { return WebApi.GoogleDrive.Hosts(); }
}
#endregion
@@ -412,21 +406,6 @@ namespace Duplicati.Library.Backend.GoogleDrive
}
#endregion
private NameValueCollection SupportsTeamDriveParam()
{
return m_useTeamDrive ? new NameValueCollection {
{ WebApi.GoogleDrive.QueryParam.SupportsTeamDrive,
WebApi.GoogleDrive.QueryValue.True }
} : null;
}
private NameValueCollection IncludeTeamDriveParam()
{
return m_useTeamDrive ? new NameValueCollection {
{ WebApi.GoogleDrive.QueryParam.IncludeTeamDrive,
WebApi.GoogleDrive.QueryValue.True } } : null;
}
private class GoogleDriveParentReference
{
@@ -503,17 +482,11 @@ namespace Duplicati.Library.Backend.GoogleDrive
string.Format("'{0}' in parents", EscapeTitleEntries(parentfolder))
};
var queryParams = new NameValueCollection
{
{WebApi.Google.QueryParam.File,
Library.Utility.Uri.UrlEncode(string.Join(" and ", fileQuery.Where(x => x != null)))},
};
queryParams.Add(SupportsTeamDriveParam());
queryParams.Add(IncludeTeamDriveParam());
var encodedFileQuery = Library.Utility.Uri.UrlEncode(string.Join(" and ", fileQuery.Where(x => x != null)));
var url = WebApi.GoogleDrive.ListUrl(encodedFileQuery, m_useTeamDrive);
while (true)
{
var url = WebApi.GoogleDrive.FileQueryUrl(queryParams);
var res = m_oauth.GetJSONData<GoogleDriveListResponse>(url);
foreach (var n in res.items)
yield return n;
@@ -521,8 +494,8 @@ namespace Duplicati.Library.Backend.GoogleDrive
var token = res.nextPageToken;
if (string.IsNullOrWhiteSpace(token))
break;
queryParams.Set(WebApi.Google.QueryParam.PageToken, token);
url = WebApi.GoogleDrive.ListUrl(encodedFileQuery, m_useTeamDrive, token);
}
}
@@ -86,7 +86,7 @@ namespace Duplicati.Library.Backend.WebApi
public static string[] Hosts()
{
return new string[] { new System.Uri(Url.UPLOAD).Host,
return new [] { new System.Uri(Url.UPLOAD).Host,
new System.Uri(Url.API).Host };
}
@@ -164,46 +164,67 @@ namespace Duplicati.Library.Backend.WebApi
public class GoogleDrive : Google
{
public static class Url
private static class Url
{
public const string DRIVE = "https://www.googleapis.com/drive/v2";
public const string UPLOAD = "https://www.googleapis.com/upload/drive/v2";
}
public static class Path
private class Path
{
public const string File = "files";
public const string About = "about";
}
public new class QueryParam : Google.QueryParam
private new class QueryParam : Google.QueryParam
{
public const string SupportsTeamDrive = "supportsTeamDrives";
public const string IncludeTeamDrive = "includeTeamDriveItems";
}
public static string FileQueryUrl(NameValueCollection values)
private static string FileQueryUrl(NameValueCollection values)
{
return Uri.UriBuilder(Url.DRIVE, Path.File, values);
}
public static string FileQueryUrl(string fileId, NameValueCollection values = null)
private static string FileQueryUrl(string fileId, NameValueCollection values = null)
{
return Uri.UriBuilder(Url.DRIVE, UrlPath.Create(Path.File).Append(fileId).ToString(),
values);
}
public static string FileUploadUrl(string fileId, NameValueCollection values)
private static string FileUploadUrl(string fileId, NameValueCollection values)
{
return Uri.UriBuilder(Url.UPLOAD, UrlPath.Create(Path.File).Append(fileId).ToString(),
values);
}
public static string FileUploadUrl(NameValueCollection values)
private static string FileUploadUrl(NameValueCollection values)
{
return Uri.UriBuilder(Url.UPLOAD, Path.File, values);
}
private static NameValueCollection SupportsTeamDriveParam(bool useTeamDrive)
{
return useTeamDrive ? new NameValueCollection {
{ WebApi.GoogleDrive.QueryParam.SupportsTeamDrive,
WebApi.GoogleDrive.QueryValue.True }
} : null;
}
private static NameValueCollection IncludeTeamDriveParam(bool useTeamDrive)
{
return useTeamDrive ? new NameValueCollection {
{ QueryParam.IncludeTeamDrive,
QueryValue.True } } : null;
}
public static string[] Hosts()
{
return new [] { new System.Uri(Url.DRIVE).Host, new System.Uri(Url.UPLOAD).Host };
}
public static string GetUrl(string fileId)
{
return FileQueryUrl(fileId, new NameValueCollection{
@@ -213,7 +234,43 @@ namespace Duplicati.Library.Backend.WebApi
public static string RenameUrl(string fileId)
{
return FileQueryUrl(Uri.UrlPathEncode(fileId);
return FileQueryUrl(Uri.UrlPathEncode(fileId));
}
public static string DeleteUrl(string fileId, bool useTeamDrive)
{
return FileQueryUrl(Uri.UrlPathEncode(fileId), SupportsTeamDriveParam(useTeamDrive));
}
public static string PutUrl(string fileId)
{
var queryParams = new NameValueCollection {
{ Google.QueryParam.UploadType,
QueryValue.Resumable } };
return !string.IsNullOrWhiteSpace(fileId) ?
FileUploadUrl(Uri.UrlPathEncode(fileId), queryParams) :
FileUploadUrl(queryParams);
}
public static string ListUrl(string fileQuery, bool useTeamDrive, string token=null)
{
var queryParams = new NameValueCollection
{
{ Google.QueryParam.File,
fileQuery},
};
queryParams.Add(SupportsTeamDriveParam(useTeamDrive));
queryParams.Add(IncludeTeamDriveParam(useTeamDrive));
if (token != null)
{
queryParams.Set(Google.QueryParam.PageToken, token);
}
return FileQueryUrl(queryParams);
}
}
}