Today so many companies uses gRPC as standard for API, but this is not an article about how this technology can improves performances or how you can implement it: I’m going to talk about how to expose REST API, building only gRPC API in your server.
Using gRPC in my work I discovered how difficult it is to maintain both gRPC API and REST API, in fact there is more effort to update both APIs or it is very difficult to debug them.
We find a very good solution using Google Apigee.
Apigee is a Full Life Cycle API Management of Google and it is named leader in 2021 Gartner Magic Quadrant, in front of MuleSoft and Kong.
With Google Apigee you can:
Apigee is a powerful tool but basically he exposes REST API for you and redirect the traffic in your backend and, then, serves your response to client. If you want to simplify the concept we can say that is a “API proxy”, however you can perform a lot of transformation or build very complex flow. For example you can perform an API Key authentication or collect insights and data of usage, even use others Google products like Firestore or Dialogflow.
First of all we need a project in Google Cloud Platform* where we have to enable these API:
Now we can finally start our Apigee Evaluation Sandbox and follow the wizard to set up the environment.
*If you don’t have any GCP project, don’t worry: you can create it with 300$ of free credits
The first thing we need is a server so, in our Apigee project, we have to configure a it: Admin -> Environments -> Target Servers.
Here we have to select gRPC :
Remember the name, because we will use soon. Then, using an openapi.yaml file, we can create an API Proxy. Select “Use OpenAPI Spec” in Reverse proxy:
Now we can configure gRPC mapping for each REST API: open your API Proxy and select “Develop” tab, then select first API:
In “Request” we can add a new step “External Callout” where we can configure our mapping:
<ExternalCallout enabled="true" continueOnError="false" name="ExternalCallout-1">
<DisplayName>My External Callout</DisplayName>
<TimeoutMs>5000</TimeoutMs>
<GrpcConnection>
<Server name="mybackend"/>
</GrpcConnection>
<Configurations>
<Property name="with.request.content">true</Property>
<Property name="with.request.headers">false</Property>
<Property name="with.response.content">true</Property>
<Property name="with.response.headers">false</Property>
</Configurations>
<ExternalCallout>
Note: at line 6 I used the name of “Target Server” configured above.
In this way we are going to map request and response content, but not the request and response headers, in the request sent to the gRPC server.
We must perform this action for each endpoint that we want expose or offer as REST API.
In this way we can develop only one type of API using gRPC and with Apigee provides REST API, and also we can check Authentication in advance or provides API Key, even collects statistics of usage of our API.
See you space cowboy…
A small guide to configure Gitlab Feature Flags, Angular and Unleash