Abstraction layer for Remote Procedure Calls (RPCs) over HTTP, using libcurl.
To send an observation to an HTTP-RPC API on localhost
:
character(len=:), allocatable :: url
integer :: rc
type(observ_type) :: observ
type(rpc_request_type) :: request
type(rpc_response_type) :: response
rc = dm_rpc_init()
call dm_error_out(rc, fatal=.true.)
url = dm_rpc_url('localhost', port=80, endpoint=RPC_ROUTE_OBSERV)
rc = dm_rpc_post(request, response, observ, url)
call dm_error_out(rc)
call dm_rpc_shutdown()
The URL returned by dm_rpc_url()
will equal
http://localhost:80/api/v1/observ
in this case.
The procedure dm_rpc_init()
has to be called once per process, and only
if neither the MQTT nor the mail backend is initialised already.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
character(len=*), | public, | parameter | :: | RPC_BASE | = | '/api/v1' |
Base path of dmapi service. |
character(len=*), | public, | parameter | :: | RPC_USER_AGENT | = | 'DMPACK '//DM_VERSION_STRING |
Default user agent of RPC client. |
character(len=*), | public, | parameter | :: | RPC_ROUTE_BEAT | = | '/beat' |
Resolves to |
character(len=*), | public, | parameter | :: | RPC_ROUTE_LOG | = | '/log' |
Resolves to |
character(len=*), | public, | parameter | :: | RPC_ROUTE_OBSERV | = | '/observ' |
Resolves to |
character(len=*), | public, | parameter | :: | RPC_ROUTE_NODE | = | '/node' |
Resolves to |
character(len=*), | public, | parameter | :: | RPC_ROUTE_SENSOR | = | '/sensor' |
Resolves to |
character(len=*), | public, | parameter | :: | RPC_ROUTE_TARGET | = | '/target' |
Resolves to |
integer, | public, | parameter | :: | RPC_AUTH_NONE | = | 0 |
No authentication. |
integer, | public, | parameter | :: | RPC_AUTH_BASIC | = | 1 |
HTTP Basic Auth. |
integer, | public, | parameter | :: | RPC_METHOD_GET | = | 0 |
HTTP GET method. |
integer, | public, | parameter | :: | RPC_METHOD_POST | = | 1 |
HTTP POST method. |
integer, | public, | parameter | :: | RPC_METHOD_PUT | = | 2 |
HTTP PUT method. |
logical, | public, | parameter | :: | RPC_KEEP_ALIVE | = | .true. |
Enable TCP keep-alive. |
integer, | public, | parameter | :: | RPC_KEEP_ALIVE_IDLE | = | 120 |
TCP keep-alive idle time in seconds. |
integer, | public, | parameter | :: | RPC_KEEP_ALIVE_INTERVAL | = | 60 |
Interval time between TCP keep-alive probes in seconds. |
Generic RPC post function.
Sends a single derived type in Namelist format to a given URL, with optional authentication and compression. The URL has to be the API endpoint that accepts HTTP POST requests.
The dummy argument type
may be of derived type beat_type
,
log_type
, node_type
, observ_type
, sensor_type
, or
target_type
. The function returns E_TYPE
on any other type.
The function returns the following error codes:
E_INVALID
if compression type is invalid.E_RPC
if request failed.E_TYPE
if type
is unsupported.E_ZLIB
if zlib libray call failed.E_ZSTD
if zstd libray call failed.Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | request |
RPC request type. |
||
type(rpc_response_type), | intent(out) | :: | response |
RPC response type. |
||
class(*), | intent(inout) | :: | type |
Derived type. |
||
character(len=*), | intent(in), | optional | :: | url |
URL of RPC API (may include port). |
|
character(len=*), | intent(in), | optional | :: | username |
HTTP Basic Auth user name. |
|
character(len=*), | intent(in), | optional | :: | password |
HTTP Basic Auth password. |
|
character(len=*), | intent(in), | optional | :: | user_agent |
HTTP User Agent. |
|
integer, | intent(in), | optional | :: | compression |
Deflate or Zstandard compression of payload for POST requests ( |
Sends multiple derived types concurrently in Namelist format to the given URL, with optional authentication and compression. The URL has to be the API endpoint that accepts HTTP POST requests.
The dummy argument types
may be of derived type beat_type
,
log_type
, node_type
, observ_type
, sensor_type
, or
target_type
. The function returns E_TYPE
on any other type.
If sequential
is .true.
, the transfer will be sequentially
instead of concurrently. The number of requests must match the
number of types, or E_CORRUPT
is returned.
The function returns the following error codes:
E_ALLOC
if memory allocation failed.E_CORRUPT
if sizes of requests and types array mismatch.E_INVALID
if compression type is invalid.E_RPC
if request failed.E_TYPE
if type of types
is unsupported.E_ZLIB
if zlib libray call failed.E_ZSTD
if zstd libray call failed.Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | requests(:) |
RPC request type array. |
||
type(rpc_response_type), | intent(out), | allocatable | :: | responses(:) |
RPC response type array. |
|
class(*), | intent(inout) | :: | types(:) |
Derived type array. |
||
character(len=*), | intent(in), | optional | :: | url |
URL of RPC API (may include port). |
|
character(len=*), | intent(in), | optional | :: | username |
HTTP Basic Auth user name. |
|
character(len=*), | intent(in), | optional | :: | password |
HTTP Basic Auth password. |
|
character(len=*), | intent(in), | optional | :: | user_agent |
HTTP User Agent. |
|
integer, | intent(in), | optional | :: | compression |
Deflate or Zstandard compression of payload for POST requests ( |
|
logical, | intent(in), | optional | :: | sequential |
Sequential instead of concurrent transfer. |
Generic RPC request function.
Sends multiple HTTP requests by GET, POST, or PUT method, with optional deflate or zstd compression.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | requests(:) |
RPC request type array. |
||
type(rpc_response_type), | intent(out), | allocatable | :: | responses(:) |
RPC response type array. |
|
character(len=*), | intent(in), | optional | :: | url |
URL of RPC API (may include port). |
|
integer, | intent(in), | optional | :: | method |
|
|
character(len=*), | intent(in), | optional | :: | accept |
HTTP Accept header. |
|
character(len=*), | intent(in), | optional | :: | username |
HTTP Basic Auth user name. |
|
character(len=*), | intent(in), | optional | :: | password |
HTTP Basic Auth password. |
|
character(len=*), | intent(in), | optional | :: | user_agent |
HTTP User Agent. |
|
integer, | intent(in), | optional | :: | compression |
Deflate or Zstandard compression of payload for POST requests ( |
Sends single HTTP request by GET, POST, or PUT method, and with optional deflate or zstd compression.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | request |
RPC request type. |
||
type(rpc_response_type), | intent(out) | :: | response |
RPC response type. |
||
character(len=*), | intent(in), | optional | :: | url |
URL of RPC API (may include port). |
|
integer, | intent(in), | optional | :: | method |
|
|
character(len=*), | intent(inout), | optional | :: | payload |
Payload data (for POST only). |
|
character(len=*), | intent(in), | optional | :: | content_type |
Payload content type (for POST only). |
|
character(len=*), | intent(in), | optional | :: | accept |
HTTP Accept header. |
|
character(len=*), | intent(in), | optional | :: | username |
HTTP Basic Auth user name. |
|
character(len=*), | intent(in), | optional | :: | password |
HTTP Basic Auth password. |
|
character(len=*), | intent(in), | optional | :: | user_agent |
HTTP User Agent. |
|
integer, | intent(in), | optional | :: | compression |
Deflate or Zstandard compression of payload for POST requests ( |
C-interoperable read/write callback for libcurl.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(c_ptr), | intent(in), | value | :: | ptr |
C pointer to a chunk of the response. |
|
integer(kind=c_size_t), | intent(in), | value | :: | size |
Always 1. |
|
integer(kind=c_size_t), | intent(in), | value | :: | nmemb |
Size of the response chunk. |
|
type(c_ptr), | intent(in), | value | :: | data |
C pointer to client data passed by caller. |
Function return value.
HTTP-RPC response type.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | public | :: | code | = | 0 |
HTTP response code. |
|
integer, | public | :: | error | = | E_NONE |
DMPACK error code. |
|
integer, | public | :: | error_curl | = | CURLE_OK |
cURL error code. |
|
real(kind=r8), | public | :: | total_time | = | 0.0_r8 |
Total transmission time. |
|
character(len=:), | public, | allocatable | :: | error_message |
cURL error message. |
||
character(len=:), | public, | allocatable | :: | content_type |
Response payload type (MIME). |
||
character(len=:), | public, | allocatable | :: | payload |
Response payload. |
HTTP-RPC request type.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | public | :: | auth | = | RPC_AUTH_NONE |
HTTP Auth. |
|
integer, | public | :: | method | = | RPC_METHOD_GET |
HTTP method (GET, POST). |
|
integer, | public | :: | compression | = | Z_TYPE_NONE |
Use deflate or zstd compression ( |
|
integer, | public | :: | connect_timeout | = | 30 |
Connection timeout in seconds. |
|
integer, | public | :: | timeout | = | 30 |
Timeout in seconds. |
|
logical, | public | :: | follow_location | = | .true. |
Follow HTTP 3xx redirects. |
|
character(len=:), | public, | allocatable | :: | payload |
Request payload. |
||
character(len=:), | public, | allocatable | :: | content_type |
Request payload type (MIME). |
||
character(len=:), | public, | allocatable | :: | accept |
HTTP Accept header. |
||
character(len=:), | public, | allocatable | :: | username |
HTTP Basic Auth user name. |
||
character(len=:), | public, | allocatable | :: | password |
HTTP Basic Auth password. |
||
character(len=:), | public, | allocatable | :: | url |
Request URL. |
||
character(len=:), | public, | allocatable | :: | user_agent |
User Agent. |
||
procedure(dm_rpc_callback), | public, | pointer, nopass | :: | callback | => | null() |
C-interoperable write callback function. |
Returns version number of libcurl an linked libreries as allocatable string.
Converts cURL easy stack error code to DMPACK error code.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | error_curl |
cURL easy error code. |
Return message associated with given cURL error code as allocatable character string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | error_curl |
cURL error code. |
Error message.
Converts cURL multi stack error code to DMPACK error code.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | multi_error |
cURL multi error code. |
Initialises RPC backend. The function returns E_RPC
on error.
Sends a single derived type in Namelist format to a given URL, with optional authentication and compression. The URL has to be the API endpoint that accepts HTTP POST requests.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | request |
RPC request type. |
||
type(rpc_response_type), | intent(out) | :: | response |
RPC response type. |
||
class(*), | intent(inout) | :: | type |
Derived type. |
||
character(len=*), | intent(in), | optional | :: | url |
URL of RPC API (may include port). |
|
character(len=*), | intent(in), | optional | :: | username |
HTTP Basic Auth user name. |
|
character(len=*), | intent(in), | optional | :: | password |
HTTP Basic Auth password. |
|
character(len=*), | intent(in), | optional | :: | user_agent |
HTTP User Agent. |
|
integer, | intent(in), | optional | :: | compression |
Deflate or Zstandard compression of payload for POST requests ( |
Sends multiple derived types concurrently in Namelist format to the given URL, with optional authentication and compression. The URL has to be the API endpoint that accepts HTTP POST requests.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | requests(:) |
RPC request type array. |
||
type(rpc_response_type), | intent(out), | allocatable | :: | responses(:) |
RPC response type array. |
|
class(*), | intent(inout) | :: | types(:) |
Derived type array. |
||
character(len=*), | intent(in), | optional | :: | url |
URL of RPC API (may include port). |
|
character(len=*), | intent(in), | optional | :: | username |
HTTP Basic Auth user name. |
|
character(len=*), | intent(in), | optional | :: | password |
HTTP Basic Auth password. |
|
character(len=*), | intent(in), | optional | :: | user_agent |
HTTP User Agent. |
|
integer, | intent(in), | optional | :: | compression |
Deflate or Zstandard compression of payload for POST requests ( |
|
logical, | intent(in), | optional | :: | sequential |
Sequential instead of concurrent transfer. |
Sends multiple HTTP requests by GET, POST, or PUT method, with optional deflate or zstd compression.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | requests(:) |
RPC request type array. |
||
type(rpc_response_type), | intent(out), | allocatable | :: | responses(:) |
RPC response type array. |
|
character(len=*), | intent(in), | optional | :: | url |
URL of RPC API (may include port). |
|
integer, | intent(in), | optional | :: | method |
|
|
character(len=*), | intent(in), | optional | :: | accept |
HTTP Accept header. |
|
character(len=*), | intent(in), | optional | :: | username |
HTTP Basic Auth user name. |
|
character(len=*), | intent(in), | optional | :: | password |
HTTP Basic Auth password. |
|
character(len=*), | intent(in), | optional | :: | user_agent |
HTTP User Agent. |
|
integer, | intent(in), | optional | :: | compression |
Deflate or Zstandard compression of payload for POST requests ( |
Sends single HTTP request by GET, POST, or PUT method, and with optional deflate or zstd compression.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | request |
RPC request type. |
||
type(rpc_response_type), | intent(out) | :: | response |
RPC response type. |
||
character(len=*), | intent(in), | optional | :: | url |
URL of RPC API (may include port). |
|
integer, | intent(in), | optional | :: | method |
|
|
character(len=*), | intent(inout), | optional | :: | payload |
Payload data (for POST only). |
|
character(len=*), | intent(in), | optional | :: | content_type |
Payload content type (for POST only). |
|
character(len=*), | intent(in), | optional | :: | accept |
HTTP Accept header. |
|
character(len=*), | intent(in), | optional | :: | username |
HTTP Basic Auth user name. |
|
character(len=*), | intent(in), | optional | :: | password |
HTTP Basic Auth password. |
|
character(len=*), | intent(in), | optional | :: | user_agent |
HTTP User Agent. |
|
integer, | intent(in), | optional | :: | compression |
Deflate or Zstandard compression of payload for POST requests ( |
Returns allocatable string of URL to HTTP-RPC API endpoint. Uses the
URL API of libcurl to create the URL. The base path and the endpoint
must both start with a /
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | host |
IP or FQDN of remote host. |
||
integer, | intent(in), | optional | :: | port |
API port (up to 5 digits). |
|
character(len=*), | intent(in), | optional | :: | base |
API base path (for example, |
|
character(len=*), | intent(in), | optional | :: | endpoint |
API endpoint (for example, |
|
logical, | intent(in), | optional | :: | tls |
TLS encryption (HTTPS). |
HTTP-RPC API endpoint URL.
C-interoperable write callback function for libcurl. Writes the
received response chunks to rpc_response_type
pointer that has to
be passed through C pointer data
. Do not call this function
directly.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(c_ptr), | intent(in), | value | :: | ptr |
C pointer to a chunk of the response. |
|
integer(kind=c_size_t), | intent(in), | value | :: | sz |
Always 1. |
|
integer(kind=c_size_t), | intent(in), | value | :: | nmemb |
Size of the response chunk. |
|
type(c_ptr), | intent(in), | value | :: | data |
C pointer to argument passed by caller. |
Auxiliary destructor routine to free allocated request memory. Cleans-up the cURL handles of the request.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | request |
Request type. |
Cleans up RPC backend.