Here at , API management is our business, and our business is simple; offer the consumer the most significant amount of APIM value with the least amount of complexity. This includes providing customers with the tooling required to handle 99 percent of your use cases and providing an extensible framework for those that might be unique to your business logic.
Transformations are included with
Take, for example, transformations. Whether you’re using Open Source or Pro, our extensive plugins (including transformations) are provided to you; batteries included- at no extra cost. ’s transformations are powered by Go templates and the sprig library, which offers powerful template functions. Things such as iterators, conditionals and data manipulations are supported natively.
So why would you want to reinvent the wheel with transformations through plugins? You may have core business logic in transformations implemented in various programming languages.
Perhaps your transformations need to leverage machine learning libraries such as NumPy or Tensorflow. Regardless of the reasoning, we’re here to enable your success. can act as a gRPC passthrough or a gRPC client and make outbound calls to your gRPC services.
This guide will take you through configuring a gRPC server which you can leverage to add additional logic to your API requests. Any gRPC-supported language will work as we provide the Protobuf definitions. However, the focus of this guide will be a Python implementation of a gRPC server.
For example, as illustrated in Figure 1, an API consumer can make an API request to a Microservice (or service) managed by . A gRPC plugin is specified in the API definition in -Gateway (client), which contacts the gRPC server running on the host system.There are many stages in the request middleware chain; you can add custom logic through plugins; for example, Figure 1 depicts the logic executing at the “response” stage. If you attach a debugger to the gRPC server, you can intercept the request and debug and develop your plugin.
Figure 1: An API consumer can make an API request to a microservice managed by . During the request lifecycle, the -Gateway acts as a gRPC client that can contact a gRPC server to layer on custom logic.
We have two possibilities for a install; this guide will cover instructions for both open-source and licensed installations.
NOTE: This guide is only tested on OSX (INTEL CPU).
1. open source Instructions:
1.1) Ensure you have the following dependencies on your system:
Docker
Docker-Compose
Python 3.*
This guide assumes you have Docker, Docker-Compose and Python 3 installed on your host system. On OSX systems, you need Docker Desktop. You will additionally need to install the following Python packages:
Ensure that you update the config file .standalone.conf with the values outlined in this documentation link. In summary, the two most important configurations are enable_coprocess and coprocess_grpc_server. Since our gateway will be running within Docker and our gRPC server on the host, we instruct the gateway to reach out to our gRPC server on the host through host.docker.internal..
Reference 1: Sample configuration options for coprocess_options.
Now with the options configured, the gateway can be launched with a simple:`docker-compose up`. You can validate that the gateway has successfully deployed with a curl command:
1.3) Clone repository, Generate Protobuf definitions and deploy gRPC server
Now that we have an open source -Gateway running, it’s time to spin up a gRPC server. We provide a few gRPC servers on the Github repository.
git clone https://github.com/Technologies/.git
Navigate to `/coprocess/proto’ and run “./update_bindings.sh”
This will generate the protobuf bindings. These definitions allow -Gateway (client) to exchange data with your gRPC server seamlessly. Now the server file needs to be updated with the correct import statement, and it can be properly launched.
Edit the following `/coprocess/bindings/python/sample_server.py` and replace it with the sample server code here.
This sample server includes an extra function called MyResponseMiddleware which performs a body transform on the upstream JsonPlaceholder to extract all IDs from a json payload.
Now that this is imported, we can launch the python3 server.
cd .//coprocess/bindings/python
python3 sample_server.py]```
1.4) Create an API with a gRPC plugin enabled
Make a POST request to the gateway’s //apis endpoint to create an API with gRPC enabled. See Reference 2 for a complete example. The reference example generates a new API definition that reverses proxies to jsonplaceholder.typicode.com and has a reference to a gRPC plugin. The listening path of this API definition is /extract-json-values-grpc-server/.
Reference 2: Making a post request to the //apis endpoint to create an API definition regarding a gRPC plugin.
1.5) Test out the endpoint
In Reference 4, you can now make a GET request against your new API, and since it reverse proxies to jsonplaceholder.typicode.com, the /users endpoint will return the ID of your request. Please see Reference 3 for the original untransformed response. Our gRPC server injects custom logic at the “response” stage in the Request Middleware Chain. This custom logic allows us to intercept the response from our upstream and manipulate the data, such as extracting all user IDs into an array for a response. Optionally, follow Step 1.6 to attach a debugger to your gRPC server and intercept the API requests for live debugging.
1.6) Attaching a debugger to the gRPC server
You’ll notice that the process for the gRPC server in Step 1.3 is simply running an instance of sample_server.py from the command line. As this process runs on your host machine, it is possible to attach a debugger. Any IDE with a Python debugger is sufficient. However, in my example, I will be using Pycharm by Jetbrains. Figure 2 illustrates the configuration of a Run/Debug configuration within my IDE, and when selecting the “Debug selected configuration” option illustrated by the red box in Figure 2, sample_server.py will launch with a debugger attached.
Creating a breakpoint in the gRPC server code, as illustrated in Figure 3, and simply making a request to an API endpoint with the gRPC plugin enabled will trigger the IDE to capture the request. As highlighted in Figure 3, the full coprocess_object is available for you to inspect.
Figure 2: Configuration of a Run/Debug Configuration in PyCharm for intercepting API requests sent to the gRPC server.
Figure 3: Setting a breakpoint in the method MyResponseMiddleware, and intercepting an API request.
Reference 3: Expand the accordion above to see a sample GET request to the jsonplaceholder upstream on the /users endpoint. This response returns an array of json objects, each with an id field.
Ensure that you obtain a licence key for the Dashboard from our website and follow the repository’s instructions for how to load the licence key through environment variables.
This repository leverages environment variables instead of configuration files, so you must update the two environment variables in the cons/_analytics.env file to the following values illustrated in Reference 4. Since our gateway will be running within Docker and our gRPC server on the host, we instruct the gateway to reach out to our gRPC server on the host through host.docker.internal.
Reference 5: The two environment variables need to be set to instruct the gateway to use coprocess and where the gRPC server is running.
Now with the options configured, the gateway can be launched with a simple `docker-compose up`. You can validate that the gateway has successfully deployed with a curl command:
curl http://localhost:8080/hello
Navigate to the Dashboard at localhost:3000 and fill in the values to bootstrap your installation.
2.3 Clone repository, generate protobuf definitions and deploy gRPC server
Same as step 1.3, follow the steps outlined in 1.3.
2.4 Create an API with a gRPC plugin enabled
Creating an API through the dashboard APIs or leveraging the GUI is possible. In this example, we will navigate to the APIs tab on the left-hand side and select Import API. You can paste in the API definition found here to generate an API that reverse proxies to httpbin.org and refers to a gRPC plugin located within it.