示例#1
0
        public ContentStackController()
        {
            ContentstackOptions options = new ContentstackOptions()
            {
                ApiKey      = _ApiKey,
                AccessToken = _AccessToken,
                Environment = _Environment,
            };

            _ContentStackClient = new ContentstackClient(options);
        }
        /// <summary>
        /// Initializes a instance of the <see cref="ContentstackClient"/> class.
        /// </summary>
        /// <param name="options"> used to get stack details via class <see cref="ContentstackOptions"/> to create client.</param>
        /// <example>
        /// <code>
        ///     //&quot;blt5d4sample2633b&quot; is a dummy Stack API key
        ///     //&quot;blt6d0240b5sample254090d&quot; is dummy access token.
        ///     var options = new ContentstackOptions()
        ///     {
        ///        ApiKey = &quot;api_key&quot;,
        ///        AccessToken = &quot;access_token&quot;
        ///        Environment = &quot;environment&quot;
        ///      }
        ///     ContentstackClient stack = new ContentstackClient(options);
        ///     ContentType contentType = stack.ContentType(&quot;contentType_name&quot;);
        /// </code>
        /// </example>
        public ContentstackClient(IOptions <ContentstackOptions> options)
        {
            _options           = options.Value;
            this.StackApiKey   = _options.ApiKey;
            this._LocalHeaders = new Dictionary <string, object>();
            this.SetHeader("api_key", _options.ApiKey);
            this.SetHeader("access_token", _options.AccessToken);
            Config cnfig = new Config();

            cnfig.Environment = _options.Environment;
            if (_options.Host != null)
            {
                cnfig.Host = _options.Host;
            }
            if (_options.Version != null)
            {
                cnfig.Version = _options.Version;
            }
            this.SetConfig(cnfig);
        }
示例#3
0
        /// <summary>
        /// Initializes a instance of the <see cref="ContentstackClient"/> class.
        /// </summary>
        /// <param name="options"> used to get stack details via class <see cref="ContentstackOptions"/> to create client.</param>
        /// <example>
        /// <code>
        ///     //&quot;blt5d4sample2633b&quot; is a dummy Stack API key
        ///     //&quot;blt6d0240b5sample254090d&quot; is dummy access token.
        ///     var options = new ContentstackOptions()
        ///     {
        ///        ApiKey = &quot;api_key&quot;,
        ///        DeliveryToken = &quot;delivery_token&quot;
        ///        Environment = &quot;environment&quot;
        ///      }
        ///     ContentstackClient stack = new ContentstackClient(options);
        ///     ContentType contentType = stack.ContentType(&quot;contentType_name&quot;);
        /// </code>
        /// </example>
        public ContentstackClient(IOptions <ContentstackOptions> options)
        {
            _options           = options.Value;
            this.StackApiKey   = _options.ApiKey;
            this._LocalHeaders = new Dictionary <string, object>();
            this.SetHeader("api_key", _options.ApiKey);
            if (_options.AccessToken != null)
            {
                this.SetHeader("access_token", _options.AccessToken);
            }
            else if (_options.DeliveryToken != null)
            {
                this.SetHeader("access_token", _options.DeliveryToken);
            }
            Config cnfig = new Config();

            cnfig.Environment = _options.Environment;
            if (_options.Host != null)
            {
                cnfig.Host = _options.Host;
            }
            if (_options.Version != null)
            {
                cnfig.Version = _options.Version;
            }
            cnfig.Region = _options.Region;
            this.SetConfig(cnfig);

            this.SerializerSettings.DateParseHandling    = DateParseHandling.None;
            this.SerializerSettings.DateFormatHandling   = DateFormatHandling.IsoDateFormat;
            this.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
            this.SerializerSettings.NullValueHandling    = NullValueHandling.Ignore;

            foreach (Type t in CSJsonConverterAttribute.GetCustomAttribute(typeof(CSJsonConverterAttribute)))
            {
                SerializerSettings.Converters.Add((JsonConverter)Activator.CreateInstance(t));
            }
        }
 public ContentstackClient(ContentstackOptions options) :
     this(new OptionsWrapper <ContentstackOptions>(options))
 {
 }
        public async Task <int> OnExecute(CommandLineApplication app, IConsole console)
        {
            var options = new ContentstackOptions
            {
                ApiKey    = ApiKey,
                Authtoken = Authtoken,
                Host      = Host
            };
            var client = new ContentstackClient(options);

            try
            {
                Console.WriteLine($"Fetching Stack details for API Key {ApiKey}");
                stack = await client.GetStack();
            } catch (Exception e)
            {
                Console.WriteLine(e);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Error.WriteLine("There was an error communicating with the Contentstack API: " + e.Message);
                Console.Error.WriteLine("Please verify that your api key and authtoken are correct");
                return(Program.ERROR);
            }

            var path = "";

            if (string.IsNullOrEmpty(Path))
            {
                path = Directory.GetCurrentDirectory();
                Console.WriteLine($"No path specified, creating files in current working directory {path}");
            }
            else
            {
                Console.WriteLine($"Path specified. Files will be created at {Path}");
                path = Path;
            }
            path = $"{path}/Models";
            DirectoryInfo dir = CreateDirectory(path);

            try
            {
                Console.WriteLine($"Fetching Content Types from {stack.Name}");
                var totalCount = await getContentTypes(client, 0);

                var skip = 100;
                Console.WriteLine($"Found {totalCount} Content Types .");

                while (totalCount > skip)
                {
                    Console.WriteLine($"{skip} Content Types Fetched.");
                    totalCount = await getContentTypes(client, skip);

                    skip += 100;
                }
                Console.WriteLine($"Total {totalCount} Content Types fetched.");

                CreateEmbeddedObjectClass(Namespace, dir);

                Console.WriteLine($"Fetching Global Fields from {stack.Name}");
                totalCount = await getGlobalFields(client, 0);

                skip = 100;
                Console.WriteLine($"Found {totalCount} Global Fields.");

                while (totalCount > skip)
                {
                    Console.WriteLine($"{skip} Global Fields Fetched.");
                    totalCount = await getGlobalFields(client, skip);

                    skip += 100;
                }
                Console.WriteLine($"Total {totalCount} Global Fields fetched.");
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Error.WriteLine("There was an error communicating with the Contentstack API: " + e.Message);
                Console.Error.WriteLine("Please verify that your api key and authtoken are correct");
                return(Program.ERROR);
            }
            Console.ResetColor();

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Generating files from content type");
            Console.ResetColor();

            Console.ForegroundColor = ConsoleColor.DarkYellow;
            CreateLinkClass(Namespace, dir);
            CreateHelperClass(Namespace, dir);
            CreateStringHelperClass(Namespace, dir);
            CreateDisplayAttributeClass(Namespace, dir);

            foreach (var contentType in _contentTypes)
            {
                CreateFile(FormatClassName(contentType.Title), Namespace, contentType, dir);
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Files successfully created!");
            Console.ResetColor();
            Console.WriteLine($"Opening {dir.FullName}");
            OpenFolderatPath(dir.FullName);
            return(Program.OK);
        }