-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work for searching and displaying list of google drive spreadsheets.
- Loading branch information
Showing
4 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using RLE.Core.Wrappers; | ||
|
||
namespace RLE.Core.Services; | ||
|
||
public interface IGoogleDriveService | ||
{ | ||
public Task<IList<string>> GetSheetFiles(); | ||
} | ||
|
||
public class GoogleDriveService : IGoogleDriveService | ||
{ | ||
private DriveServiceWrapper _driveService; | ||
|
||
public GoogleDriveService(string accessToken) | ||
{ | ||
_driveService = new DriveServiceWrapper(accessToken); | ||
} | ||
|
||
public GoogleDriveService(Dictionary<string, string> parameters) | ||
{ | ||
_driveService = new DriveServiceWrapper(parameters); | ||
} | ||
|
||
public async Task<IList<string>> GetSheetFiles() | ||
{ | ||
var sheets = await _driveService.GetSheetFiles(); | ||
|
||
var sheetList = sheets.Select(s => s.Name.ToString()).ToList(); | ||
|
||
return sheetList; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Google.Apis.Auth.OAuth2; | ||
using Google.Apis.Drive.v3; | ||
using RLE.Core.Constants; | ||
using File = Google.Apis.Drive.v3.Data.File; | ||
|
||
namespace RLE.Core.Wrappers; | ||
|
||
public interface IDriveServiceWrapper | ||
{ | ||
Task<IList<File>> GetSheetFiles(); | ||
Task<IList<File>> SearchSheetFiles(string name); | ||
} | ||
public class DriveServiceWrapper : DriveService, IDriveServiceWrapper | ||
{ | ||
private DriveService _driveService = new(); | ||
|
||
public DriveServiceWrapper(string accessToken) | ||
{ | ||
var credential = GoogleCredential.FromAccessToken(accessToken.Trim()); | ||
|
||
InitializeService(credential); | ||
} | ||
|
||
public DriveServiceWrapper(Dictionary<string, string> parameters) | ||
{ | ||
var jsonCredential = new JsonCredentialParameters | ||
{ | ||
Type = parameters["type"].Trim(), | ||
PrivateKeyId = parameters["privateKeyId"].Trim(), | ||
PrivateKey = parameters["privateKey"].Trim(), | ||
ClientEmail = parameters["clientEmail"].Trim(), | ||
ClientId = parameters["clientId"].Trim(), | ||
}; | ||
|
||
var credential = GoogleCredential.FromJsonParameters(jsonCredential); | ||
|
||
InitializeService(credential); | ||
} | ||
|
||
private DriveService InitializeService(GoogleCredential credential) | ||
{ | ||
_driveService = new DriveService(new Initializer() | ||
{ | ||
HttpClientInitializer = credential, | ||
ApplicationName = GoogleConfig.AppName | ||
}); | ||
|
||
return _driveService; | ||
} | ||
|
||
public async Task<IList<File>> GetSheetFiles() | ||
{ | ||
// Define parameters of request. | ||
FilesResource.ListRequest listRequest = _driveService.Files.List(); | ||
//listRequest.PageSize = 10; | ||
listRequest.Q = "mimeType='application/vnd.google-apps.spreadsheet'"; | ||
|
||
// List files. | ||
return (await listRequest.ExecuteAsync()).Files; | ||
} | ||
|
||
public async Task<IList<File>> SearchSheetFiles(string name) | ||
{ | ||
// Define parameters of request. | ||
FilesResource.ListRequest listRequest = _driveService.Files.List(); | ||
//listRequest.PageSize = 10; | ||
listRequest.Q = $"mimeType='application/vnd.google-apps.spreadsheet' and name contains '{name}'"; | ||
|
||
// List files. | ||
return (await listRequest.ExecuteAsync()).Files; | ||
} | ||
} |
This file contains 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