Lessons Learned: Using a Headless CMS without an SDK
Tags: python, kenticokontent, headless, caas
Published: 2019-11-21 14:12:11

How complicated is it for a beginner to implement a headless CMS powered site without an SDK? I chose Python and Flask to build a section of a sample site. Did I succeed? And what did I learn in the process?

<h2>Why Python and Flask?</h2>

Earlier this year, I found myself spending a lot of time manually grinding through administrative tasks that felt repetitive, time consuming, and tedious. I began to search for the best way for a non-developer to start automating tasks. After a few days of browsing, I noticed that I always seemed to end up on a blog, forum, or community post talking about Python. I picked up a few books, took an online introductory course, and started writing scripts to help me with my work.

After completing some basic tutorials, I wondered if I could somehow connect these newly learned skills to the Content-as-a-Service product that my team supports, <a href="https://kontent.ai/">Kentico Kontent</a>. Furthermore, could I do it without an SDK since Kontent does not currently have one for Python? To test this, I decided to recreate the articles section of the <a href="https://docs.kontent.ai/tutorials/develop-apps/get-started/running-a-sample-application#a-accessing-the-sample-project">Dancing Goat sample project</a> that comes with new Kontent subscriptions and catalog some of the lessons I learned from the project.

<h2>Learn the Terminology</h2>

If you've decided to use Kentico Kontent for your project, reviewing and bookmarking the <a href="https://docs.kontent.ai/tutorials/assorted-items/dive-deeper-into-kentico-kontent/terminology">terminology</a> we use throughout the documentation and product UI is an important step.  If you're diving into a project and haven't established a firm grasp on the differences between content types, content items, and components, then the Kentico Kontent documentation won’t deliver its true value.

<b>Lesson learned: taking the time to “learn the lingo” makes working in the Kentico Kontent ecosystem easier.</b>

<h2>Establish Your Project Scope</h2>

Scope creep has always been an issue of mine, and I don't think I'm alone.  When I began this project, I naively wanted to recreate the Dancing Goat site in its entirety while leveraging all of Kentico Kontent’s features. This wasn't an effective plan.
Although no Kentico Kontent features are too difficult to implement, starting small and focusing just on the home page and articles in the sample site gave me the best opportunity to learn.  I could see the API in action, learn about the Flask microframework, and understand how content modeling was done in the sample project.

<b>Lesson learned: starting small and then scaling up prevents the project from becoming overwhelming and provides the opportunity to learn the Kentico Kontent basics.</b>

<h2>Structure Your Content</h2>

Knowing how the content types and content items connected to each other in my sample project saved me a significant amount of time. Since mapping out the home page and article relationships was so useful in a pre-generated project, I know it will be even more beneficial for a new project.
I've seen a fair share of real customer projects that have redundant entries because they didn't consider the relationships between their content prior to development.  This section on <a href="https://docs.kontent.ai/tutorials/set-up-projects/define-content-models/content-modeling-101">content modeling</a> from the Kentico Kontent documentation will likely be helpful in avoiding this pitfall.

<b>Lesson learned: prevent duplicative content by understanding what is reusable and which content is related.</b>

<h2>Create Sample/Starter Content</h2>

I remember feeling a bit overwhelmed by the size of the Delivery API responses from my project. This is because I was using the sample site which has a lot of content items and linked items.  The sample site is a great reference tool, but may be more complex than most projects need to be.
Building out a minimal set of content types and items related to the core functionality in your project can save time when developing your front-end application. Creating only a few content items in the project will make reading and using the JSON responses provided by the Delivery API more manageable.  You'll also have an easier time remembering the codenames of your content items, which you will be using a lot during testing.  Keep it simple, then scale up.

<b>Lesson learned: only build a few content items of each type during the early phases of the project to make analyzing JSON responses more straightforward.</b>

<h2>Identify the Necessary API Requests</h2>

I started using the <a href="https://docs.kontent.ai/tutorials/develop-apps/get-started/using-the-apis-with-our-postman-collection">POSTman</a> REST client to test and document the GET requests I needed to make in my project.  This was immensely helpful since I needed to construct requests in my front-end application using a Python library. It can also be helpful in other scenarios since:

1. You'll familiarize yourself with the <a href="https://developer.kenticocloud.com/reference#delivery-api">Delivery API’s response structure</a>.
2. You'll be more aware of the API's <a href="https://developer.kenticocloud.com/reference#content-filtering">filtering capabilities</a>.
3. You can more easily determine if there is an issue with your code, the content, or our API endpoint during debugging.  By running tests in a REST client, you separate your code from our product.

