Skip to content

Commit

Permalink
Merge pull request #87 from abhinavminhas/dev
Browse files Browse the repository at this point in the history
Nuget Package Creation - v1.11.0
  • Loading branch information
abhinavminhas authored Dec 22, 2024
2 parents 57a4cce + 71df123 commit 4e6a36e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-nuget-Package.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Publish Nuget Package

env:
NUGET_PACKAGE_NAME_VERSION: "GmailHelper.1.10.2.nupkg"
NUGET_PACKAGE_NAME_VERSION: "GmailHelper.1.11.0.nupkg"

on:
workflow_dispatch:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project documented here.

## [Released]

## [1.11.0](https://www.nuget.org/packages/GmailHelper/1.11.0) - 2024-12-22
### Changed
- GetGmailService() - Additional optional argument 'credentialsPath' to define custom 'credentials.json' file path.

## [1.10.2](https://www.nuget.org/packages/GmailHelper/1.10.2) - 2024-12-13
### Changed
- MimeKitLite dependency update from ('4.8.0' -> '4.9.0').
Expand Down
33 changes: 28 additions & 5 deletions GmailAPIHelper.CORE.Tests/GmailTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ public void Test_GetGmailService_TokenPath_WorkingDirectory()
[TestCategory("GMAIL-TESTS-DOTNETCORE")]
public void Test_GetGmailService_TokenPath_Custom()
{
var credPath = "";
var tokenPath = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
credPath = Environment.CurrentDirectory + "\\" + "token.json";
tokenPath = Environment.CurrentDirectory + "\\" + "token.json";
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
credPath = Environment.CurrentDirectory + "/" + "token.json";
tokenPath = Environment.CurrentDirectory + "/" + "token.json";
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
credPath = Environment.CurrentDirectory + "/" + "token.json";
var service = GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.CUSTOM, credPath);
tokenPath = Environment.CurrentDirectory + "/" + "token.json";
var service = GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.CUSTOM, tokenPath);
Assert.IsTrue(service.GetType() == typeof(Google.Apis.Gmail.v1.GmailService));
}

Expand All @@ -117,6 +117,29 @@ public void Test_GetGmailService_TokenPath_Custom_EmptyPath()
catch (ArgumentException) { }
}

[TestMethod]
[TestCategory("GMAIL-TESTS-DOTNETCORE")]
public void Test_GetGmailService_CredentialsPath_Custom()
{
var tokenPath = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
tokenPath = Environment.CurrentDirectory + "\\" + "token.json";
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
tokenPath = Environment.CurrentDirectory + "/" + "token.json";
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
tokenPath = Environment.CurrentDirectory + "/" + "token.json";
var credentialsPath = Environment.CurrentDirectory + "/" + "credentials.json";
// Token Path & Credentials Path
var service = GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.CUSTOM, tokenPath, credentialsPath);
Assert.IsTrue(service.GetType() == typeof(Google.Apis.Gmail.v1.GmailService));
// Only Token Path
service = GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.CUSTOM, tokenPath);
Assert.IsTrue(service.GetType() == typeof(Google.Apis.Gmail.v1.GmailService));
// Only Credentials Path
service = GmailHelper.GetGmailService(ApplicationName, GmailHelper.TokenPathType.WORKING_DIRECTORY, credentialsPath: credentialsPath);
Assert.IsTrue(service.GetType() == typeof(Google.Apis.Gmail.v1.GmailService));
}

[TestMethod]
[TestCategory("GMAIL-TESTS-DOTNETCORE")]
public void Test_GmailService_Dispose()
Expand Down
4 changes: 2 additions & 2 deletions GmailAPIHelper/GmailAPIHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Solution Version:
<PackageId>GmailHelper</PackageId>
<RepositoryType>git</RepositoryType>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReleaseNotes>1. MimeKitLite dependency update from ('4.8.0' -&gt; '4.9.0').</PackageReleaseNotes>
<Version>1.10.2</Version>
<PackageReleaseNotes>1. GetGmailService() - Additional optional argument 'credentialsPath' to define custom 'credentials.json' file path.</PackageReleaseNotes>
<Version>1.11.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
48 changes: 32 additions & 16 deletions GmailAPIHelper/GmailHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,40 +75,54 @@ public enum MessageListVisibility
}

/// <summary>
/// Sets the credentials path to be used.
/// Sets the token file path to be used.
/// </summary>
/// <param name="tokenPathType">'TokenPathType' enum value. 'HOME' for users home directory, 'WORKING_DIRECTORY' for working directory, 'CUSTOM' for any other custom path to be used.</param>
/// <param name="tokenPath">Token path value in case of 'TokenPathType - CUSTOM' value.</param>
/// <returns>Credentials file path.</returns>
/// <param name="tokenPath">Token file path value in case of 'TokenPathType - CUSTOM' value.</param>
/// <returns>Token file path.</returns>
/// <exception cref="NotImplementedException">Throws - 'NotImplementedException' for OS Platforms other than Windows/Linux/OSX.</exception>
private static string SetCredentialPath(TokenPathType tokenPathType, string tokenPath = "")
private static string SetTokenPath(TokenPathType tokenPathType, string tokenPath = "")
{
string credPath = "";
string filePath = "";
if (tokenPathType == TokenPathType.HOME)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
credPath = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\" + _tokenFile;
filePath = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\" + _tokenFile;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
credPath = Environment.GetEnvironmentVariable("HOME") + "/" + _tokenFile;
filePath = Environment.GetEnvironmentVariable("HOME") + "/" + _tokenFile;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
credPath = Environment.GetEnvironmentVariable("HOME") + "/" + _tokenFile;
filePath = Environment.GetEnvironmentVariable("HOME") + "/" + _tokenFile;
else
throw new NotImplementedException("OS Platform: Not 'Windows/Linux/OSX' Platform.");
}
else if (tokenPathType == TokenPathType.WORKING_DIRECTORY)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
credPath = Environment.CurrentDirectory + "\\" + _tokenFile;
filePath = Environment.CurrentDirectory + "\\" + _tokenFile;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
credPath = Environment.CurrentDirectory + "/" + _tokenFile;
filePath = Environment.CurrentDirectory + "/" + _tokenFile;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
credPath = Environment.CurrentDirectory + "/" + _tokenFile;
filePath = Environment.CurrentDirectory + "/" + _tokenFile;
else
throw new NotImplementedException("OS Platform: Not 'Windows/Linux/OSX' Platform.");
}
else if (tokenPathType == TokenPathType.CUSTOM)
credPath = tokenPath;
return credPath;
filePath = tokenPath;
return filePath;
}

/// <summary>
/// Sets the 'credentials.json' file path to be used.
/// </summary>
/// <param name="credentialsPath">'credentials.json' file path.</param>
/// <returns>'credentials.json' file path.</returns>
private static string SetCredentialsPath(string credentialsPath)
{
string file = "credentials.json";
if (string.IsNullOrEmpty(credentialsPath))
return file;
else
return credentialsPath;
}

/// <summary>
Expand All @@ -119,8 +133,9 @@ private static string SetCredentialPath(TokenPathType tokenPathType, string toke
/// Default value - 'WORKING_DIRECTORY'.</param>
/// <param name="tokenPath">'token.json' path to save generated token from gmail authentication/authorization.
/// Always asks in case of change in gmail authentication or valid token file missing in the given path. Default path is blank, required for 'TokenPathType - CUSTOM'.</param>
/// <param name="credentialsPath">'credentials.json' file path. Default path is blank in which case uses working directory for 'credentials.json' file lookup.
/// <returns>Gmail Service.</returns>
public static GmailService GetGmailService(string applicationName, TokenPathType tokenPathType = TokenPathType.WORKING_DIRECTORY, string tokenPath = "")
public static GmailService GetGmailService(string applicationName, TokenPathType tokenPathType = TokenPathType.WORKING_DIRECTORY, string tokenPath = "", string credentialsPath = "")
{
var scopes = new List<string>
{
Expand All @@ -131,10 +146,11 @@ public static GmailService GetGmailService(string applicationName, TokenPathType
GmailService.Scope.GmailSend
};
UserCredential credential;
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
var credentials = SetCredentialsPath(credentialsPath);
using (var stream = new FileStream(credentials, FileMode.Open, FileAccess.Read))
{
//The file token.json stores the user's access and refresh tokens, and is created automatically when the authorization flow completes for the first time.
string credPath = SetCredentialPath(tokenPathType, tokenPath);
string credPath = SetTokenPath(tokenPathType, tokenPath);
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.FromStream(stream).Secrets,
scopes,
Expand Down

0 comments on commit 4e6a36e

Please sign in to comment.