示例#1
0
        private async Task <bool> DownloadSchemaAsync(InitCommandContext context)
        {
            using var activity = Output.WriteActivity("Download schema");

            try
            {
                HttpClient client = HttpClientFactory.Create(
                    context.Uri, context.Token, context.Scheme);
                DocumentNode schema = await IntrospectionClient.LoadSchemaAsync(client);

                schema = IntrospectionClient.RemoveBuiltInTypes(schema);

                string schemaFilePath = FileSystem.CombinePath(
                    context.Path, context.SchemaFileName);
                await FileSystem.WriteToAsync(schemaFilePath, stream =>
                                              Task.Run(() => SchemaSyntaxSerializer.Serialize(
                                                           schema, stream, true)));

                return(true);
            }
            catch (HttpRequestException ex)
            {
                activity.WriteError(
                    HCErrorBuilder.New()
                    .SetMessage(ex.Message)
                    .SetCode("HTTP_ERROR")
                    .Build());
                return(false);
            }
        }
示例#2
0
        private async Task UpdateSchemaAsync(string path, SchemaFile schemaFile)
        {
            var httpClient = new HttpClient();

            httpClient.BaseAddress = new Uri(schemaFile.Url);

            if (Token != null)
            {
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue(
                        Scheme ?? "bearer", Token);
            }

            var stopwatch = Stopwatch.StartNew();

            Console.WriteLine("Download schema started.");
            DocumentNode schema = await IntrospectionClient.LoadSchemaAsync(httpClient);

            schema = IntrospectionClient.RemoveBuiltInTypes(schema);
            Console.WriteLine(
                "Download schema completed in " +
                $"{stopwatch.ElapsedMilliseconds} ms for {path}.");

            stopwatch.Restart();
            Console.WriteLine("Client configuration started.");

            string fileName = IOPath.Combine(path, schemaFile.Name + ".graphql");

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            using (var stream = File.Create(fileName))
            {
                using (var sw = new StreamWriter(stream))
                {
                    SchemaSyntaxSerializer.Serialize(schema, sw, true);
                }
            }

            Console.WriteLine(
                "Client configuration completed in " +
                $"{stopwatch.ElapsedMilliseconds} ms for {path}.");
        }
示例#3
0
        public async Task <int> OnExecute()
        {
            if (Path is null)
            {
                throw new InvalidOperationException("Path mustn't not be null.");
            }

            if (Url is null)
            {
                throw new InvalidOperationException("Url mustn't not be null.");
            }

            var httpClient = new HttpClient();

            httpClient.BaseAddress = new Uri(Url);
            httpClient.DefaultRequestHeaders.UserAgent.Add(
                new ProductInfoHeaderValue(
                    new ProductHeaderValue(
                        "StrawberryShake",
                        GetType() !.Assembly !.GetName() !.Version !.ToString())));

            if (Token != null)
            {
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue(
                        Scheme ?? "bearer", Token);
            }

            var stopwatch = Stopwatch.StartNew();

            Console.WriteLine("Download schema started.");
            DocumentNode schema = await IntrospectionClient.LoadSchemaAsync(httpClient);

            schema = IntrospectionClient.RemoveBuiltInTypes(schema);
            Console.WriteLine(
                "Download schema completed in " +
                $"{stopwatch.ElapsedMilliseconds} ms.");

            stopwatch.Restart();
            Console.WriteLine("Client configuration started.");

            if (!Directory.Exists(Path))
            {
                Directory.CreateDirectory(Path);
            }

            SchemaName = (SchemaName ?? "schema").Trim();
            string schemaFielName = SchemaName + ".graphql";

            var configuration = new Configuration();

            configuration.ClientName = SchemaName + "Client";
            configuration.Schemas    = new List <SchemaFile>();
            configuration.Schemas.Add(new SchemaFile
            {
                Type = "http",
                Name = SchemaName,
                File = schemaFielName,
                Url  = Url
            });

            using (var stream = File.Create(IOPath.Combine(Path, WellKnownFiles.Config)))
            {
                await JsonSerializer.SerializeAsync(stream, configuration);
            }

            using (var stream = File.Create(IOPath.Combine(Path, schemaFielName)))
            {
                using (var sw = new StreamWriter(stream))
                {
                    SchemaSyntaxSerializer.Serialize(schema, sw, true);
                }
            }

            Console.WriteLine(
                "Client configuration completed in " +
                $"{stopwatch.ElapsedMilliseconds} ms for {Path}.");
            return(0);
        }
示例#4
0
        public async Task <int> OnExecute()
        {
            var httpClient = new HttpClient();

            httpClient.BaseAddress = new Uri(Url);

            if (Token != null)
            {
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue(
                        Scheme ?? "bearer", Token);
            }

            var stopwatch = Stopwatch.StartNew();

            Console.WriteLine("Download schema started.");
            DocumentNode schema = await IntrospectionClient.LoadSchemaAsync(httpClient);

            schema = IntrospectionClient.RemoveBuiltInTypes(schema);
            Console.WriteLine(
                "Download schema completed in " +
                $"{stopwatch.ElapsedMilliseconds} ms.");

            stopwatch.Restart();
            Console.WriteLine("Client configuration started.");

            if (!Directory.Exists(Path))
            {
                Directory.CreateDirectory(Path);
            }

            SchemaName = SchemaName ?? "schema";
            string schemaFielName = SchemaName + ".graphql";

            var configuration = new Configuration();

            configuration.ClientName = SchemaName + "Client";
            configuration.Schemas    = new List <SchemaFile>();
            configuration.Schemas.Add(new SchemaFile
            {
                Type = "http",
                Name = SchemaName,
                File = schemaFielName,
                Url  = Url
            });

            using (var stream = File.Create(IOPath.Combine(Path, "config.json")))
            {
                await JsonSerializer.SerializeAsync(stream, configuration);
            }

            using (var stream = File.Create(IOPath.Combine(Path, schemaFielName)))
            {
                using (var sw = new StreamWriter(stream))
                {
                    SchemaSyntaxSerializer.Serialize(schema, sw, true);
                }
            }

            Console.WriteLine(
                "Client configuration completed in " +
                $"{stopwatch.ElapsedMilliseconds} ms for {Path}.");
            return(0);
        }