<b>Lesson learned: understanding the API calls in advance provides a helpful roadmap for building a front-end application and helps during debugging.</b>

<h2>Decide If You Are Going to Use an SDK</h2>

I knew there wasn't a Python SDK for Kentico Kontent. I needed to find documentation on how to call API endpoints using Python and Flask. This may not be your situation, but it would be a shame if halfway through a JavaScript, .Net, PHP, Ruby, or Java project, you or your developers stumbled upon an SDK that would have made your lives a lot easier.

For projects that will use an SDK, it is important to review the <a href="https://docs.kontent.ai/tutorials/develop-apps/get-started/getting-development-resources">SDK documentation</a>, stay up to date on current GitHub issue submissions, and check release notes for the chosen SDK. The SDK GitHub repositories and their accompanying documentation are updated frequently, including breaking change documentation when a new version of an SDK is released.

<b>Lesson learned: collect and review relevant documentation <i>before</i> starting your project, and watch the GitHub repositories that you are using for your project.</b>

<h2>Understand the Conventions in Your Selected Framework/Programming Language</h2>

Let's be honest, I started hacking away trying to get my front-end application to work. The approach should have been to explore Flask and Python conventions first.  Understanding what options I had for structuring my application, determining routes, error handling capabilities, and configuration file conventions should have come sooner rather than later in the project.  

I'm sure Flask and Python purists could have a field day critiquing my sample project, but when I started applying some conventions, my application started to become more manageable.  If I had known this, I could have saved a lot of time!

<b>Lesson learned: learning about framework conventions prior to development makes the project easier to navigate and manage.</b>

<h2>Formulate a Resolver Strategy (for Types and Inline Items)</h2>

Early on, it was clear that not using <a href="https://docs.kontent.ai/tutorials/develop-apps/get-content/using-strongly-typed-models">strongly typed models</a> made my project very messy and hard to read. Once I implemented my own <a href="https://github.com/kentico-michaelb/cloud-sample-app-flask/blob/master/DancingGoat/helpers/type.py">type helper</a>, similar to the <a href="https://github.com/Kentico/cloud-sample-app-net/blob/master/DancingGoat/Models/ContentTypes/CustomTypeProvider.cs">CustomTypeProvider</a> in the Kentico Kontent .Net sample site, I was able to improve the readability of my code.  To help with strongly typing models, Kentico created model generators for the <a href="https://github.com/Kentico/cloud-generators-net">.Net</a>, <a href="https://github.com/Kentico/kontent-model-generator-js">JavaScript</a>, and <a href="https://github.com/Kentico/cloud-generators-java">Java</a> SDKs.  This didn't help me with my Python project, but it is good to know that these tools are available for projects that use the SDKs.

In addition to strongly typing models,  you'll want to resolve linked items, components, and text links that appear in Rich Text elements. In my project, I had to create a <a href="https://github.com/kentico-michaelb/cloud-sample-app-flask/blob/master/DancingGoat/resolvers/inline_resolver.py">custom resolver</a>. This resolver scrapes the content in my Rich Text elements for object and anchor tags and replaces the references with actual links or content. This seems confusing at first, but after spending some time looking at the JSON returned by the API for individual content items, you'll see that Rich Text elements come with accompanying “links” and “modular_content” values that can be used for inline link and item resolution.

Luckily, the Kentico Kontent documentation has a section on <a href="https://docs.kontent.ai/tutorials/develop-apps/get-content/dealing-with-structure-in-rich-text">dealing with structure in rich text</a>, and there are built-in methods in the Kentico Kontent SDKs that handle this resolution for the most part. If you decide to use an SDK, a resolver strategy will be more straightforward.  Otherwise, spend some time looking at the structure of the API's JSON responses for linked items and rich text.

<b>Lessons learned: plan on implementing strongly typed models from the beginning of the project, and review how inline content items appear in the Delivery API responses.</b>

<h2>Start Building</h2>

I hope that after reading about the steps that helped me with my project, you feel more prepared to take on a new Kontent project of your own. As a Python and programming novice, I was able to get a Kentico Kontent blog up and running without an SDK, so I am sure you will create some killer apps that enable a flawless content strategy. You can check out my project <a href="https://github.com/kentico-michaelb/cloud-sample-app-flask">here</a> on GitHub, and feel free to leave comments and suggestions.
If Vue.js is more interesting or relevant to you, my colleague Chris Jennings also wrote an excellent <a href="https://kontent.ai/blog/lessons-learned-from-building-a-blog-on-kentico-cloud">"Lessons learned" blog post</a> that may be helpful.