Last active
July 23, 2018 03:42
-
-
Save LindaLawton/ae2f95c777d65770302ac5a5562cb4fa to your computer and use it in GitHub Desktop.
Generated sample for Google Drive v3. Current issues with body, and needing to test the upload methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2016 DAIMTO : www.daimto.com | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | |
// the License. You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | |
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
// specific language governing permissions and limitations under the License. | |
//------------------------------------------------------------------------------ | |
// <auto-generated> | |
// This code was generated by DAIMTO-Google-apis-Sample-generator 1.0.0 | |
// Template File Name: Methodtemplate.tt | |
// Build date: 12/11/2016 18:20:16 | |
// C# generater version: 1.0.0 | |
// | |
// Changes to this file may cause incorrect behavior and will be lost if | |
// the code is regenerated. | |
// </auto-generated> | |
//------------------------------------------------------------------------------ | |
// About | |
// | |
// Unoffical sample for the Drive v3 API for C#. | |
// This sample is designed to be used with the Google .Net client library. (https://github.com/google/google-api-dotnet-client) | |
// | |
// API Description: Manages files in Drive including uploading, downloading, searching, detecting changes, and updating sharing permissions. | |
// API Documentation Link https://developers.google.com/drive/ | |
// | |
// Discovery Doc https://www.googleapis.com/discovery/v1/apis/Drive/v3/rest | |
// | |
//------------------------------------------------------------------------------ | |
// Installation | |
// | |
// This sample code uses the Google .Net client library | |
// | |
// NuGet package: | |
// | |
// Location: https://www.nuget.org/packages/Google.Apis.Drive.v3/ | |
// Install Command: PM> Install-Package Google.Apis.Drive.v3 | |
// | |
//------------------------------------------------------------------------------ | |
using Google.Apis.Drive.v3; | |
using System; | |
namespace GoogleSamplecSharpSample.Drivev3.Methods | |
{ | |
public class FilesSample | |
{ | |
public class FilesCopyOptionalParms | |
{ | |
/// Whether to ignore the domain's default visibility settings for the created file. Domain administrators can choose to make all uploaded files visible to the domain by default; this parameter bypasses that behavior for the request. Permissions are still inherited from parent folders. | |
public bool IgnoreDefaultVisibility { get; set; } | |
/// Whether to set the 'keepForever' field in the new head revision. This is only applicable to files with binary content in Drive. | |
public bool KeepRevisionForever { get; set; } | |
/// A language hint for OCR processing during image import (ISO 639-1 code). | |
public string OcrLanguage { get; set; } | |
} | |
/// <summary> | |
/// Creates a copy of a file and applies any requested updates with patch semantics. | |
/// Documentation https://developers.google.com/drive/v3/reference/files/copy | |
/// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. | |
/// </summary> | |
/// <param name="service">Authenticated Drive service.</param> | |
/// <param name="fileId">The ID of the file.</param> | |
/// <param name="body">A valid Drive v3 body.</param> | |
/// <param name="optional">Optional paramaters.</param> | |
/// <returns>FileResponse</returns> | |
public static Google.Apis.Drive.v3.Data.File Copy(DriveService service, string fileId, Google.Apis.Drive.v3.Data.File body, FilesCopyOptionalParms optional = null) | |
{ | |
try | |
{ | |
// Initial validation. | |
if (service == null) | |
throw new ArgumentNullException("service"); | |
if (body == null) | |
throw new ArgumentNullException("body"); | |
if (fileId == null) | |
throw new ArgumentNullException(fileId); | |
// Building the initial request. | |
var request = service.Files.Copy(body, fileId); | |
// Applying optional parameters to the request. | |
request = (FilesResource.CreateRequest)SampleHelpers.ApplyOptionalParms(request, optional); | |
// Requesting data. | |
return request.Execute(); | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception("Request Files.Copy failed.", ex); | |
} | |
} | |
public class FilesCreateOptionalParms | |
{ | |
/// Whether to ignore the domain's default visibility settings for the created file. Domain administrators can choose to make all uploaded files visible to the domain by default; this parameter bypasses that behavior for the request. Permissions are still inherited from parent folders. | |
public bool IgnoreDefaultVisibility { get; set; } | |
/// Whether to set the 'keepForever' field in the new head revision. This is only applicable to files with binary content in Drive. | |
public bool KeepRevisionForever { get; set; } | |
/// A language hint for OCR processing during image import (ISO 639-1 code). | |
public string OcrLanguage { get; set; } | |
/// Whether to use the uploaded content as indexable text. | |
public bool UseContentAsIndexableText { get; set; } | |
} | |
/// <summary> | |
/// Creates a new file. | |
/// Documentation https://developers.google.com/drive/v3/reference/files/create | |
/// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. | |
/// </summary> | |
/// <param name="service">Authenticated Drive service.</param> | |
/// <param name="body">A valid Drive v3 body.</param> | |
/// <param name="optional">Optional paramaters.</param> | |
/// <returns>FileResponse</returns> | |
public static Google.Apis.Drive.v3.Data.File Create(DriveService service, Google.Apis.Drive.v3.Data.File body, FilesCreateOptionalParms optional = null) | |
{ | |
try | |
{ | |
// Initial validation. | |
if (service == null) | |
throw new ArgumentNullException("service"); | |
if (body == null) | |
throw new ArgumentNullException("body"); | |
// Building the initial request. | |
var request = service.Files.Create(body); | |
// Applying optional parameters to the request. | |
request = (FilesResource.CreateRequest)SampleHelpers.ApplyOptionalParms(request, optional); | |
// Requesting data. | |
return request.Execute(); | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception("Request Files.Create failed.", ex); | |
} | |
} | |
/// <summary> | |
/// Permanently deletes a file owned by the user without moving it to the trash. If the target is a folder, all descendants owned by the user are also deleted. | |
/// Documentation https://developers.google.com/drive/v3/reference/files/delete | |
/// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. | |
/// </summary> | |
/// <param name="service">Authenticated Drive service.</param> | |
/// <param name="fileId">The ID of the file.</param> | |
public static void Delete(DriveService service, string fileId) | |
{ | |
try | |
{ | |
// Initial validation. | |
if (service == null) | |
throw new ArgumentNullException("service"); | |
if (fileId == null) | |
throw new ArgumentNullException(fileId); | |
// Make the request. | |
return service.Files.Delete(fileId).Execute(); | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception("Request Files.Delete failed.", ex); | |
} | |
} | |
/// <summary> | |
/// Permanently deletes all of the user's trashed files. | |
/// Documentation https://developers.google.com/drive/v3/reference/files/emptyTrash | |
/// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. | |
/// </summary> | |
/// <param name="service">Authenticated Drive service.</param> | |
public static void EmptyTrash(DriveService service) | |
{ | |
try | |
{ | |
// Initial validation. | |
if (service == null) | |
throw new ArgumentNullException("service"); | |
// Make the request. | |
return service.Files.EmptyTrash().Execute(); | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception("Request Files.EmptyTrash failed.", ex); | |
} | |
} | |
/// <summary> | |
/// Exports a Google Doc to the requested MIME type and returns the exported content. | |
/// Documentation https://developers.google.com/drive/v3/reference/files/export | |
/// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. | |
/// </summary> | |
/// <param name="service">Authenticated Drive service.</param> | |
/// <param name="fileId">The ID of the file.</param> | |
/// <param name="mimeType">The MIME type of the format requested for this export.</param> | |
public static void Export(DriveService service, string fileId, string mimeType) | |
{ | |
try | |
{ | |
// Initial validation. | |
if (service == null) | |
throw new ArgumentNullException("service"); | |
if (fileId == null) | |
throw new ArgumentNullException(fileId); | |
if (mimeType == null) | |
throw new ArgumentNullException(mimeType); | |
// Make the request. | |
return service.Files.Export(fileId, mimeType).Execute(); | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception("Request Files.Export failed.", ex); | |
} | |
} | |
public class FilesGenerateIdsOptionalParms | |
{ | |
/// The number of IDs to return. | |
public int Count { get; set; } | |
/// The space in which the IDs can be used to create new files. Supported values are 'drive' and 'appDataFolder'. | |
public string Space { get; set; } | |
} | |
/// <summary> | |
/// Generates a set of file IDs which can be provided in create requests. | |
/// Documentation https://developers.google.com/drive/v3/reference/files/generateIds | |
/// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. | |
/// </summary> | |
/// <param name="service">Authenticated Drive service.</param> | |
/// <param name="optional">Optional paramaters.</param> | |
/// <returns>GeneratedIdsResponse</returns> | |
public static Google.Apis.Drive.v3.Data.GeneratedIds GenerateIds(DriveService service, FilesGenerateIdsOptionalParms optional = null) | |
{ | |
try | |
{ | |
// Initial validation. | |
if (service == null) | |
throw new ArgumentNullException("service"); | |
// Building the initial request. | |
var request = service.Files.GenerateIds(); | |
// Applying optional parameters to the request. | |
request = (FilesResource.CreateRequest)SampleHelpers.ApplyOptionalParms(request, optional); | |
// Requesting data. | |
return request.Execute(); | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception("Request Files.GenerateIds failed.", ex); | |
} | |
} | |
public class FilesGetOptionalParms | |
{ | |
/// Whether the user is acknowledging the risk of downloading known malware or other abusive files. This is only applicable when alt=media. | |
public bool AcknowledgeAbuse { get; set; } | |
} | |
/// <summary> | |
/// Gets a file's metadata or content by ID. | |
/// Documentation https://developers.google.com/drive/v3/reference/files/get | |
/// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. | |
/// </summary> | |
/// <param name="service">Authenticated Drive service.</param> | |
/// <param name="fileId">The ID of the file.</param> | |
/// <param name="optional">Optional paramaters.</param> | |
/// <returns>FileResponse</returns> | |
public static Google.Apis.Drive.v3.Data.File Get(DriveService service, string fileId, FilesGetOptionalParms optional = null) | |
{ | |
try | |
{ | |
// Initial validation. | |
if (service == null) | |
throw new ArgumentNullException("service"); | |
if (fileId == null) | |
throw new ArgumentNullException(fileId); | |
// Building the initial request. | |
var request = service.Files.Get(fileId); | |
// Applying optional parameters to the request. | |
request = (FilesResource.CreateRequest)SampleHelpers.ApplyOptionalParms(request, optional); | |
// Requesting data. | |
return request.Execute(); | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception("Request Files.Get failed.", ex); | |
} | |
} | |
public class FilesListOptionalParms | |
{ | |
/// The source of files to list. | |
public string Corpus { get; set; } | |
/// A comma-separated list of sort keys. Valid keys are 'createdTime', 'folder', 'modifiedByMeTime', 'modifiedTime', 'name', 'quotaBytesUsed', 'recency', 'sharedWithMeTime', 'starred', and 'viewedByMeTime'. Each key sorts ascending by default, but may be reversed with the 'desc' modifier. Example usage: ?orderBy=folder,modifiedTime desc,name. Please note that there is a current limitation for users with approximately one million files in which the requested sort order is ignored. | |
public string OrderBy { get; set; } | |
/// The maximum number of files to return per page. | |
public int PageSize { get; set; } | |
/// The token for continuing a previous list request on the next page. This should be set to the value of 'nextPageToken' from the previous response. | |
public string PageToken { get; set; } | |
/// A query for filtering the file results. See the "Search for Files" guide for supported syntax. | |
public string Q { get; set; } | |
/// A comma-separated list of spaces to query within the corpus. Supported values are 'drive', 'appDataFolder' and 'photos'. | |
public string Spaces { get; set; } | |
} | |
/// <summary> | |
/// Lists or searches files. | |
/// Documentation https://developers.google.com/drive/v3/reference/files/list | |
/// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. | |
/// </summary> | |
/// <param name="service">Authenticated Drive service.</param> | |
/// <param name="optional">Optional paramaters.</param> | |
/// <returns>FileListResponse</returns> | |
public static Google.Apis.Drive.v3.Data.FileList List(DriveService service, FilesListOptionalParms optional = null) | |
{ | |
try | |
{ | |
// Initial validation. | |
if (service == null) | |
throw new ArgumentNullException("service"); | |
// Building the initial request. | |
var request = service.Files.List(); | |
// Applying optional parameters to the request. | |
request = (FilesResource.CreateRequest)SampleHelpers.ApplyOptionalParms(request, optional); | |
// Requesting data. | |
return request.Execute(); | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception("Request Files.List failed.", ex); | |
} | |
} | |
public class FilesUpdateOptionalParms | |
{ | |
/// A comma-separated list of parent IDs to add. | |
public string AddParents { get; set; } | |
/// Whether to set the 'keepForever' field in the new head revision. This is only applicable to files with binary content in Drive. | |
public bool KeepRevisionForever { get; set; } | |
/// A language hint for OCR processing during image import (ISO 639-1 code). | |
public string OcrLanguage { get; set; } | |
/// A comma-separated list of parent IDs to remove. | |
public string RemoveParents { get; set; } | |
/// Whether to use the uploaded content as indexable text. | |
public bool UseContentAsIndexableText { get; set; } | |
} | |
/// <summary> | |
/// Updates a file's metadata and/or content with patch semantics. | |
/// Documentation https://developers.google.com/drive/v3/reference/files/update | |
/// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. | |
/// </summary> | |
/// <param name="service">Authenticated Drive service.</param> | |
/// <param name="fileId">The ID of the file.</param> | |
/// <param name="body">A valid Drive v3 body.</param> | |
/// <param name="optional">Optional paramaters.</param> | |
/// <returns>FileResponse</returns> | |
public static Google.Apis.Drive.v3.Data.File Update(DriveService service, string fileId, Google.Apis.Drive.v3.Data.File body, FilesUpdateOptionalParms optional = null) | |
{ | |
try | |
{ | |
// Initial validation. | |
if (service == null) | |
throw new ArgumentNullException("service"); | |
if (body == null) | |
throw new ArgumentNullException("body"); | |
if (fileId == null) | |
throw new ArgumentNullException(fileId); | |
// Building the initial request. | |
var request = service.Files.Update(body, fileId); | |
// Applying optional parameters to the request. | |
request = (FilesResource.CreateRequest)SampleHelpers.ApplyOptionalParms(request, optional); | |
// Requesting data. | |
return request.Execute(); | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception("Request Files.Update failed.", ex); | |
} | |
} | |
public class FilesWatchOptionalParms | |
{ | |
/// Whether the user is acknowledging the risk of downloading known malware or other abusive files. This is only applicable when alt=media. | |
public bool AcknowledgeAbuse { get; set; } | |
} | |
/// <summary> | |
/// Subscribes to changes to a file | |
/// Documentation https://developers.google.com/drive/v3/reference/files/watch | |
/// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. | |
/// </summary> | |
/// <param name="service">Authenticated Drive service.</param> | |
/// <param name="fileId">The ID of the file.</param> | |
/// <param name="body">A valid Drive v3 body.</param> | |
/// <param name="optional">Optional paramaters.</param> | |
/// <returns>ChannelResponse</returns> | |
public static Google.Apis.Drive.v3.Data.Channel Watch(DriveService service, string fileId, Google.Apis.Drive.v3.Data.Channel body, FilesWatchOptionalParms optional = null) | |
{ | |
try | |
{ | |
// Initial validation. | |
if (service == null) | |
throw new ArgumentNullException("service"); | |
if (body == null) | |
throw new ArgumentNullException("body"); | |
if (fileId == null) | |
throw new ArgumentNullException(fileId); | |
// Building the initial request. | |
var request = service.Files.Watch(body, fileId); | |
// Applying optional parameters to the request. | |
request = (FilesResource.CreateRequest)SampleHelpers.ApplyOptionalParms(request, optional); | |
// Requesting data. | |
return request.Execute(); | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception("Request Files.Watch failed.", ex); | |
} | |
} | |
} | |
public class SampleHelpers | |
{ | |
/// <summary> | |
/// Using reflection to apply optional parameters to the request. | |
/// | |
/// If the optonal parameters are null then we will just return the request as is. | |
/// </summary> | |
/// <param name="request">The request. </param> | |
/// <param name="optional">The optional parameters. </param> | |
/// <returns></returns> | |
public static object ApplyOptionalParms(object request, object optional) | |
{ | |
if (optional == null) | |
return request; | |
System.Reflection.PropertyInfo[] optionalProperties = (optional.GetType()).GetProperties(); | |
foreach (System.Reflection.PropertyInfo property in optionalProperties) | |
{ | |
// Copy value from optional parms to the request. They should have the same names and datatypes. | |
System.Reflection.PropertyInfo piShared = (request.GetType()).GetProperty(property.Name); | |
if (property.GetValue(optional, null) != null) // TODO Test that we do not add values for items that are null | |
piShared.SetValue(request, property.GetValue(optional, null), null); | |
} | |
return request; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment