Abstraction layer for Remote Procedure Calls (RPCs) over HTTP, using libcurl.
Send the observation observ
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_destroy(request)
call dm_rpc_destroy(response)
call dm_rpc_shutdown()
The URL returned by dm_rpc_url()
will equal
http://localhost:80/api/v1/observ
in this case. Add HTTP response
header names to array response%headers
to read them automatically:
allocate (response%headers(1))
response%headers(1)%name = 'etag'
rc = dm_rpc_post(request, response, observ, url)
The HTTP response header etag
is stored in response%headers(1)%value
afterwards. HTTP request headers have to be added to array
request%headers
.
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_IMAGE | = | '/image' |
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_HEADER_NAME_LEN | = | 32 |
Max. HTTP header name length. |
integer, | public, | parameter | :: | RPC_HEADER_VALUE_LEN | = | 512 |
Max. HTTP header value length. |
integer, | public, | parameter | :: | RPC_RESPONSE_UNIT_NONE | = | -99999 |
Default file unit. |
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 destroy routine.
Frees memory allocated by header type.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_header_type), | intent(inout) | :: | header |
Header type. |
Frees memory allocated by request type.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | request |
Request type. |
Frees memory allocated by response type.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_response_type), | intent(inout) | :: | response |
Response type. |
Generic RPC reset routine.
Auxiliary routine to reset request for future reuse. Cleans-up the libcurl handles of the request.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | request |
Request type. |
Auxiliary routine to reset response for future reuse. Response headers are kept and only header values are deallocated. This routine does not reset the file unit by default.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_response_type), | intent(inout) | :: | response |
Response type. |
||
logical, | intent(in), | optional | :: | reset_unit |
Reset file unit. |
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_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) | :: | responses(size(requests)) |
RPC response type array. |
||
class(*), | intent(inout) | :: | types(size(requests)) |
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) | :: | responses(size(requests)) |
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 request and response header type.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
character(len=:), | public, | allocatable | :: | name |
Header name. |
||
character(len=:), | public, | allocatable | :: | value |
Header value. |
HTTP-RPC response type.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | public | :: | code | = | HTTP_NONE |
HTTP response code. |
|
integer, | public | :: | error | = | E_NONE |
Error code of DMPACK. |
|
integer, | public | :: | error_curl | = | CURLE_OK |
Error code of libcurl easy. |
|
integer, | public | :: | unit | = | RPC_RESPONSE_UNIT_NONE |
Optional file unit. |
|
integer(kind=i8), | public | :: | last_modified | = | -1_i8 |
File time, -1 if unavailable [Epoch]. |
|
real(kind=r8), | public | :: | total_time | = | 0.0_r8 |
Total transmission time. |
|
character(len=:), | public, | allocatable | :: | error_message |
libcurl error message. |
||
character(len=:), | public, | allocatable | :: | content_type |
Response payload type [MIME]. |
||
character(len=:), | public, | allocatable | :: | payload |
Response payload. |
||
type(rpc_header_type), | public, | allocatable | :: | headers(:) |
HTTP response header. |
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, PUT). |
|
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. |
|
integer(kind=i8), | public | :: | modified_since | = | 0_i8 |
If-modified-since timestamp (Epoch). |
|
logical, | public | :: | follow_location | = | .true. |
Follow HTTP 3xx redirects. |
|
character(len=:), | public, | allocatable | :: | payload |
Request payload (POST). |
||
character(len=:), | public, | allocatable | :: | payload_path |
Request payload file path (PUT). |
||
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. |
||
type(rpc_header_type), | public, | allocatable | :: | headers(:) |
HTTP request header. |
||
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 libcurl easy stack error code to DMPACK error code.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | error_curl |
libcurl easy error code. |
Return message associated with given libcurl error code as allocatable character string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | error_curl |
libcurl error code. |
Error message.
Converts libcurl multi stack error code to DMPACK error code.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | multi_error |
libcurl multi error code. |
Sends generic HTTP GET request to URL.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | request |
RPC request type. |
||
type(rpc_response_type), | intent(inout) | :: | response |
RPC response type. |
||
character(len=*), | intent(in), | optional | :: | url |
URL of RPC API (may include port). |
|
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(kind=i8), | intent(in), | optional | :: | modified_since |
Only fetch if modified since given time [Epoch]. |
|
procedure(dm_rpc_callback), | optional | :: | callback |
Callback function to pass to libcurl. |
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) | :: | responses(size(requests)) |
RPC response type array. |
||
class(*), | intent(inout) | :: | types(size(requests)) |
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. |
Returns .true.
if request has associated callback procedure.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | request |
RPC request type. |
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) | :: | responses(size(requests)) |
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 response header value of name name
in argument value
. On
error, value
is allocated but empty.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | request |
RPC request type. |
||
character(len=*), | intent(in) | :: | name |
Header name. |
||
character(len=:), | intent(out), | allocatable | :: | value |
Header value. |
|
integer(kind=i8), | intent(out), | optional | :: | n |
Number of headers of this name. |
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 read callback function for libcurl. Reads chunks using fread(3). 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. |
Function return value.
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. |
Function return value.
Sets RPC request settings.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(rpc_request_type), | intent(inout) | :: | request | |||
integer, | intent(in), | optional | :: | auth |
HTTP Auth type ( |
|
integer, | intent(in), | optional | :: | method |
HTTP method (GET, POST). |
|
integer, | intent(in), | optional | :: | compression |
Use deflate or zstd compression ( |
|
integer, | intent(in), | optional | :: | connect_timeout |
Connection timeout in seconds. |
|
integer, | intent(in), | optional | :: | timeout |
Timeout in seconds. |
|
integer(kind=i8), | intent(in), | optional | :: | modified_since |
If-modified-since timestamp (Epoch). |
|
logical, | intent(in), | optional | :: | follow_location |
Follow HTTP 3xx redirects. |
|
character(len=*), | intent(in), | optional | :: | payload |
Request payload. |
|
character(len=*), | intent(in), | optional | :: | content_type |
Request payload type (MIME). |
|
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 | :: | url |
Request URL. |
|
character(len=*), | intent(in), | optional | :: | user_agent |
User Agent. |
|
procedure(dm_rpc_callback), | optional | :: | callback |
C-interoperable write callback function. |
Cleans up RPC backend.