| Quaintous | Published: |
(tags)
|
(category)
|
|||||||||||||||||
| Updated: | ||||||||||||||||||||
| Map an existing REST API to a D-Bus service | ||||||||||||||||||||
The rationaleAs I started my new job, I was appointed to design and implement a REST API through which the industrial machines of our company were to be controlled. The existing controlling software used a permanent serial connection to communicate with the machine firmware. Since the serial connection could not be shared, either only my HTTP server (handling REST queries) or the controlling software could communicate with the machine at a time. Since both the controlling software and the HTTP server were running on a Linux machine, it was only convenient to use D-Bus as our IPC tool to enable both applications control the machine at the same time. DisclaimerI consider a fully one-to-one mapping of a REST API (including the HTTP server, etc.) to a D-Bus service madness! What follows is just a simple approach which should be regarded as a guide rather than a full specification. Object Path | Server URLA REST API is provided by a server which is reachable at a specific URL. Resources are to found under specific paths. For example At first I considered having each resource provided by its own object (e.g. HTTP Methods | Interface membersEach REST resource responds to different HTTP methods (e.g. HTTP requests can carry parameters as query string or as message body. On the other hand, D-Bus methods are normal functions with a number of parameters and a result value. Thus, there is no way of mapping HTTP request to D-Bus method calls without further specification. So I decided to have my interface methods all have the same signature with a string (serialized JSON) as input parameter and another one as output parameter. This way invocation of interface methods resembles an HTTP request with message body parameters. Since my API used JSON as the default media type for requests and responses. I decided to have my D-Bus parameters (both input and output) also in JSON format. Considering the fact that D-Bus has its own marshaling methods, it might sound irrational to have another layer of serialization (JSON strings) on the top. I could also try to be more idiomatic and use the closest thing resembling JSON in D-Bus, that is to mix and match container types, to imitate an HTTP request with JSON body. However, following reasons might justify this compromise:
GotchasIt is usual for REST APIs to address resources using their IDs. For example to fetch the user with ID ConclusionI this article a rather simple approach was provided to map an existing REST API to a D-Bus service. Such mapping might be useful if users/developers are already accustomed to existing procedures and data structures and want to migrate to and understand a new approach. The provided has the advantage of being easily understandable. Its downside, however, is the introduction of serializing (JSON) layer atop of D-Bus’s marshaling facility. The following table provides an overview of the mapping approach:
|
||||||||||||||||||||
| License: | ![]() |
🟢 No JavaScript | (Notes) | |||||||||||||||||
| 🟢 No Tracking | ||||||||||||||||||||
