// [END fs_count_indexes]

        // Creates an index for a given collection in a project.
        // [START fs_create_index]
        private static async Task CreateIndex(string project, string collectionId, string field1, string order1, string field2, string order2)
        {
            GoogleCredential credential =
                GoogleCredential.GetApplicationDefault();

            // Inject the Cloud Platform scope if required.
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[]
                {
                    "https://www.googleapis.com/auth/cloud-platform"
                });
            }
            HttpClient http = new Google.Apis.Http.HttpClientFactory()
                              .CreateHttpClient(
                new Google.Apis.Http.CreateHttpClientArgs()
            {
                ApplicationName = "Google Cloud Platform Firestore Sample",
                GZipEnabled     = true,
                Initializers    = { credential },
            });

            IndexContent indexContent = new IndexContent(collectionId, field1, order1, field2, order2);
            string       jsonRequest  = JsonConvert.SerializeObject(indexContent);
            string       uriString    = "https://firestore.googleapis.com/v1beta1/projects/" + project + "/databases/(default)/indexes";
            UriBuilder   uri          = new UriBuilder(uriString);
            await http.PostAsync(uri.Uri, new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json"));
        }
        // [END auth_api_explicit]

        // [START auth_http_implicit]
        static object AuthHttpImplicit(string projectId)
        {
            GoogleCredential credential =
                GoogleCredential.GetApplicationDefaultAsync().Result;
            // Inject the Cloud Storage scope if required.
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[]
                {
                    "https://www.googleapis.com/auth/devstorage.read_only"
                });
            }
            HttpClient http = new Google.Apis.Http.HttpClientFactory()
                .CreateHttpClient(
                new Google.Apis.Http.CreateHttpClientArgs()
                {
                    ApplicationName = "Google Cloud Platform Auth Sample",
                    GZipEnabled = true,
                    Initializers = { credential },
                });
            UriBuilder uri = new UriBuilder(
                "https://www.googleapis.com/storage/v1/b");
            uri.Query = "project=" +
                System.Web.HttpUtility.UrlEncode(projectId);
            var resultText = http.GetAsync(uri.Uri).Result.Content
                .ReadAsStringAsync().Result;
            dynamic result = Newtonsoft.Json.JsonConvert
                .DeserializeObject(resultText);
            foreach (var bucket in result.items)
            {
                Console.WriteLine(bucket.name);
            }
            return null;
        }
        public Shouter(LogWriter logWriter = null)
        {
            init           = new Initializer();
            init.LogWriter = logWriter;
            init.Random    = new Random();
            var authInit = new Auth.Initializer();

            authInit.LogWriter = logWriter;
            var credentials = Auth.GetCredentials(authInit);

            init.PubsubService = new PubsubService(new BaseClientService.Initializer()
            {
                ApplicationName       = Constants.UserAgent,
                HttpClientInitializer = credentials,
            });
            var args = new Google.Apis.Http.CreateHttpClientArgs
            {
                ApplicationName = Constants.UserAgent,
                GZipEnabled     = true,
            };

            args.Initializers.Add(credentials);
            var factory = new Google.Apis.Http.HttpClientFactory();

            init.HttpClient = factory.CreateHttpClient(args);
        }
Beispiel #4
0
        // [END fs_create_index]

        // Retrieves all indexes for a given collection in a project and deletes them.
        // [START fs_delete_indexes]
        private static async Task DeleteIndexes(string project, string collectionId)
        {
            GoogleCredential credential =
                GoogleCredential.GetApplicationDefault();

            // Inject the Cloud Platform scope if required.
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[]
                {
                    "https://www.googleapis.com/auth/cloud-platform"
                });
            }
            HttpClient http = new Google.Apis.Http.HttpClientFactory()
                              .CreateHttpClient(
                new Google.Apis.Http.CreateHttpClientArgs()
            {
                ApplicationName = "Google Cloud Platform Firestore Sample",
                GZipEnabled     = true,
                Initializers    = { credential },
            });

            string     uriString  = "https://firestore.googleapis.com/v1beta1/projects/" + project + "/databases/(default)/indexes";
            UriBuilder uri        = new UriBuilder(uriString);
            var        resultText = http.GetAsync(uri.Uri).Result.Content
                                    .ReadAsStringAsync().Result;
            dynamic result = Newtonsoft.Json.JsonConvert
                             .DeserializeObject(resultText);

            List <string> indexesToBeDeleted = new List <string>();

            if (result.indexes != null)
            {
                foreach (var index in result.indexes)
                {
                    if (index.collectionId == collectionId)
                    {
                        string name = index.name;
                        indexesToBeDeleted.Add(name);
                    }
                }
            }

            foreach (string indexToBeDeleted in indexesToBeDeleted)
            {
                uriString = "https://firestore.googleapis.com/v1beta1/" + indexToBeDeleted;
                UriBuilder deleteUri = new UriBuilder(uriString);
                await http.DeleteAsync(deleteUri.Uri);
            }
            Console.WriteLine("Index deletion completed!");
        }
Beispiel #5
0
        // Counts the number of created indexes for a given collection in a project.
        // [START fs_count_indexes]
        private static int CountIndexes(string project, string collectionId)
        {
            GoogleCredential credential =
                GoogleCredential.GetApplicationDefault();

            // Inject the Cloud Platform scope if required.
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[]
                {
                    "https://www.googleapis.com/auth/cloud-platform"
                });
            }
            HttpClient http = new Google.Apis.Http.HttpClientFactory()
                              .CreateHttpClient(
                new Google.Apis.Http.CreateHttpClientArgs()
            {
                ApplicationName = "Google Cloud Platform Firestore Sample",
                GZipEnabled     = true,
                Initializers    = { credential },
            });
            string     uriString  = "https://firestore.googleapis.com/v1beta1/projects/" + project + "/databases/(default)/indexes";
            UriBuilder uri        = new UriBuilder(uriString);
            var        resultText = http.GetAsync(uri.Uri).Result.Content
                                    .ReadAsStringAsync().Result;
            dynamic result = Newtonsoft.Json.JsonConvert
                             .DeserializeObject(resultText);

            int numIndexesCreated = 0;

            if (result.indexes != null)
            {
                foreach (var index in result.indexes)
                {
                    Console.WriteLine(index.name);
                    if (index.collectionId == collectionId & index.state == "READY")
                    {
                        numIndexesCreated = numIndexesCreated + 1;
                    }
                }
            }
            return(numIndexesCreated);
        }
 public Shouter(LogWriter logWriter = null)
 {
     init = new Initializer();
     init.LogWriter = logWriter;
     init.Random = new Random();
     var authInit = new Auth.Initializer();
     authInit.LogWriter = logWriter;
     var credentials = Auth.GetCredentials(authInit);
     init.PubsubService = new PubsubService(new BaseClientService.Initializer()
     {
         ApplicationName = Constants.UserAgent,
         HttpClientInitializer = credentials,
     });
     var args = new Google.Apis.Http.CreateHttpClientArgs
     {
         ApplicationName = Constants.UserAgent,
         GZipEnabled = true,
     };
     args.Initializers.Add(credentials);
     var factory = new Google.Apis.Http.HttpClientFactory();
     init.HttpClient = factory.CreateHttpClient(args);
 }