Eric Fredericksen Wed May 15

FastAPI: The Ultimate Guide for Ignition Offboard-Services

Many Ignition programmers face the challenge of developing efficient web applications. That’s where FastAPI comes to the rescue. It’s a high-performance framework for building APIs with Python 3.6 and above. FastAPI promotes the creation of clean, scalable, and efficient web applications for use in Ignition Offboard-Services.

But First, What is FastAPI?

FastAPI is at the forefront of synchronous web frameworks and provides a comprehensive solution for modern web development needs. It’s not only light and fast, it comes with automatic and interactive documentation, data validation, and security features.

How Does FastAPI Work?

 With FastAPI, you define the endpoints of your remote APIs and link to the logic that implements the endpoints using routes. The routes contain the data models to receive and deliver the data.

FastAPI Feature We Love: ASGI

FastAPI is built on top of an ASGI (Asynchronous Server Gateway Interface) called Starlette (Starlette Service). FastAPI works with any web server with ASGI support such as uvicorn.

 ASGI is the standard for Python and allows FastAPI to handle concurrent requests with ease, making it significantly faster than synchronous frameworks. This is particularly useful for IO-bound and high-performance applications.

FastAPI Feature We Love: PyDantic Data Validation

PyDantic is a validation model used by Python and FastAPI. Data validation is critical in any web application. FastAPI uses PyDantic to validate and serialize data. Data is automatically type-checked to and from JSON ensuring a robust and secure application.

Slide about Pydantic Data Validation

How it Works: 

PyDantic data validation enforces type hints at runtime. This means that when we’re structuring the data model, I can tell the API endpoint what data types it's going to expect to receive.

This could be simple like strings, lists, dictionaries, but for best practice, you’d want to use the PyDantic data model that uses base model superclass to establish data models that would then be picked up by the automated documentation system.

This gives you data validation and automatic JSON encoding and decoding processes.

FastAPI Feature We Love: Automatic Documentation

I personally believe that the automatic documentation is one of the best features.

It’s no surprise that documentation is often a chore that people don't want to do, yet it's crucial for API usability and maintenance. The documentation is real-time using the OpenAI standards; this simplifies the developer experience and fosters better collaboration.

Slide about Automatic Documentation

How it Works:

The documentation shows you what the payloads should look like from the API’s perspective, and then what the response (output) object will look like. So as someone developing something to work with this API, I would know how to format my payload and what the response will look like before I ever have to write a line of code. This fosters efficiency.

Why We Love It:

If you’re working on a web development project or something working from Ignition Gateway server, you want to be able to publish that API as quickly as possible and establish your data models. So that the people working on the other parts of the system have significant documentation to work against as they’re developing their parts of the software.

FastAPI Feature We Love: Dependency Injection

Another significant feature of FastAPI is Dependency Injection. 

Dependency injection means that, in programming, there is a way for your code (in this case, your path operations functions) to declare things that it requires to work as dependencies. These dependencies, which can be nested, are resolved before calling the path operation function.

This ensures that the dependencies happen first and gives you an API route that is independent of the actual authorization or authentication model you are using.

Slide about Dependency Injection

How it Works: 

You would just add a dependency flag, tell it what dependencies are expected to happen before that API call succeeds. 

Why We Love it:

Dependency injection follows a core principle of clean architecture that is the “separation of concerns.” The FastAPI dependency injection system allows for more modular and testable code by providing a way to declare and manage dependencies separately from the business logic.

This means that as you're developing the business logic, you can create a separate part of the software that does all the other things–background tasks, session and cookies support, middleware, cores, authentication–and handles them seamlessly and quickly within FastAPI.

FastAPI Feature We Love: Performance

In some web applications, some background tasks can be deferred until after a response has been sent to the client. FastAPI supports these operations natively, enabling more efficient resource applications and smoother user experiences.

Slide about Async nature performance

How it Works: 

You can have an API endpoint that will return a response immediately and then in the background, run some long-running IO-intensive process that reports back to the caller once it’s finished.

Why We Love it:

That way you don’t have a protracted length of time between the API call and the response. Making the front-end application work more fluidly and seamlessly for the user experience.

FastAPI Feature We Love: Security

When we talk about security with FastAPI, it’s important to reiterate that security is paramount.

We work with clients and develop solutions with clients, and we pick frameworks that serve a number of different security models. 

Slide about OpenAPI security methods

Thankfully, FastAPI provides several tools and mechanisms to secure the application, including:

  • OAuth 2
  • API Key
  • Http
  • openidConnect

This flexibility ensures the API integrity and meets the company security needs, including supporting off-the-shelf security models like Okta.

Why Use Offboard-Services

Taking code out of the Ignition system needs to be done mindfully. But for the right use cases, it is beneficial in three primary ways:

Code Reuse

With Offboard-Services, we can leverage Python 3 ecosystems and third-party libraries.

Code Repository Friendly

We do have the ability to save Ignition code in a repository, but Offboard-Services is a more straightforward approach.

The Python 3 Ecosystem

There are millions of Python 3 developers and it’s easy to solve complex problems when you have that much knowledge to draw from.

When to Use Offboard-Services with Ignition

We don’t want to replace –nor could we–replace everything in Ignition with an API. But there are situations where it’s low-hanging fruit to use a library that was built to perform a specific task.

Slide on why and when to use Ignition offboard services

Here are some reasons to use Offboard-Services:

Avoid complex algorithm development inside Ignition

If something is already out there that does what you want to do, but you can’t access it inside Ignition, then that’s a good reason to build an API to do some of that work.

If there’s nothing out there in Python that solves your problem, it might not be a good reason to do it. But there’s many opportunities and lots of stuff out there.

For example, I once spent three hours and 20 lines of code to solve a problem that would have cost a lot more.

Performance Enhancements

If you’re running into performance problems in ignition, Off-boarding some of that computational weight is really the answer. You have the ability to leverage more threads and do background tasks for long-running things that may cause problems in Ignition where you might have to end them.

Ignition Web Development Streamlined with FastAPI

In conclusion, FastAPI emerges as a game-changer for Ignition programmers who need performance and scalability in their web applications. Its high-performance framework, coupled with seamless integration with Python 3.6 and above, offers a robust solution for Ignition Offboard-Services. FastAPI empowers developers with the tools needed to streamline development processes and enhance overall application performance. By leveraging Offboard-Services, developers can tap into the vast Python 3 ecosystem, promoting code reuse, repository-friendly practices, and access to a wealth of third-party libraries.

Whether optimizing performance or enhancing security, FastAPI proves indispensable in navigating the evolving landscape of web development within the Ignition ecosystem.

avatar

Eric Fredericksen

Senior Industrial Programmer

COMMENTS

RELATED ARTICLES

SUBSCRIBE TO OUR BLOG

Sign up to get the latest from Vertech delivered right to your inbox.