Example #1
0
        /// <summary>
        ///
        /// <para>BGoogleSlidesService: Parametered Constructor</para>
        ///
        /// <para>Parameters:</para>
        /// <para><paramref name="_ProgramUniqueID"/>           Program Unique ID</para>
        /// <para><paramref name="_ProjectID"/>                 GC Project ID</para>
        /// <para><paramref name="_ErrorMessageAction"/>        Error messages will be pushed to this action</para>
        ///
        /// </summary>
        public BGoogleSlidesService(
            string _ProgramUniqueID,
            string _ProjectID,
            Action <string> _ErrorMessageAction = null)
        {
            ProgramUniqueID = _ProgramUniqueID;
            ProjectID       = _ProjectID;
            try
            {
                string ApplicationCredentials      = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
                string ApplicationCredentialsPlain = Environment.GetEnvironmentVariable("GOOGLE_PLAIN_CREDENTIALS");

                if (ApplicationCredentials == null && ApplicationCredentialsPlain == null)
                {
                    _ErrorMessageAction?.Invoke("BGoogleSlidesService->Constructor: GOOGLE_APPLICATION_CREDENTIALS (or GOOGLE_PLAIN_CREDENTIALS) environment variable is not defined.");
                    bInitializationSucceed = false;
                }
                else
                {
                    if (ApplicationCredentials == null)
                    {
                        if (!BUtility.HexDecode(out ApplicationCredentialsPlain, ApplicationCredentialsPlain, _ErrorMessageAction))
                        {
                            throw new Exception("Hex decode operation for application credentials plain has failed.");
                        }
                        Credential = GoogleCredential.FromJson(ApplicationCredentialsPlain)
                                     .CreateScoped(
                            new string[]
                        {
                            SlidesService.Scope.PresentationsReadonly
                        })
                                     .UnderlyingCredential as ServiceAccountCredential;
                    }
                    else
                    {
                        using (var Stream = new FileStream(ApplicationCredentials, FileMode.Open, FileAccess.Read))
                        {
                            Credential = GoogleCredential.FromStream(Stream)
                                         .CreateScoped(
                                new string[]
                            {
                                SlidesService.Scope.PresentationsReadonly
                            })
                                         .UnderlyingCredential as ServiceAccountCredential;
                        }
                    }

                    if (Credential != null)
                    {
                        bInitializationSucceed = true;
                    }
                    else
                    {
                        bInitializationSucceed = false;
                    }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BGoogleSlidesService->Constructor: " + e.Message + ", Trace: " + e.StackTrace);
                bInitializationSucceed = false;
            }
        }
Example #2
0
        /// <summary>
        ///
        /// <para>BTracingServiceGC: Parametered Constructor for Managed Service by Google</para>
        ///
        /// <para>Parameters:</para>
        /// <para><paramref name="_ProjectID"/>              GC Project ID</para>
        /// <para><paramref name="_ProgramUniqueID"/>        Program Unique ID</para>
        /// <para><paramref name="_ErrorMessageAction"/>     Error messages will be pushed to this action</para>
        ///
        /// </summary>
        public BTracingServiceGC(
            string _ProjectID,
            string _ProgramUniqueID,
            Action <string> _ErrorMessageAction = null)
        {
            ProgramUniqueID    = _ProgramUniqueID;
            ErrorMessageAction = _ErrorMessageAction;

            try
            {
                string ApplicationCredentials      = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
                string ApplicationCredentialsPlain = Environment.GetEnvironmentVariable("GOOGLE_PLAIN_CREDENTIALS");
                if (ApplicationCredentials == null && ApplicationCredentialsPlain == null)
                {
                    _ErrorMessageAction?.Invoke("BTracingServiceGC->Constructor: GOOGLE_APPLICATION_CREDENTIALS (or GOOGLE_PLAIN_CREDENTIALS) environment variable is not defined.");
                    bInitializationSucceed = false;
                }
                else
                {
                    var Scopes = new List <string>();
                    foreach (var Scope in TraceServiceClient.DefaultScopes)
                    {
                        if (!Scopes.Contains(Scope))
                        {
                            Scopes.Add(Scope);
                        }
                    }

                    if (ApplicationCredentials == null)
                    {
                        if (!BUtility.HexDecode(out ApplicationCredentialsPlain, ApplicationCredentialsPlain, _ErrorMessageAction))
                        {
                            throw new Exception("Hex decode operation for application credentials plain has failed.");
                        }
                        Credential = GoogleCredential.FromJson(ApplicationCredentialsPlain)
                                     .CreateScoped(
                            Scopes.ToArray())
                                     .UnderlyingCredential as ServiceAccountCredential;
                    }
                    else
                    {
                        using (var Stream = new FileStream(ApplicationCredentials, FileMode.Open, FileAccess.Read))
                        {
                            Credential = GoogleCredential.FromStream(Stream)
                                         .CreateScoped(
                                Scopes.ToArray())
                                         .UnderlyingCredential as ServiceAccountCredential;
                        }
                    }

                    if (Credential != null)
                    {
                        Channel = new Grpc.Core.Channel(
                            TraceServiceClient.DefaultEndpoint.ToString(),
                            Credential.ToChannelCredentials());
                    }

                    if (Channel != null)
                    {
                        TraceClient            = TraceServiceClient.Create(Channel);
                        ProjectName            = new Google.Api.Gax.ResourceNames.ProjectName(_ProjectID);
                        bInitializationSucceed = TraceClient != null;

                        UploadTimer           = new Timer(1_000);
                        UploadTimer.Elapsed  += OnTimedEvent;
                        UploadTimer.AutoReset = true;
                        UploadTimer.Enabled   = true;
                    }
                    else
                    {
                        bInitializationSucceed = false;
                    }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BTracingServiceGC->Constructor: " + e.Message + ", Trace: " + e.StackTrace);
                bInitializationSucceed = false;
            }
        }
Example #3
0
        /// <summary>
        ///
        /// <para>BPubSubServiceGC: Parametered Constructor for Managed Service by Google</para>
        ///
        /// <para>Parameters:</para>
        /// <para><paramref name="_ProjectID"/>              GC Project ID</para>
        /// <para><paramref name="_ErrorMessageAction"/>     Error messages will be pushed to this action</para>
        ///
        /// </summary>
        public BPubSubServiceGC(
            string _ProjectID,
            Action <string> _ErrorMessageAction = null)
        {
            ProjectID = _ProjectID;
            try
            {
                string ApplicationCredentials      = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
                string ApplicationCredentialsPlain = Environment.GetEnvironmentVariable("GOOGLE_PLAIN_CREDENTIALS");
                if (ApplicationCredentials == null && ApplicationCredentialsPlain == null)
                {
                    _ErrorMessageAction?.Invoke("BPubSubServiceGC->Constructor: GOOGLE_APPLICATION_CREDENTIALS (or GOOGLE_PLAIN_CREDENTIALS) environment variable is not defined.");
                    bInitializationSucceed = false;
                }
                else
                {
                    var PublishScopes   = new List <string>();
                    var SubscribeScopes = new List <string>();
                    foreach (var Scope in PublisherServiceApiClient.DefaultScopes)
                    {
                        if (!PublishScopes.Contains(Scope))
                        {
                            PublishScopes.Add(Scope);
                        }
                    }
                    foreach (var Scope in SubscriberServiceApiClient.DefaultScopes)
                    {
                        if (!SubscribeScopes.Contains(Scope))
                        {
                            SubscribeScopes.Add(Scope);
                        }
                    }

                    if (ApplicationCredentials == null)
                    {
                        if (!BUtility.HexDecode(out ApplicationCredentialsPlain, ApplicationCredentialsPlain, _ErrorMessageAction))
                        {
                            throw new Exception("Hex decode operation for application credentials plain has failed.");
                        }
                        PublishCredential = GoogleCredential.FromJson(ApplicationCredentialsPlain)
                                            .CreateScoped(
                            PublishScopes.ToArray())
                                            .UnderlyingCredential as ServiceAccountCredential;
                        SubscribeCredential = GoogleCredential.FromJson(ApplicationCredentialsPlain)
                                              .CreateScoped(
                            SubscribeScopes.ToArray())
                                              .UnderlyingCredential as ServiceAccountCredential;
                    }
                    else
                    {
                        using (var Stream = new FileStream(ApplicationCredentials, FileMode.Open, FileAccess.Read))
                        {
                            PublishCredential = GoogleCredential.FromStream(Stream)
                                                .CreateScoped(
                                PublishScopes.ToArray())
                                                .UnderlyingCredential as ServiceAccountCredential;
                            SubscribeCredential = GoogleCredential.FromStream(Stream)
                                                  .CreateScoped(
                                SubscribeScopes.ToArray())
                                                  .UnderlyingCredential as ServiceAccountCredential;
                        }
                    }

                    if (PublishCredential != null && SubscribeCredential != null)
                    {
                        PublishChannel = new Channel(
                            PublisherServiceApiClient.DefaultEndpoint.ToString(),
                            PublishCredential.ToChannelCredentials());

                        SubscribeChannel = new Channel(
                            SubscriberServiceApiClient.DefaultEndpoint.ToString(),
                            SubscribeCredential.ToChannelCredentials());

                        bInitializationSucceed = true;
                    }
                    else
                    {
                        bInitializationSucceed = false;
                    }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BPubSubServiceGC->Constructor: " + e.Message + ", Trace: " + e.StackTrace);
                bInitializationSucceed = false;
            }
        }